2009-07-24 51 views

回答

2

尝试表单的DesktopBounds属性。

+0

如果`FormBorderStyle`是`FixedToolWindow`,那么我不认为`DesktopBounds`比`Size`提供更好的答案。如果启用Aero,我认为这两者都不正确。 – 2013-11-21 23:47:44

2

Size属性应该可以工作。请注意,由于设计机器和生产机器之间系统字体或视频适配器DPI设置的差异,表单可能会重新缩放。在Load事件之前,实际尺寸将不可用。

0

如果启用了Aero并且您的FormBorderStyleFixedToolWindow,则Windows将对该表单的大小进行说明。我认为Form中的以下代码将给出这种窗口的正确高度和宽度。

[DllImport("dwmapi.dll", PreserveSig = false)] 
public static extern bool DwmIsCompositionEnabled(); 

// When Aero is enabled, and our FormBorderStyle is FixedToolWindow, 
// Windows will lie to us about our size and position. 
public bool AeroIsMessingWithUs() 
{ 
    bool ret = false; 

    // check for other Fixed styles here if needed 
    if (FormBorderStyle == System.Windows.Forms.FormBorderStyle.FixedToolWindow) 
    { 
     if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled()) 
     { 
      // Aero is enabled 
      ret = true; 
     } 
    } 
    return ret; 
} 

public int MyWindowHeight() 
{ 
    int height = Height; 
    if (AeroIsMessingWithUs()) 
    { 
     // there are actually 5 more pixels on the top and bottom 
     height += 10; 
    } 
    return height; 
} 

public int MyWindowWidth() 
{ 
    int width = Width; 
    if (AeroIsMessingWithUs()) 
    { 
     // there are 5 more pixels on the left and right 
     width += 10; 
    } 
    return width; 
} 
相关问题