質問: 親フォームの中央のmessagebox .net 2.0の親フォームにmessageboxを中央に配置する簡単な方法はありますか
回答: 私はこれをc#で本当に必要としていました center messagebox c# これはきれいにフォーマットされたバージョンですusing system; using system.windows.forms; using system.text; using system.drawing; using system.runtime.interopservices; public class messageboxex { private static iwin32window _owner; private static hookproc _hookproc; private static intptr _hhook; public static dialogresult show(string text) { initialize(); return messagebox.show(text); } public static dialogresult show(string text, string caption) { initialize(); return messagebox.show(text, caption); } public static dialogresult show(string text, string caption, messageboxbuttons buttons) { initialize(); return messagebox.show(text, caption, buttons); } public static dialogresult show(string text, string caption, messageboxbuttons buttons, messageboxicon icon) { initialize(); return messagebox.show(text, caption, buttons, icon); } public static dialogresult show(string text, string caption, messageboxbuttons buttons, messageboxicon icon, messageboxdefaultbutton defbutton) { initialize(); return messagebox.show(text, caption, buttons, icon, defbutton); } public static dialogresult show(string text, string caption, messageboxbuttons buttons, messageboxicon icon, messageboxdefaultbutton defbutton, messageboxoptions options) { initialize(); return messagebox.show(text, caption, buttons, icon, defbutton, options); } public static dialogresult show(iwin32window owner, string text) { _owner = owner; initialize(); return messagebox.show(owner, text); } public static dialogresult show(iwin32window owner, string text, string caption) { _owner = owner; initialize(); return messagebox.show(owner, text, caption); } public static dialogresult show(iwin32window owner, string text, string caption, messageboxbuttons buttons) { _owner = owner; initialize(); return messagebox.show(owner, text, caption, buttons); } public static dialogresult show(iwin32window owner, string text, string caption, messageboxbuttons buttons, messageboxicon icon) { _owner = owner; initialize(); return messagebox.show(owner, text, caption, buttons, icon); } public static dialogresult show(iwin32window owner, string text, string caption, messageboxbuttons buttons, messageboxicon icon, messageboxdefaultbutton defbutton) { _owner = owner; initialize(); return messagebox.show(owner, text, caption, buttons, icon, defbutton); } public static dialogresult show(iwin32window owner, string text, string caption, messageboxbuttons buttons, messageboxicon icon, messageboxdefaultbutton defbutton, messageboxoptions options) { _owner = owner; initialize(); return messagebox.show(owner, text, caption, buttons, icon, defbutton, options); } public delegate intptr hookproc(int ncode, intptr wparam, intptr lparam); public delegate void timerproc(intptr hwnd, uint umsg, uintptr nidevent, uint dwtime); public const int wh_callwndprocret = 12; public enum cbthookaction : int { hcbt_movesize = 0, hcbt_minmax = 1, hcbt_qs = 2, hcbt_createwnd = 3, hcbt_destroywnd = 4, hcbt_activate = 5, hcbt_clickskipped = 6, hcbt_keyskipped = 7, hcbt_syscommand = 8, hcbt_setfocus = 9 } [dllimport("user32.dll")] private static extern bool getwindowrect(intptr hwnd, ref rectangle lprect); [dllimport("user32.dll")] private static extern int movewindow(intptr hwnd, int x, int y, int nwidth, int nheight, bool brepaint); [dllimport("user32.dll")] public static extern uintptr settimer(intptr hwnd, uintptr nidevent, uint uelapse, timerproc lptimerfunc); [dllimport("user32.dll")] public static extern intptr sendmessage(intptr hwnd, int msg, intptr wparam, intptr lparam); [dllimport("user32.dll")] public static extern intptr setwindowshookex(int idhook, hookproc lpfn, intptr hinstance, int threadid); [dllimport("user32.dll")] public static extern int unhookwindowshookex(intptr idhook); [dllimport("user32.dll")] public static extern intptr callnexthookex(intptr idhook, int ncode, intptr wparam, intptr lparam); [dllimport("user32.dll")] public static extern int getwindowtextlength(intptr hwnd); [dllimport("user32.dll")] public static extern int getwindowtext(intptr hwnd, stringbuilder text, int maxlength); [dllimport("user32.dll")] public static extern int enddialog(intptr hdlg, intptr nresult); [structlayout(layoutkind.sequential)] public struct cwpretstruct { public intptr lresult; public intptr lparam; public intptr wparam; public uint message; public intptr hwnd; } ; static messageboxex() { _hookproc = new hookproc(messageboxhookproc); _hhook = intptr.zero; } private static void initialize() { if (_hhook != intptr.zero) { throw new notsupportedexception("multiple calls are not supported"); } if (_owner != null) { _hhook = setwindowshookex(wh_callwndprocret, _hookproc, intptr.zero, appdomain.getcurrentthreadid()); } } private static intptr messageboxhookproc(int ncode, intptr wparam, intptr lparam) { if (ncode < 0) { return callnexthookex(_hhook, ncode, wparam, lparam); } cwpretstruct msg = (cwpretstruct)marshal.ptrtostructure(lparam, typeof(cwpretstruct)); intptr hook = _hhook; if (msg.message == (int)cbthookaction.hcbt_activate) { try { centerwindow(msg.hwnd); } finally { unhookwindowshookex(_hhook); _hhook = intptr.zero; } } return callnexthookex(hook, ncode, wparam, lparam); } private static void centerwindow(intptr hchildwnd) { rectangle recchild = new rectangle(0, 0, 0, 0); bool success = getwindowrect(hchildwnd, ref recchild); int width = recchild.width - recchild.x; int height = recchild.height - recchild.y; rectangle recparent = new rectangle(0, 0, 0, 0); success = getwindowrect(_owner.handle, ref recparent); point ptcenter = new point(0, 0); ptcenter.x = recparent.x + ((recparent.width - recparent.x) / 2); ptcenter.y = recparent.y + ((recparent.height - recparent.y) / 2); point ptstart = new point(0, 0); ptstart.x = (ptcenter.x - (width / 2)); ptstart.y = (ptcenter.y - (height / 2)); ptstart.x = (ptstart.x < 0) ? 0 : ptstart.x; ptstart.y = (ptstart.y < 0) ? 0 : ptstart.y; int result = movewindow(hchildwnd, ptstart.x, ptstart.y, width, height, false); } }