表单位置和大小行为

时间:2016-09-20 19:21:42

标签: c# .net forms winforms location

我想将表格放在屏幕的左上角。

我已尝试this.Location = new Point(0,0)但窗口位于(7,0) - 窗口顶部位于屏幕顶部,但左侧距屏幕边缘7个像素。 我创建了新的WinForms应用程序进行测试,只添加了这段代码:

private void Form1_Load(object sender, EventArgs e)
{
    Point p = new Point(0, 0);

    WindowState = FormWindowState.Maximized;
    Debug.WriteLine("\nMaximized");
    Debug.WriteLine("Location: " + Location);
    Debug.WriteLine("Size: " + Size);
    Debug.WriteLine("PointToScreen(0,0): " + PointToScreen(p));

    WindowState = FormWindowState.Normal;
    Location = p;            
    Debug.WriteLine("\nNormal");
    Debug.WriteLine("Location: " + Location);
    Debug.WriteLine("Size: " + Size);
    Debug.WriteLine("PointToScreen(0,0): " + PointToScreen(p));

    Debug.Write("\nScreen.PrimaryScreen.WorkingArea: ");
    Debug.WriteLine(Screen.PrimaryScreen.WorkingArea);
}

输出结果为:

Maximized  
Location: {X=-8,Y=-8}  
Size: {Width=1936, Height=1056}
PointToScreen(0,0): {X=0,Y=23}

Normal
Location: {X=0,Y=0}
Size: {Width=300, Height=300}
PointToScreen(0,0): {X=8,Y=31}

Screen.PrimaryScreen.WorkingArea: {X=0,Y=0,Width=1920,Height=1040}

为什么Location = new Point(0,0)没有在(0,0)上定位表格? 这是由于我的系统上有什么问题吗?我有Win10和VS2015。任务栏位于底部,桌面左侧没有任何内容。 为了将它定位在(0,0)上,我实际上必须将它放在(-7,0)上。此外,报告的最大化形式的宽度比屏幕宽度大16个像素。据我所知,由于窗口边缘,标题栏等客户区域大小和表单大小之间存在差异,但这不是它。当窗体最大化时,没有左右边缘(客户区宽度=桌面宽度),但窗体宽度为+ 16px。表格的4个边各有+ 8px,但Y位置正常。为什么Y位置OK而X不是?

2 个答案:

答案 0 :(得分:2)

感谢Uwe Keim和他自己的answer question,我创建了public void getGrade(double average){ double grade; if... 函数来计算偏移并正确设置表单的位置,而不管Windows版本(即边框大小):

MoveForm

void MoveForm(Point p) // Move form to point 'p' { this.WindowState = FormWindowState.Normal; this.Location = new Point(0, 0); Rectangle rec = WindowHelper.GetWindowRectangle(this.Handle); p.Offset(-rec.Location.X, -rec.Location.Y); this.Location = p; } 函数使用来自Uwe KeimpostMoveForm类:

WindowHelper

答案 1 :(得分:0)

我发现了问题。

只需设置

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

并测试结果。对我来说就是这样:

Maximized
Location: {X=0,Y=0}
Size: {Width=1280, Height=800}
PointToScreen(0,0): {X=0,Y=0}

Normal
Location: {X=0,Y=0}
Size: {Width=477, Height=321}
PointToScreen(0,0): {X=0,Y=0}

这完全是关于边界的。

Look at this

相关问题