MessageBox.Show怎么在父窗体居中
1个回答
展开全部
挺复杂的,得截获MessageBox显示。
C# code
using System.Runtime.InteropServices;public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);[DllImport("user32.dll")]public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid);[DllImport("user32.dll")]public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);[DllImport("kernel32.dll")]public static extern IntPtr GetModuleHandle(string modName);[DllImport("user32.dll")]public static extern bool UnhookWindowsHookEx(IntPtr hhook);[DllImport("user32.dll")]public static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);[DllImport("user32.dll")]public static extern bool MoveWindow( IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);public const int WH_CBT = 5;public const int HCBT_ACTIVATE = 5;IntPtr hookHandle = IntPtr.Zero;private IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam){ switch(nCode) { case HCBT_ACTIVATE: Rectangle vRectangle = new Rectangle(); GetWindowRect(wParam, ref vRectangle); vRectangle.Width = vRectangle.Width - vRectangle.Left; vRectangle.Height = vRectangle.Height - vRectangle.Top; MoveWindow(wParam, // 右下 Screen.GetWorkingArea(this).Width - vRectangle.Width, Screen.GetWorkingArea(this).Height - vRectangle.Height, vRectangle.Width, vRectangle.Height, false); UnhookWindowsHookEx(hookHandle); break; } return CallNextHookEx(hookHandle, nCode, wParam, lParam);}private void button1_Click(object sender, EventArgs e){ hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback), GetModuleHandle(null), 0); MessageBox.Show("Zswang 路过");}
C# code
using System.Runtime.InteropServices;public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);[DllImport("user32.dll")]public static extern IntPtr SetWindowsHookEx(int hookid, HookProc pfnhook, IntPtr hinst, int threadid);[DllImport("user32.dll")]public static extern IntPtr CallNextHookEx(IntPtr hhook, int code, IntPtr wparam, IntPtr lparam);[DllImport("kernel32.dll")]public static extern IntPtr GetModuleHandle(string modName);[DllImport("user32.dll")]public static extern bool UnhookWindowsHookEx(IntPtr hhook);[DllImport("user32.dll")]public static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);[DllImport("user32.dll")]public static extern bool MoveWindow( IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);public const int WH_CBT = 5;public const int HCBT_ACTIVATE = 5;IntPtr hookHandle = IntPtr.Zero;private IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam){ switch(nCode) { case HCBT_ACTIVATE: Rectangle vRectangle = new Rectangle(); GetWindowRect(wParam, ref vRectangle); vRectangle.Width = vRectangle.Width - vRectangle.Left; vRectangle.Height = vRectangle.Height - vRectangle.Top; MoveWindow(wParam, // 右下 Screen.GetWorkingArea(this).Width - vRectangle.Width, Screen.GetWorkingArea(this).Height - vRectangle.Height, vRectangle.Width, vRectangle.Height, false); UnhookWindowsHookEx(hookHandle); break; } return CallNextHookEx(hookHandle, nCode, wParam, lParam);}private void button1_Click(object sender, EventArgs e){ hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback), GetModuleHandle(null), 0); MessageBox.Show("Zswang 路过");}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询