ZenosZeng 发表于 2012-10-31 19:34:00

在 TX Text Control 中实现 “Fit to page” 和“Fit to width”

随着电脑硬件配置的不断提升,现在主流的显示器已经达到21-24寸,甚至更大的显示器。

在这种大尺寸显示器中,缩放页面至可用宽度或者将整页显示在课间范围内的功能需求就显得更加有意义。

TX Text Control 提供了一个 ZoomFactor 属性用于设置页面的缩放比例,下面的代码演示了如何将当前页全部显示在可见范围内:
private void FitToWindow()
{
    textControl1.PageUnit = MeasuringUnit.Twips;
    int iVisibleGap = 65;

    Graphics g = textControl1.CreateGraphics();
    int iTwipsPerPixel = (int)(1440 / g.DpiX);

    SectionFormat currentSection = textControl1.Sections.GetItem().Format;

    double widthZoom = 100 * textControl1.Width /
      ((currentSection.PageSize.Width / iTwipsPerPixel)
      + iVisibleGap);
    double heightZoom = 100 * textControl1.Height /
      ((currentSection.PageSize.Height / iTwipsPerPixel)
      + iVisibleGap);

    if (widthZoom < heightZoom)
      textControl1.ZoomFactor = (int)widthZoom;
    else
      textControl1.ZoomFactor = (int)heightZoom;
}

以下方法演示了如何让页面宽度达到可使用的最大宽度:
private void FitToWidth()
{
    textControl1.PageUnit = MeasuringUnit.Twips;
    int iVisibleGap = 200;

    // get resolution to calculate convert twips 1/100 inch
    Graphics g = textControl1.CreateGraphics();
    int iTwipsPerPixel = (int)(1440 / g.DpiX);

    SectionFormat currentSection = textControl1.Sections.GetItem().Format;

    double widthZoom = 100 * textControl1.Width /
      ((currentSection.PageSize.Width / iTwipsPerPixel) + iVisibleGap);

    textControl1.ZoomFactor = (int)widthZoom;
}

Fit to page:


Fit to width:


源码下载:VS2010 + TX Text Control 17.0/X8
页: [1]
查看完整版本: 在 TX Text Control 中实现 “Fit to page” 和“Fit to width”