Monday, 15 September 2014

.net - Messagebox customization is not shown correctly -



.net - Messagebox customization is not shown correctly -

time ago i've taken this code of @hans passant create modifications, works fine i've noticed custom messagebox not shown properly.

this how shown normal messagebox custom windows theme:

and else how shown custom messagebox:

as can see darkened bottom layer in custom messagebox not alligned or resized or don't know.

how can prepare issue?, total code:

' [ centered messagebox ] ' ' author of original code hans passant: ' http://stackoverflow.com/questions/2576156/winforms-how-can-i-make-messagebox-appear-centered-on-mainform ' ' examples : ' ' using new centeredmessagebox(me, new font(new fontfamily("lucida console"), font.sizeinpoints, fontstyle.bold)) ' messagebox.show("test text", "test title", messageboxbuttons.ok, messageboxicon.information) ' end using #region " centered messagebox class" imports system.drawing imports system.runtime.interopservices imports system.text imports system.windows.forms class centeredmessagebox : implements idisposable private mtries integer = 0 private mowner form private mfont font ' p/invoke declarations private const wm_setfont integer = &h30 private const wm_getfont integer = &h31 private delegate function enumthreadwndproc(hwnd intptr, lp intptr) boolean friend declare function setwindowpos lib "user32" (byval hwnd intptr, byval hwndinsertafter intptr, byval x integer, byval y integer, byval cx integer, byval cy integer, byval wflags uint32) boolean <dllimport("user32.dll")> private shared function enumthreadwindows(tid integer, callback enumthreadwndproc, lp intptr) boolean end function <dllimport("kernel32.dll")> private shared function getcurrentthreadid() integer end function <dllimport("user32.dll")> private shared function getclassname(hwnd intptr, buffer stringbuilder, buflen integer) integer end function <dllimport("user32.dll")> private shared function getdlgitem(hwnd intptr, item integer) intptr end function <dllimport("user32.dll")> private shared function sendmessage(hwnd intptr, msg integer, wp intptr, lp intptr) intptr end function <dllimport("user32.dll")> shared function getwindowrect(hwnd intptr, byref rc rect) boolean end function <dllimport("user32.dll")> shared function movewindow(hwnd intptr, x integer, y integer, w integer, h integer, repaint boolean) boolean end function construction rect public left integer public top integer public right integer public bottom integer end construction public sub new(owner form, optional custom_font font = nothing) mowner = owner mfont = custom_font owner.begininvoke(new methodinvoker(addressof finddialog)) end sub private sub finddialog() ' enumerate windows find message box if mtries < 0 homecoming end if dim callback new enumthreadwndproc(addressof checkwindow) if enumthreadwindows(getcurrentthreadid(), callback, intptr.zero) if system.threading.interlocked.increment(mtries) < 10 mowner.begininvoke(new methodinvoker(addressof finddialog)) end if end if end sub private function checkwindow(hwnd intptr, lp intptr) boolean ' checks if <hwnd> dialog dim sb new stringbuilder(260) getclassname(hwnd, sb, sb.capacity) if sb.tostring() <> "#32770" homecoming true ' static command displays text dim htext intptr = getdlgitem(hwnd, &hffff) dim frmrect new rectangle(mowner.location, mowner.size) dim dlgrect rect getwindowrect(hwnd, dlgrect) if htext <> intptr.zero if mfont nil ' current font mfont = font.fromhfont(sendmessage(htext, wm_getfont, intptr.zero, intptr.zero)) end if sendmessage(htext, wm_setfont, mfont.tohfont(), new intptr(1)) ' resize , positionate messagebox window: movewindow(hwnd, _ frmrect.left + (frmrect.width - dlgrect.right + dlgrect.left) \ 2, _ frmrect.top + (frmrect.height - dlgrect.bottom + dlgrect.top) \ 2, _ (dlgrect.right - dlgrect.left), _ (dlgrect.bottom - dlgrect.top), true) ' resize messagebox text label if mfont isnot nil setwindowpos(htext, 0, 70, 35, 1920, 1080, 0) end if end if ' done homecoming false end function public sub dispose() implements idisposable.dispose mtries = -1 mowner = nil if mfont isnot nil mfont.dispose() end sub end class #end part

it looks code resize text label causing problem

' resize messagebox text label if mfont isnot nil setwindowpos(htext, 0, 70, 35, 1920, 1080, 0) end if

this code resizes text label big size , covers lower area. if alter height 1080 smaller, 30, problem disappears.

i able work removing if statement entirely. messagebox still centered in parent form , appears work correctly without it.

.net vb.net winforms winapi messagebox

No comments:

Post a Comment