C# 如何防止窗户被移动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/907830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you prevent a windows from being moved?
提问by Ozzy
How would i go about stopping a form from being moved. I have the form border style set as FixedSingle and would like to keep it this way because it looks good in vista :)
我将如何停止移动表单。我将表单边框样式设置为 FixedSingle 并希望保持这种方式,因为它在 vista 中看起来不错:)
采纳答案by Jason Down
Take a look at this link. You might be interested in option #3. It will require you to wrap some native code, but should work. There's also a comment at the bottom of the link that shows an easier way to do it. Taken from the comment (can't take credit for it, but I'll save you some searching):
看看这个链接。您可能对选项 #3 感兴趣。它需要您包装一些本机代码,但应该可以工作。链接底部还有一条评论,显示了一种更简单的方法。摘自评论(不能相信它,但我会为您节省一些搜索时间):
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch(message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
return;
break;
}
base.WndProc(ref message);
}
回答by Fredrik M?rk
It's not all pretty (there is some flashing going on when you try to move the form), but you can use the LocationChanged property to keep the form where you want it:
这并不是很漂亮(当您尝试移动表单时会有一些闪烁),但是您可以使用 LocationChanged 属性将表单保留在您想要的位置:
private Point _desiredLocation;
// assign the _desiredLocation variable with the form location at some
// point in the code where you know that the form is in the "correct" position
private void Form_LocationChanged(object sender, EventArgs e)
{
if (this.Location != _desiredLocation)
{
this.Location = _desiredLocation;
}
}
Out of curiousity; why would you want to do this?
出于好奇;你为什么想做这个?
回答by Tommy Hui
In Windows, the WS_CAPTION style is the non-client area that allows your window to be moved with a mouse. So the easiest way to do what you want is to remove this style from your window.
在 Windows 中,WS_CAPTION 样式是允许用鼠标移动窗口的非客户区。所以做你想做的最简单的方法是从你的窗口中删除这个样式。
However, if you need to have a caption and still achieve what you want, then the next style would be to capture the WM_NCHITTEST message and check for HTCAPTION. If the code is HTCAPTION, return NTNOWHERE instead. This will prevent the default window procedure from executing the default move window thing.
然而,如果您需要一个标题并且仍然实现您想要的,那么下一个样式将是捕获 WM_NCHITTEST 消息并检查 HTCAPTION。如果代码是 HTCAPTION,则返回 NTNOWHERE。这将阻止默认窗口过程执行默认的移动窗口事物。
回答by David McEwing
I would question your need to make the form unmovable. This doesn't sound nice. You could of course save the location of the window when the window closes and reopen the window into that position. That gives the user some control over where the window should be located.
我会质疑您是否需要使表格不可移动。这听起来不太好。您当然可以在窗口关闭时保存窗口的位置,然后将窗口重新打开到该位置。这使用户可以对窗口的位置进行一些控制。
回答by Dylan Roberts
You can try:
你可以试试:
this.Locked = true;
回答by V4Vendetta
I found this to stop the form from moving (its in c#)
我发现这可以阻止表单移动(它在 c# 中)
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
switch (m.Msg)
{
case WM_SYSCOMMAND:
int command = m.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
return;
break;
}
base.WndProc(ref m);
}
回答by crypted
You can set the FormBorderStyle
property of the Form to None
您可以将FormBorderStyle
Form的属性设置为 None
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None
回答by Kamyar
It's not a good practice to make your form immovable. I'd think agfain about it if I were you.
Anyway, you can do this by overridding the WinProc to disable the [Move] menuitem from the system menu.
使您的表单不可移动并不是一个好习惯。如果我是你,我会考虑的。
无论如何,您可以通过覆盖 WinProc 以禁用系统菜单中的 [Move] 菜单项来实现此目的。
[DllImport("user32.dll")]
private static extern Int32 EnableMenuItem ( System.IntPtr hMenu , Int32uIDEnableItem, Int32 uEnable);
private const Int32 HTCAPTION = 0×00000002;
private const Int32 MF_BYCOMMAND =0×00000000;
private const Int32 MF_ENABLED =0×00000000;
private const Int32 MF_GRAYED =0×00000001;
private const Int32 MF_DISABLED =0×00000002;
private const Int32 SC_MOVE = 0xF010;
private const Int32 WM_NCLBUTTONDOWN = 0xA1;
private const Int32 WM_SYSCOMMAND = 0×112;
private const Int32 WM_INITMENUPOPUP = 0×117;
protected override void WndProc(ref System.Windows.Forms.Message m )
{
if (m.Msg == WM_INITMENUPOPUP)
{
//handles popup of system menu
if ((m.LParam.ToInt32() / 65536) != 0) // 'divide by 65536 to get hiword
{
Int32 AbleFlags = MF_ENABLED;
if (!Moveable)
{
AbleFlags = MF_DISABLED | MF_GRAYED; // disable the move
}
EnableMenuItem(m.WParam, SC_MOVE, MF_BYCOMMAND | AbleFlags);
}
}
if (!Moveable)
{
if (m.Msg == WM_NCLBUTTONDOWN) //cancels the drag this is IMP
{
if (m.WParam.ToInt32() == HTCAPTION) return;
}
if (m.Msg == WM_SYSCOMMAND) // Cancels any clicks on move menu
{
if ((m.WParam.ToInt32() & 0xFFF0) == SC_MOVE) return;
}
}
base.WndProc(ref m);
}
Also, you can handle OnMove
event of your form. But I think this will cause some flickering:
此外,您可以处理OnMove
表单的事件。但我认为这会导致一些闪烁:
private void Form1_Move(object sender, EventArgs e)
{
this.Location = defaultLocation;
}
回答by Renatas M.
Try to override WndProc:
尝试覆盖 WndProc:
protected override void WndProc(ref Message m)
{
const int WM_NCLBUTTONDOWN = 161;
const int WM_SYSCOMMAND = 274;
const int HTCAPTION = 2;
const int SC_MOVE = 61456;
if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE))
{
return;
}
if ((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION))
{
return;
}
base.WndProc(ref m);
}