且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#设置Windows窗体的修复位置

更新时间:2023-12-06 11:06:22

要做的第一件事:将Form属性StartPostion更改为Manual。

然后,处理LocationChanged事件:

  private   void  frmMain_LocationChanged( object  sender,EventArgs e)
{
Location = new Point( 0 0 );
}

当他们试图移动它时,你会快速闪烁,但表格将始终位于屏幕的TLHC中。对于TRHC来说,它有点复杂:

  private   void  frmMain_LocationChanged( object  sender,EventArgs e)
{
Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, 0 );
}

你也想要处理Resize事件。


在这种情况下,我认为你没有选择合适的设计选项。手头的任务。



如果你想永远不要移动一个Form,那么为什么要让它被移动:为什么不通过使用各种Form属性去除TitleBar像FormBorderStyle一样,将Form的Text设置为空字符串,将MinimizeBox,MaximizeBox,ControlBox等设置为适当的值。



如果表单有一个TitleBar,你离开了ControlBox,MinimizeBox和MinimizeBox,装饰就位,你无法取消用户移动表格的能力:是的,你可以快速将它复制到位通过覆盖Move或LocationChanged EventHandlers,但用户仍然可以拖动一个空的Rectangle。这不是一件好事。



fyi:当表格被移动时,'移动事件在LocationChanged事件之前被提升。



您可以使用Windows API / pInvoke来防止表单移动:使用Google时不到5秒就会出现这样的情况:在c#中创建一个不动的窗体[ ^ ]。注意:我没有尝试过这段代码。



只需将Form的MinimumSize和MaximumSize属性设置为与所需的Size参数相同,即可解决其他问题。表格。



但是,正如(我认为)我尊敬的导师和同事OriginalGriff说的那样,你真的违背了Windows UI惯例

Consider there is a windows form.I need to give specific location(always) to this windows form(Ex: right top corner on desktop). If form will dragged by the user,after release the mouse, form must go to the same place. Please somebody help me.

First thing to do: change the Form property StartPostion to Manual.
Then, handle the LocationChanged event:
private void frmMain_LocationChanged(object sender, EventArgs e)
    {
    Location = new Point(0, 0);
    }

You will get quick "flashes" when they try to move it, but the form will always be in the TLHC of the screen. For TRHC it's a little more complex:

private void frmMain_LocationChanged(object sender, EventArgs e)
    {
    Location = new Point(Screen.PrimaryScreen.Bounds.Width - Width, 0);
    }

And you will want to handle the Resize event as well.


In this case I think you are not choosing the right design option for the task at hand.

If you want a Form to never be moved, then why allow it to be moved: why not get rid of the TitleBar through using various Form properties like FormBorderStyle, setting 'Text of the Form to an empty string, set the MinimizeBox, MaximizeBox, ControlBox, etc., to the appropriate values.

If the Form has a TitleBar, and you leave the ControlBox, MinimizeBox, and MinimizeBox, decorations in place, there is no way you are going to be able to cancel the user's ability to move the Form around: yes, you can "snap it" back in place by over-riding the Move, or LocationChanged EventHandlers, but the user is still going to be able to drag-around an empty Rectangle. This is not a good thing.

fyi: when a Form is moved, the 'Move Event is raised before the LocationChanged Event.

You could use the Windows API/pInvoke to prevent Form movement: less than 5 seconds using Google turned up this: "Creating a immovable windows’ form in c#" [^]. Note: I haven't tried this code.

Other issues you raise can be addressed by simply setting the Form's MinimumSize and MaximumSize Properties to be identical to the desired Size parameters of the Form.

But, as (I think) my esteemed mentor, and colleague, OriginalGriff, says, you are really going against the grain of Windows UI conventions here.