C#窗口位置 [英] C# window positioning

查看:183
本文介绍了C#窗口位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Windows窗体我想靠窗的位置成特定COORDS。我认为它可以简单的方式来完成,但下面的代码无法在所有的工作:

Using Windows Forms I wanted to position window into specific coords. I thought it can be done in simple way, but following code does not work at all:

public Form1()
{
    InitializeComponent();

    this.Top = 0;
    this.Left = 0;
}



然而,当只得到处理该窗口,它工作得很好:

However, when only get handle to that window, it works well:

public Form1()
{
    InitializeComponent();

    IntPtr hwnd = this.Handle;
    this.Top = 0;
    this.Left = 0;
}

您可以看到,我不与指针的工作都没有。我发现在MSDN下面的语句:

You can see that I am not working with that pointer at all. I found at MSDN following statement:

Handle属性的值是一个Windows HWND。如果该手柄具有
尚未创建,参照该属性将强制要创建的手柄

The value of the Handle property is a Windows HWND. If the handle has not yet been created, referencing this property will force the handle to be created.

这是否意味着我们可以设置窗口的位置后,才创立了手柄的?使用此手柄内部制定者顶部/左侧?感谢您的澄清。

Does it mean that we can set window position only AFTER the creation of its handle? Are setters Top/Left using this handle internally? Thank you for clarification.

推荐答案

通常一个WinForm是根据StartupPosition物业定位在屏幕上。结果
本意味着从Form1的构造函数退出后,窗口管理器,按照该物业是建立窗口和位置。结果
如果将 StartupPosition =手动,然后Left和Top通过设计者设定的值(位置)将被aknowledged。结果,
请参阅MSDN 为StartupPosition,也为的 FormStartPosition 枚举。

Usually a WinForm is positioned on the screen according to the StartupPosition property.
This means that after exiting from the constructor of Form1 the Window Manager build the window and position it according to that property.
If you set StartupPosition = Manual then the Left and Top values (Location) set via the designer will be aknowledged.
See MSDN for the StartupPosition and also for the FormStartPosition enum.

当然,这除去需要使用 this.Handle
(我假设你引用迫使窗口管理器中StartupPosition使用设计值打造立刻形式的财产)

Of course this remove the need to use this.Handle. (I suppose that referencing that property you are forcing the windows manager to build immediatly the form using the designer values in StartupPosition)

这篇关于C#窗口位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆