在C#法式中對MessageBox停止定位的辦法。本站提示廣大學習愛好者:(在C#法式中對MessageBox停止定位的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是在C#法式中對MessageBox停止定位的辦法正文
在 C# 中沒有供給辦法用來對 MessageBox 停止定位,然則經由過程 C++ 你可以查找窗口並挪動它們,本文講述若何在 C# 中對 MessageBox 停止定位。
起首需在代碼上引入所需名字空間:
using System.Runtime.InteropServices; using System.Threading;
在你的 Form 類裡添加以下 DllImport 屬性:
[DllImport("user32.dll")]
static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow
[DllImport("user32.dll")]
static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool rePaint); // extern method: MoveWindow
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect); // extern method: GetWindowRect
接上去便可以查找窗口並挪動它:
void FindAndMoveMsgBox(int x, int y, bool repaint, string title)
{
Thread thr = new Thread(() => // create a new thread
{
IntPtr msgBox = IntPtr.Zero;
// while there's no MessageBox, FindWindow returns IntPtr.Zero
while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ;
// after the while loop, msgBox is the handle of your MessageBox
Rectangle r = new Rectangle();
GetWindowRect(msgBox, out r); // Gets the rectangle of the message box
MoveWindow(msgBox /* handle of the message box */, x , y,
r.Width - r.X /* width of originally message box */,
r.Height - r.Y /* height of originally message box */,
repaint /* if true, the message box repaints */);
});
thr.Start(); /: starts the thread
}
你要在 MessageBox.Show 之前挪用這個辦法,並確保 caption 參數不克不及為空,由於 title 參數必需等於 caption 參數。
應用辦法:
FindAndMoveMsgBox(0,0,true,"Title");
MessageBox.Show("Message","Title");