2013-10-17 27 views
0

我有,對採用基於文本大小的字體大小的變化對整個窗口改變了Windows窗體應用程序。即使整個窗口的文本大小從小到中更改,如何在Windows窗體應用程序中保持默認字體大小?我已將AutoScalMode用於None,但字體大小與控件相互重疊(如果它們彼此非常接近)。即使將整個Windows Text大小從Small更改爲Medium,如何保持Windows Forms應用程序的默認字體大小?

overlap controls

感謝

+0

您明確*不*要設置AutoScaleMode爲無。它必須是Font才能使表單正確縮放。確保窗體的字體設置正確,並且控件只是繼承窗體的字體。右鍵單擊它們的Font屬性並點擊重置。 –

+0

如果你不設置它們從容器繼承它們,那麼這些屬性是環境屬性,你爲表單設置字體還是隻設置文本框? – terrybozzio

回答

1

不幸的是它與WinForms的一個普遍問題。

如果我明白你的問題 - 當你手動設置字體中的WinForms控制F.E.出現問題標籤。 根據this answer,我寫下面的代碼。也許這將幫助

獲得實際的dpi的顯示器尺寸:

public class FontResizeFactorProvider 
    { 
     private const int DpiSmallSize = 96; 
     private const int DpiMediumSize = 120; 
     private const int DpiLargeSize = 144; 

     public float GetFontResizeFactor() 
     { 
      var graphics = Graphics.FromHwnd(IntPtr.Zero); 
      IntPtr desktop = graphics.GetHdc(); 

      var displayDimensions = new List<int> 
       { 
        GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX), 
        GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY) 
       }; 

      var matchedDim = displayDimensions.First(dim => dim == DpiSmallSize || dim == DpiMediumSize || dim == DpiLargeSize); 

      if (matchedDim == default(int)) 
      { 
       throw new ArgumentException("Dpi size not standard."); 
      } 

      var resizeFactor = (float) DpiSmallSize/matchedDim; 

      return resizeFactor; 
     } 

     [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
     public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); 

     public enum DeviceCap 
     { 
      /// <summary> 
      /// Logical pixels inch in X 
      /// </summary> 
      LOGPIXELSX = 88, 
      /// <summary> 
      /// Logical pixels inch in Y 
      /// </summary> 
      LOGPIXELSY = 90 

      // Other constants may be founded on pinvoke.net 
     } 
    } 

服務負責表格大小調整字體:

public class FormFontsResizeService 
    { 
     private const double EPSILON = 0.1; 
     private readonly FontResizeFactorProvider _fontResizeFactorProvider; 

     public FormFontsResizeService():this(new FontResizeFactorProvider()) 
     { 
     } 

     public FormFontsResizeService(FontResizeFactorProvider fontResizeFactorProvider) 
     { 
      _fontResizeFactorProvider = fontResizeFactorProvider; 
     } 

     public void ResizeControlFonts(Form form) 
     { 
      var resizeFactor = _fontResizeFactorProvider.GetFontResizeFactor(); 

      foreach (Control control in form.Controls) 
      { 
       var baseFont = control.Font; 

       if (Math.Abs(baseFont.Size - SystemFonts.DefaultFont.Size) < EPSILON) 
       { 
        continue; 
       } 

       var scaledFont = new Font(baseFont.FontFamily, baseFont.Size*resizeFactor, baseFont.Style); 
       control.Font = scaledFont; 
      } 
     } 
    } 

使用表格:

protected override void OnShown(System.EventArgs e) 
     { 
      var formFontsResizeService = new FormFontsResizeService(); 
      formFontsResizeService.ResizeControlFonts(this); 
     } 

而且檢查this question

相關問題