我想限制窗體可以移動到桌面的位置。基本上我不希望他們能夠將表格從桌面上移開。我發現了一堆SetBounds函數,但它們似乎對我的函數名稱看起來很奇怪,並沒有達到我的目的。C#想要限制窗體可以移動到的位置
1
A
回答
0
我認爲,如果您將邊框樣式設置爲無,那麼您無法移動該表單。
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle%28VS.71%29.aspx
1
只是處理Move
事件,或覆蓋OnMove
,以確保窗口是在桌面上:
protected override OnMove(EventArgs e)
{
if (Screen.PrimaryScreen.WorkingArea.Contains(this.Location))
{
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}
}
0
我只是做了和OnLocationChanged的覆蓋。這很粗糙,但工作,我只有一天,才能找到一個修復,所以我完成了。表單的長度和寬度是544和312. OnMove和OnLocationChanged有什麼區別?
protected override void OnLocationChanged(EventArgs e)
{
if (this.Location.X > Screen.PrimaryScreen.WorkingArea.X + Screen.PrimaryScreen.WorkingArea.Width - 544)
{
this.SetBounds(0, 0, 544, 312);
}
else if(this.Location.X < Screen.PrimaryScreen.WorkingArea.X)
{
this.SetBounds(0, 0, 544, 312);
}
if (this.Location.Y > Screen.PrimaryScreen.WorkingArea.Y + Screen.PrimaryScreen.WorkingArea.Height - 312)
{
this.SetBounds(0, 0, 544, 312);
}
else if (this.Location.Y < Screen.PrimaryScreen.WorkingArea.Y)
{
this.SetBounds(0, 0, 544, 312);
}
}
5
我意識到你對答案不再感興趣,反正我會發布解決方案。您想要處理WM_MOVING消息並覆蓋目標位置。請注意,它在Win7上有副作用,如果用戶有多個顯示器,則不適用。鼠標位置處理也不是很好。代碼:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void WndProc(ref Message m) {
if (m.Msg == 0x216) { // Trap WM_MOVING
RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
Screen scr = Screen.FromRectangle(Rectangle.FromLTRB(rc.left, rc.top, rc.right, rc.bottom));
if (rc.left < scr.WorkingArea.Left) {rc.left = scr.WorkingArea.Left; rc.right = rc.left + this.Width; }
if (rc.top < scr.WorkingArea.Top) { rc.top = scr.WorkingArea.Top; rc.bottom = rc.top + this.Height; }
if (rc.right > scr.WorkingArea.Right) { rc.right = scr.WorkingArea.Right; rc.left = rc.right - this.Width; }
if (rc.bottom > scr.WorkingArea.Bottom) { rc.bottom = scr.WorkingArea.Bottom; rc.top = rc.bottom - this.Height; }
Marshal.StructureToPtr(rc, m.LParam, false);
}
base.WndProc(ref m);
}
private struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
}
}
相關問題
- 1. 可可限制窗口移動區域?
- 2. Form.Show()稍微移動窗體位置
- 3. Vlookup限制 - 想要向後轉移
- 4. C#窗體控件移動
- 5. 球體中的物體(邊界球體),想要限制球體內的運動
- 6. 如何正確地停止移動物體在我想要的位置
- 7. 如果可以,我可以移動modalpopextender編輯窗體嗎?
- 8. 將繪製的圓移動到C#中的固定位置WPF
- 9. 位置:GPS /網絡權限:想要如果可用
- 10. C#和窗體上的位置
- 11. 移動行到列(限制我可以使用什麼方法)
- 12. 無法讓圖片移動到我想要的位置(並不斷更新)?
- 13. Captionless但可移動的WinForms窗體
- 14. 從C中的圖片框移動窗體窗體#
- 15. 爲窗體移動設置窗體的背景圖像
- 16. C#儘快將文件移動到可用位置
- 17. 如何設置想要的位置與德比在java想要的位置
- 18. C#:將控件從窗體移動到VSPage窗體設計器中的tabPage
- 19. C#Windows窗體。 PictureBox上的可移動區域
- 20. Listview子項目位置(C#窗體)
- 21. 的Android移動窗體Form1到FORM2
- 22. 我想設置限制滾動到一個滾動面板
- 23. 如何在C#.Net中移動窗體?
- 24. C# - 使窗體移動時半透明
- 25. C#窗體移動停止事件
- 26. 限制qgraphicsitem的可移動區域
- 27. 限制可以輸入到Windows窗體文本框的字符數
- 28. c#從子窗體返回主窗體在同一位置
- 29. 需要限制訪問移動網站到特定的註冊移動設備
- 30. 限制攝像機位置移動到特定3D對象的區域
是的,但是你不能移動它*完全*,這不是OP所要求的... – 2009-12-11 01:33:41