问题描述
我使用的是其最初是由这里@Hans帕桑特定制MessageBox类:Winforms-How我可以让消息框出现在中心上的MainForm?
(我使用修改后的版本是在这个问题底部)
自定义消息框,可以接收自定义文本的字体来显示它。
这是一个使用示例:
使用新CenteredMessageBox(我,_
新的字体(新的FontFamily(龙力控制台),16,FontStyle.Bold))
的MessageBox.show(测试文本,测试题,MessageBoxButtons.YesNo,MessageBoxIcon.Question)
结束使用
的窗口,需要各方面的内容进行调整/手动位置,如果不是,该文本将取消显示冒出这样的:
这是结果。如果我用更大的字体(我已经调整了文本控件):
所以,我需要计算其位置和大小将具备的要素realatively字体大小,调整窗口的大小和positionate按钮,而这正是我所要求的。我该怎么办呢?我需要一个多只是公式事业数学我没有做很多很好。
我在考虑到这些因素:
- MessageBox的文本字体大小。
- MessageBox的静态控件包含并显示文本。
- MessageBox的按钮(S)(它可以是1个按钮,2个按钮,或3个按钮的组合,具有不同的默认位置)
- 在其中包含了所有这些元素在MessageBox窗口形式。
按钮(S)变量:
- 如果只是1布顿在消息框窗口(如:'好吧'),它会被神韵中等,但如果是3个按钮(例如:AbortRetryIgnore),那么第一个按钮将被神韵下来左侧角球,第二个按钮,中间和最后一个按钮的右侧角球,我觉得没有必要解释这导致所有知道如何MSGBOX显示其按钮。
我不是一个算术大师,所以请原谅我,如果我错过了一些必要的值,但我认为这些将是必要的值/在code变量一起工作:
昏暗Text_width为整数= 0
昏暗Text_height为整数= 0
昏暗Text_Container_width为整数= 0
昏暗Text_Container_height为整数= 0
不要计算信息框窗口的确切大小,
只是我认为只需要总结额外的大小设置为默认的消息框的矩形
昏暗Messagebox_window_extra_width为整数= 0
昏暗Messageboxwindow_extra_height为整数= 0
这再presents的第一个按钮,
在1,2,或3个按钮的消息框按钮组合,
可以显示该按钮在神韵底部中间(如独特的按钮),
'或可在左下角被神韵(作为第一的2 3的按键组合)。
昏暗Button_1_Pos_X为整数= 0
昏暗Button_1_Pos_Y为整数= 0
这个按钮重新presents第二个按钮
在2或3个按钮的消息框按钮组合,
在布敦可能存在或无法
'和可以显示在神韵底部中间(在3个按钮的中间),
可以显示该按钮在右下角神韵(如第二个和最后一个按钮)。
昏暗Button_2_Pos_X为整数= 0
昏暗Button_2_Pos_Y为整数= 0
这个按钮重新presents第三个按钮
2个或3个按钮的消息框按钮组合,
在布敦可能存在或无法
'和可以显示在右下方神韵(如第三和最后一个按钮)。
昏暗Button_3_Pos_X为整数= 0
昏暗Button_3_Pos_Y为整数= 0
这是什么,我可以做我自己,我一直在测试存储这样的消息框按钮元素:
昏暗button_Ok作为IntPtr的=函数GetDlgItem(HWND,1)确定按钮。
昏暗button_Cancel作为IntPtr的=函数GetDlgItem(HWND,2)的取消按钮。
昏暗button_Abort作为IntPtr的=函数GetDlgItem(HWND,3)的终止按钮。
昏暗button_Retry作为IntPtr的=函数GetDlgItem(HWND,4)的重试按钮。
昏暗button_Ignore作为IntPtr的=函数GetDlgItem(HWND,5)的忽略按钮。
昏暗button_Yes作为IntPtr的=函数GetDlgItem(HWND,6)是按钮。
昏暗button_No作为IntPtr的=函数GetDlgItem(HWND,7)的否按钮。
(他们总是有相同的项目指标,不管在MessageBox布敦组合)
要调整的消息框窗,我可以这样使用的MoveWindow API:
这是调整和positionate在MessageBox窗口
的MoveWindow(HWND,_
frmRect.Left +(frmRect.Width - dlgRect.Right + dlgRect.Left)\ 2,_
frmRect.Top +(frmRect.Height - dlgRect.Bottom + dlgRect.Top)\ 2,_
(dlgRect.Right - dlgRect.Left)+ Messagebox_window_extra_width,_
(dlgRect.Bottom - dlgRect.Top)+ Messagebox_window_extra_height,真)
和调整大小,其余元素(文本容器,按钮),调整的消息框的形式后,我就可以这样使用SetWindowPos API:
文本容器:
SetWindowPos(hText,0,70,30,1920,1080,0)的值是不完美的计算,但能正常工作
消息框按钮:
'SetWindowPos(button_Ok,0,0,0,0,0,0)
'SetWindowPos(button_Cancel,0,0,0,0,0,0)
'SetWindowPos(button_Abort,0,0,0,0,0,0)
'SetWindowPos(button_Retry,0,0,0,0,0,0)
'SetWindowPos(button_Ignore,0,0,0,0,0,0)
'SetWindowPos(button_Yes,0,0,0,0,0,0)
'SetWindowPos(button_No,0,0,0,0,0,0)
下面是搞砸了一切,我之前说过的事情:
'[居中的MessageBox]
原来的code作者是汉斯帕桑特: http://stackoverflow.com/questions/2576156/winforms-how-can-i-make-messagebox-appear-centered-on-mainform
例子:
使用新CenteredMessageBox(Me中,新字体(新的FontFamily(龙力控制台),Font.SizeInPoints,FontStyle.Bold))
'的MessageBox.show(测试文本,测试题,MessageBoxButtons.OK,MessageBoxIcon.Information)
结束使用
#REGION中心MessageBox类
进口System.Drawing中
进口了System.Runtime.InteropServices
进口System.Text
进口System.Windows.Forms的
类CenteredMessageBox:实现IDisposable
私人mTries为整数= 0
私人mOwner作为表
私人mFont由于字体
昏暗Text_width为整数= 0
昏暗Text_height为整数= 0
昏暗Text_Container_width为整数= 0
昏暗Text_Container_height为整数= 0
昏暗Messagebox_window_extra_width为整数= 0
昏暗Messagebox_window_extra_height为整数= 0
昏暗Button_1_Pos_X为整数= 0'确定按钮
昏暗Button_1_Pos_Y为整数= 0'确定按钮
昏暗Button_2_Pos_X为整数= 0'这个按钮就不可能存在
昏暗Button_2_Pos_Y为整数= 0'这个按钮就不可能存在
昏暗Button_3_Pos_X为整数= 0'这个按钮就不可能存在
昏暗Button_3_Pos_Y为整数= 0'这个按钮就不可能存在
'P / Invoke的声明
私人常量WM_SETFONT作为整数=安培; H30
私人常量WM_GETFONT作为整数=安培; H31
私人委托函数EnumThreadWndProc(HWND作为IntPtr的,LP作为IntPtr的)作为布尔
<的DllImport(user32.dll中)> _
私人共享功能EnumThreadWindows(TID为整数,回调作为EnumThreadWndProc,LP作为IntPtr的)作为布尔
端功能
<的DllImport(KERNEL32.DLL)> _
私人共享功能GetCurrentThreadId()作为整数
端功能
<的DllImport(user32.dll中)> _
私人共享功能GetClassName(HWND作为IntPtr的,缓冲区的StringBuilder,buflen为整数)作为整数
端功能
<的DllImport(user32.dll中)> _
私人共享功能函数GetDlgItem(HWND作为IntPtr的,项目为整数)作为IntPtr的
端功能
<的DllImport(user32.dll中)> _
私人共享SendMessage函数(HWND作为IntPtr的,味精作为整数,WP作为IntPtr的,LP作为IntPtr的)作为IntPtr的
端功能
<的DllImport(user32.dll中)> _
共享功能GetWindowRect(HWND作为IntPtr的,为ByRef RC作为RECT)作为布尔
端功能
<的DllImport(user32.dll中)> _
共享功能的MoveWindow(HWND作为IntPtr的,X作为整数,Y为整数,W为整数,H为整数,重绘由于布尔)作为布尔
端功能
结构RECT
公共左为整数
公共顶部整数
公权作为整数
公共底部整数
末端结构
朋友声明函数SetWindowPos库USER32(BYVAL HWND作为IntPtr的,BYVAL hWndInsertAfter作为IntPtr的,BYVAL x As中整数,BYVAL y为整数,BYVAL CX作为整数,BYVAL CY作为整数,BYVAL wFlags作为UInt32的)作为布尔
公共子新(所有者格式,可选Custom_Font由于字体=无)
mOwner =所有者
mFont = Custom_Font
owner.BeginInvoke(新MethodInvoker(AddressOf FindDialog的))
结束小组
私人小组FindDialog的()
枚举窗口找到该消息框
如果mTries< 0然后
返回
结束如果
昏暗的回调作为新EnumThreadWndProc(AddressOf checkWindow)
如果EnumThreadWindows(GetCurrentThreadId(),回调,IntPtr.Zero)然后
如果System.Threading.Interlocked.Increment(mTries)< 10然后
mOwner.BeginInvoke(新MethodInvoker(AddressOf FindDialog的))
结束如果
结束如果
结束小组
专用功能checkWindow(HWND作为IntPtr的,LP作为IntPtr的)作为布尔
检查是否<的hWnd>是一个对话
昏暗的SB作为新的StringBuilder(260)
GetClassName(HWND,SB,sb.Capacity)
如果sb.ToString()&其中;> #32770返回true
心动不如行动,让静态控件中显示文本
昏暗hText作为IntPtr的=函数GetDlgItem(HWND,和放大器; HFFFF)
获取消息框按钮元素
昏暗button_Ok作为IntPtr的=函数GetDlgItem(HWND,1)确定按钮。
昏暗button_Cancel作为IntPtr的=函数GetDlgItem(HWND,2)的取消按钮。
昏暗button_Abort作为IntPtr的=函数GetDlgItem(HWND,3)的终止按钮。
昏暗button_Retry作为IntPtr的=函数GetDlgItem(HWND,4)的重试按钮。
昏暗button_Ignore作为IntPtr的=函数GetDlgItem(HWND,5)的忽略按钮。
昏暗button_Yes作为IntPtr的=函数GetDlgItem(HWND,6)是按钮。
昏暗button_No作为IntPtr的=函数GetDlgItem(HWND,7)的否按钮。
昏暗frmRect作为新的矩形(mOwner.Location,mOwner.Size)
昏暗dlgRect作为RECT
GetWindowRect(HWND,dlgRect)
如果hText<> IntPtr.Zero然后
如果mFont是没有那么
获取当前字体
mFont = Font.FromHfont(SendMessage函数(hText,WM_GETFONT,IntPtr.Zero,IntPtr.Zero))
结束如果
SendMessage消息(hText,WM_SETFONT,mFont.ToHfont(),新的IntPtr(1))
就在这里是一个空的空间,我可以测试一些操作:
'Messagebox_window_extra_width =(mFont.Height \ mFont.Size)+(dlgRect.Right)
Messagebox_window_extra_height = mFont.Height +
这是调整和positionate在MessageBox窗口:
的MoveWindow(HWND,_
frmRect.Left +(frmRect.Width - dlgRect.Right + dlgRect.Left)\ 2,_
frmRect.Top +(frmRect.Height - dlgRect.Bottom + dlgRect.Top)\ 2,_
(dlgRect.Right - dlgRect.Left)+ Messagebox_window_extra_width,_
(dlgRect.Bottom - dlgRect.Top)+ Messagebox_window_extra_height,真)
这是调整和positionate其余的元素:
文本容器:
SetWindowPos(hText,0,70,30,1920,1080,0)
消息框按钮:
'SetWindowPos(button_Ok,0,0,0,0,0,0)
'SetWindowPos(button_Cancel,0,0,0,0,0,0)
'SetWindowPos(button_Abort,0,0,0,0,0,0)
'SetWindowPos(button_Retry,0,0,0,0,0,0)
'SetWindowPos(button_Ignore,0,0,0,0,0,0)
'SetWindowPos(button_Yes,0,0,0,0,0,0)
'SetWindowPos(button_No,0,0,0,0,0,0)
结束如果
完成
返回False
端功能
公用Sub Dispose()方法实现IDisposable.Dispose
mTries = -1
mOwner =无
如果mFont状态并没有任何然后mFont.Dispose()
结束小组
末级
#END地区
这是我得到尝试使用@Grahamvs解决方案:
使用新CenteredMessageBox(Me中,新字体(新的FontFamily(龙力控制台),Font.SizeInPoints,FontStyle.Bold))
的MessageBox.show(测试文本,测试题,MessageBoxButtons.OK,MessageBoxIcon.Information)
结束使用
这为我工作:
我改了:
<的DllImport(user32.dll中)> _
私人共享SendMessage函数(BYVAL的HWND作为IntPtr的,BYVAL味精作为整数,BYVAL WP作为IntPtr的,BYVAL lp的作为IntPtr的)作为IntPtr的
端功能
到
自动声明SendMessage函数库user32.dll中(BYVAL的HWND作为IntPtr的,BYVAL味精作为整数,BYVAL wParam中的IntPtr,BYVAL lParam中的IntPtr)作为IntPtr的
和添加(后的SendMessage(hText,WM_SETFONT,mFont.ToHfont(),新的IntPtr(1))
)
获取文本
昏暗WM_GETTEXT作为整数=安培;高清
的Alloc内存为临危文本缓冲区
昏暗HNDL作为IntPtr的= Marshal.AllocHGlobal(200)
发送WM_GETTEXT消息
昏暗NumText作为整数= SendMessage函数(hText,WM_GETTEXT,200,HNDL)
从非托管内存复制字符到托管字符串
昏暗MSGTEXT作为字符串= Marshal.PtrToStringUni(HNDL)
测量的文本
昏暗TEXTSIZE作为的SizeF
将G作为图形= mOwner.CreateGraphics
TEXTSIZE = G.MeasureString(MSGTEXT&安培;MMM,mFont)
结束使用
和改变的MoveWindow
来:
的MoveWindow(HWND,frmRect.Left +(frmRect.Width - dlgRect.Right + dlgRect.Left)\ 2,frmRect.Top +(frmRect.Height - dlgRect.Bottom + dlgRect .TOP)\ 2,CINT(TextSize.Width)+ Messagebox_window_extra_width,(dlgRect.Bottom - dlgRect.Top)+ Messagebox_window_extra_height,真)
希望这有助于
编辑:这里是全code
[居中的MessageBox]的原始code的作者是汉斯帕桑特:Winforms-How我可以让消息框出现在中心上的MainForm?
的例子:
使用新CenteredMessageBox(Me中,新字体(新的FontFamily(龙力控制台),Font.SizeInPoints,FontStyle.Bold))
的MessageBox.show(测试文本,测试题,MessageBoxButtons.OK,MessageBoxIcon.Information)
结束使用
区域中心MessageBox类
进口System.Drawing中
进口了System.Runtime.InteropServices
进口System.Text
进口System.Windows.Forms的
类CenteredMessageBox:实现IDisposable
私人mTries为整数= 0 私人mOwner作为表 私人mFont由于字体 昏暗Text_width为整数= 0 昏暗Text_height为整数= 0 昏暗Text_Container_width为整数= 0 昏暗Text_Container_height为整数= 0 昏暗Messagebox_window_extra_width为整数= 0 昏暗Messagebox_window_extra_height为整数= 0 昏暗Button_1_Pos_X为整数= 0'确定按钮 昏暗Button_1_Pos_Y为整数= 0'确定按钮 昏暗Button_2_Pos_X为整数= 0'这个按钮就不可能存在 昏暗Button_2_Pos_Y为整数= 0'这个按钮就不可能存在 昏暗Button_3_Pos_X为整数= 0'这个按钮就不可能存在 昏暗Button_3_Pos_Y为整数= 0'这个按钮就不可能存在 'P / Invoke的声明 私人常量WM_SETFONT作为整数=安培; H30 私人常量WM_GETFONT作为整数=安培; H31 私人委托函数EnumThreadWndProc(BYVAL的HWND作为IntPtr的,BYVAL lp的作为IntPtr的)作为布尔 <的DllImport(user32.dll中)> _ 私人共享功能EnumThreadWindows(BYVAL TID为整数,BYVAL回调作为EnumThreadWndProc,BYVAL LP作为IntPtr的)作为布尔 端功能 <的DllImport(KERNEL32.DLL)> _ 私人共享功能GetCurrentThreadId()作为整数 端功能 <的DllImport(user32.dll中)> _ 私人共享功能GetClassName(BYVAL的HWND作为IntPtr的,BYVAL缓冲区StringBuilder的,BYVAL buflen为整数)作为整数 端功能 <的DllImport(user32.dll中)> _ 私人共享功能函数GetDlgItem(BYVAL的HWND作为IntPtr的,BYVAL项目作为整数)作为IntPtr的 端功能 '<的DllImport(user32.dll中)> _ 私人共享SendMessage函数(BYVAL的HWND作为IntPtr的,BYVAL味精作为整数,BYVAL WP作为IntPtr的,BYVAL lp的作为IntPtr的)作为IntPtr的 端功能 <的DllImport(user32.dll中)> _ 共享功能GetWindowRect(BYVAL的HWND作为IntPtr的,为ByRef RC作为RECT)作为布尔 端功能 <的DllImport(user32.dll中)> _ 共享功能的MoveWindow(BYVAL的HWND作为IntPtr的,BYVAL x As中整数,BYVAL y为整数,BYVALW¯¯作为整数,BYVAL事项h作为整数,BYVAL重绘由于布尔)作为布尔 端功能 声明自动SendMessage函数库user32.dll中(BYVAL的HWND作为IntPtr的,BYVAL味精作为整数,BYVAL wParam中的IntPtr,BYVAL lParam中的IntPtr)作为IntPtr的 结构RECT 公共左为整数 公共顶部整数 公权作为整数 公共底部整数 末端结构 朋友声明函数SetWindowPos库USER32(BYVAL HWND作为IntPtr的,BYVAL hWndInsertAfter作为IntPtr的,BYVAL x As中整数,BYVAL y为整数,BYVAL CX作为整数,BYVAL CY作为整数,BYVAL wFlags作为UInt32的)作为布尔 公共子新(BYVAL所有者的形式,可选BYVAL Custom_Font由于字体=无) mOwner =所有者 mFont = Custom_Font owner.BeginInvoke(新MethodInvoker(AddressOf FindDialog的)) 结束小组 私人小组FindDialog的() 枚举窗口找到该消息框 如果mTries< 0然后 返回 结束如果 昏暗的回调作为新EnumThreadWndProc(AddressOf checkWindow) 如果EnumThreadWindows(GetCurrentThreadId(),回调,IntPtr.Zero)然后 如果System.Threading.Interlocked.Increment(mTries)< 10然后 mOwner.BeginInvoke(新MethodInvoker(AddressOf FindDialog的)) 结束如果 结束如果 结束小组 专用功能checkWindow(BYVAL的HWND作为IntPtr的,BYVAL lp的作为IntPtr的)作为布尔 检查是否<的hWnd>是一个对话 昏暗的SB作为新的StringBuilder(260) GetClassName(HWND,SB,sb.Capacity) 如果sb.ToString()&其中;> #32770返回true 心动不如行动,让静态控件中显示文本 昏暗hText作为IntPtr的=函数GetDlgItem(HWND,和放大器; HFFFF) 获取消息框按钮元素 昏暗button_Ok作为IntPtr的=函数GetDlgItem(HWND,1)确定按钮。 昏暗button_Cancel作为IntPtr的=函数GetDlgItem(HWND,2)的取消按钮。 昏暗button_Abort作为IntPtr的=函数GetDlgItem(HWND,3)的终止按钮。 昏暗button_Retry作为IntPtr的=函数GetDlgItem(HWND,4)的重试按钮。 昏暗button_Ignore作为IntPtr的=函数GetDlgItem(HWND,5)的忽略按钮。 昏暗button_Yes作为IntPtr的=函数GetDlgItem(HWND,6)是按钮。 昏暗button_No作为IntPtr的=函数GetDlgItem(HWND,7)的否按钮。 昏暗frmRect作为新的矩形(mOwner.Location,mOwner.Size) 昏暗dlgRect作为RECT GetWindowRect(HWND,dlgRect) 如果hText<> IntPtr.Zero然后 如果mFont是没有那么 获取当前字体 mFont = Font.FromHfont(SendMessage函数(hText,WM_GETFONT,IntPtr.Zero,IntPtr.Zero)) 结束如果 SendMessage消息(hText,WM_SETFONT,mFont.ToHfont(),新的IntPtr(1)) 获取文本 昏暗WM_GETTEXT作为整数=安培;高清 的Alloc内存为临危文本缓冲区 昏暗HNDL作为IntPtr的= Marshal.AllocHGlobal(200) 发送WM_GETTEXT消息 昏暗NumText作为整数= SendMessage函数(hText,WM_GETTEXT,200,HNDL) 从非托管内存复制字符到托管字符串 昏暗MSGTEXT作为字符串= Marshal.PtrToStringUni(HNDL) 测量的文本 昏暗TEXTSIZE作为的SizeF 将G作为图形= mOwner.CreateGraphics TEXTSIZE = G.MeasureString(MSGTEXT&安培;MMM,mFont) 结束使用 就在这里是一个空的空间,我可以测试一些操作: 'Messagebox_window_extra_width =(mFont.Height \ mFont.Size)+(dlgRect.Right) Messagebox_window_extra_height = mFont.Height + 这是调整和positionate在MessageBox窗口: 的MoveWindow(HWND,frmRect.Left +(frmRect.Width - dlgRect.Right + dlgRect.Left)\ 2,frmRect.Top +(frmRect.Height - dlgRect.Bottom + dlgRect.Top)\ 2,_ (dlgRect.Right - dlgRect.Left)+ Messagebox_window_extra_width,(dlgRect.Bottom - dlgRect.Top)+ Messagebox_window_extra_height,真) 的MoveWindow(HWND,frmRect.Left +(frmRect.Width - dlgRect.Right + dlgRect.Left)\ 2,frmRect.Top +(frmRect.Height - dlgRect.Bottom + dlgRect.Top)\ 2,Math.Max(CINT( TextSize.Width),200)+ Messagebox_window_extra_width,(dlgRect.Bottom - dlgRect.Top)+ Messagebox_window_extra_height,真) 这是调整和positionate其余的元素: 文本容器: SetWindowPos(hText,0,70,30,1920,1080,0) 消息框按钮: 'SetWindowPos(button_Ok,0,0,0,0,0,0) 'SetWindowPos(button_Cancel,0,0,0,0,0,0) 'SetWindowPos(button_Abort,0,0,0,0,0,0) 'SetWindowPos(button_Retry,0,0,0,0,0,0) 'SetWindowPos(button_Ignore,0,0,0,0,0,0) 'SetWindowPos(button_Yes,0,0,0,0,0,0) 'SetWindowPos(button_No,0,0,0,0,0,0) 结束如果 完成 返回False 端功能 公用Sub Dispose()方法实现IDisposable.Dispose mTries = -1 mOwner =无 如果mFont状态并没有任何然后mFont.Dispose() 结束小组
末级
#END地区
I'm using a custom messagebox class which was originally written by @Hans Passant here: Winforms-How can I make MessageBox appear centered on MainForm?
(The modified version that I'm using is at bottom of this question)
The custom messagebox can receive a custom text font to display it.
This is an usage example:
Using New CenteredMessageBox(Me, _
New Font(New FontFamily("Lucida Console"), 16, FontStyle.Bold))
MessageBox.Show("Test Text", "Test Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
End Using
All the contents of the window need to be resized/positioned manually, if not, the text will de displayed cropped like this:
And this is the result If I use bigger fonts (I've resized the text control):
So I need to calculate which position and size would have the elements realatively to the font size, to resize the window and to positionate the buttons, and that is what I'm asking for. How I can do it? I would need more than a just formula 'cause mathematics i do not do much well.
I have these elements in mind:
- The Messagebox text-font size.
- The Messagebox static control that contains and displays the text.
- The Messagebox button(s) (which can be a combination of 1 button, 2 buttons, or 3 buttons, with different default positions)
- The Messagebox Window Form which contains all these elements.
Button(s) variables:
- If only is 1 buton in messagebox window (ex: 'Ok') it will be alligned to middle, but if they are 3 buttons (ex: 'AbortRetryIgnore') then the first button will be alligned to down left corner, the second button to middle and the last button to the right corner, I think there is no need to explain this 'cause all knows how the msgbox displays their buttons.
I'm not an arithmetic guru, so forgive me if I miss some necessary values, but I think that these would be the necessary values/variables in the code to work with:
Dim Text_width As Integer = 0
Dim Text_height As Integer = 0
Dim Text_Container_width As Integer = 0
Dim Text_Container_height As Integer = 0
' Don't calculate the exact size of messagebox window,
' just I think only needs to sum an extra size to a default messagebox rectangle
Dim Messagebox_window_extra_width As Integer = 0
Dim Messageboxwindow_extra_height As Integer = 0
' This represents the first button,
' in a messagebox button combination of 1, 2, or 3 buttons,
' this button could be displayed alligned at bottom-middle (as unique button),
' or could be alligned at bottom-left (as the first of 2 of 3 button combination).
Dim Button_1_Pos_X As Integer = 0
Dim Button_1_Pos_Y As Integer = 0
' This button represents the second button
' in a messagebox button combination of 2 or 3 buttons,
' the buton could exist or could not
' and could be displayed alligned at bottom-middle (in the middle of 3 buttons),
' this button could be displayed alligned at bottom-right (as second and last button).
Dim Button_2_Pos_X As Integer = 0
Dim Button_2_Pos_Y As Integer = 0
' This button represents the third button
' a messagebox button combination of 2 or 3 buttons,
' the buton could exist or could not
' and could be displayed alligned at bottom-right (as third and last button).
Dim Button_3_Pos_X As Integer = 0
Dim Button_3_Pos_Y As Integer = 0
This is what I could did by myself, I've been testing to store the messagebox button elements in this way:
Dim button_Ok As IntPtr = GetDlgItem(hWnd, 1) ' The 'Ok' button.
Dim button_Cancel As IntPtr = GetDlgItem(hWnd, 2) ' the 'Cancel' button.
Dim button_Abort As IntPtr = GetDlgItem(hWnd, 3) ' the 'Abort' button.
Dim button_Retry As IntPtr = GetDlgItem(hWnd, 4) ' the 'Retry' button.
Dim button_Ignore As IntPtr = GetDlgItem(hWnd, 5) ' the 'Ignore' button.
Dim button_Yes As IntPtr = GetDlgItem(hWnd, 6) ' the 'Yes' button.
Dim button_No As IntPtr = GetDlgItem(hWnd, 7) ' the 'NO' button.
(Their always have the same Item index no matter the messagebox buton combination)
To resize the messagebox window I can use the MoveWindow API like this:
' This is to resize and positionate the 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) + Messagebox_window_extra_width, _
(dlgRect.Bottom - dlgRect.Top) + Messagebox_window_extra_height, True)
And to resize the rest elements (text container, buttons), after resizing the messagebox form then I can use the SetWindowPos API like this:
' Text container:
SetWindowPos(hText, 0, 70, 30, 1920, 1080, 0) ' Values are not perfect calculated but works fine
' Messagebox buttons:
' SetWindowPos(button_Ok, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Cancel, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Abort, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Retry, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Ignore, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Yes, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_No, 0, 0, 0, 0, 0, 0)
Here is messed up all the things that I've mentioned before:
' [ Centered MessageBox ]
'
' The author of the original code is 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 As Integer = 0
Private mOwner As Form
Private mFont As Font
Dim Text_width As Integer = 0
Dim Text_height As Integer = 0
Dim Text_Container_width As Integer = 0
Dim Text_Container_height As Integer = 0
Dim Messagebox_window_extra_width As Integer = 0
Dim Messagebox_window_extra_height As Integer = 0
Dim Button_1_Pos_X As Integer = 0 ' "OK" Button
Dim Button_1_Pos_Y As Integer = 0 ' "OK" Button
Dim Button_2_Pos_X As Integer = 0 ' This button could not exist
Dim Button_2_Pos_Y As Integer = 0 ' This button could not exist
Dim Button_3_Pos_X As Integer = 0 ' This button could not exist
Dim Button_3_Pos_Y As Integer = 0 ' This button could not exist
' P/Invoke declarations
Private Const WM_SETFONT As Integer = &H30
Private Const WM_GETFONT As Integer = &H31
Private Delegate Function EnumThreadWndProc(hWnd As IntPtr, lp As IntPtr) As Boolean
<DllImport("user32.dll")> _
Private Shared Function EnumThreadWindows(tid As Integer, callback As EnumThreadWndProc, lp As IntPtr) As Boolean
End Function
<DllImport("kernel32.dll")> _
Private Shared Function GetCurrentThreadId() As Integer
End Function
<DllImport("user32.dll")> _
Private Shared Function GetClassName(hWnd As IntPtr, buffer As StringBuilder, buflen As Integer) As Integer
End Function
<DllImport("user32.dll")> _
Private Shared Function GetDlgItem(hWnd As IntPtr, item As Integer) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wp As IntPtr, lp As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Shared Function GetWindowRect(hWnd As IntPtr, ByRef rc As RECT) As Boolean
End Function
<DllImport("user32.dll")> _
Shared Function MoveWindow(hWnd As IntPtr, x As Integer, y As Integer, w As Integer, h As Integer, repaint As Boolean) As Boolean
End Function
Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
Friend Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As UInt32) As Boolean
Public Sub New(owner As Form, Optional Custom_Font As Font = Nothing)
mOwner = owner
mFont = Custom_Font
owner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
End Sub
Private Sub findDialog()
' Enumerate windows to find the message box
If mTries < 0 Then
Return
End If
Dim callback As New EnumThreadWndProc(AddressOf checkWindow)
If EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero) Then
If System.Threading.Interlocked.Increment(mTries) < 10 Then
mOwner.BeginInvoke(New MethodInvoker(AddressOf findDialog))
End If
End If
End Sub
Private Function checkWindow(hWnd As IntPtr, lp As IntPtr) As Boolean
' Checks if <hWnd> is a dialog
Dim sb As New StringBuilder(260)
GetClassName(hWnd, sb, sb.Capacity)
If sb.ToString() <> "#32770" Then Return True
' Got it, get the STATIC control that displays the text
Dim hText As IntPtr = GetDlgItem(hWnd, &HFFFF)
' Get the messagebox button elements
Dim button_Ok As IntPtr = GetDlgItem(hWnd, 1) ' The 'Ok' button.
Dim button_Cancel As IntPtr = GetDlgItem(hWnd, 2) ' the 'Cancel' button.
Dim button_Abort As IntPtr = GetDlgItem(hWnd, 3) ' the 'Abort' button.
Dim button_Retry As IntPtr = GetDlgItem(hWnd, 4) ' the 'Retry' button.
Dim button_Ignore As IntPtr = GetDlgItem(hWnd, 5) ' the 'Ignore' button.
Dim button_Yes As IntPtr = GetDlgItem(hWnd, 6) ' the 'Yes' button.
Dim button_No As IntPtr = GetDlgItem(hWnd, 7) ' the 'NO' button.
Dim frmRect As New Rectangle(mOwner.Location, mOwner.Size)
Dim dlgRect As RECT
GetWindowRect(hWnd, dlgRect)
If hText <> IntPtr.Zero Then
If mFont Is Nothing Then
' Get the current font
mFont = Font.FromHfont(SendMessage(hText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero))
End If
SendMessage(hText, WM_SETFONT, mFont.ToHfont(), New IntPtr(1))
' Just here is an empty space where I can test some operations:
'
' Messagebox_window_extra_width = (mFont.Height \ mFont.Size) + (dlgRect.Right)
' Messagebox_window_extra_height = mFont.Height +
' This is to resize and positionate the 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) + Messagebox_window_extra_width, _
(dlgRect.Bottom - dlgRect.Top) + Messagebox_window_extra_height, True)
' And this is to resize and positionate the rest elements:
'
' Text container:
SetWindowPos(hText, 0, 70, 30, 1920, 1080, 0)
'
' Messagebox buttons:
' SetWindowPos(button_Ok, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Cancel, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Abort, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Retry, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Ignore, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_Yes, 0, 0, 0, 0, 0, 0)
' SetWindowPos(button_No, 0, 0, 0, 0, 0, 0)
End If
' Done
Return False
End Function
Public Sub Dispose() Implements IDisposable.Dispose
mTries = -1
mOwner = Nothing
If mFont IsNot Nothing Then mFont.Dispose()
End Sub
End Class
#End Region
This is what I get trying to use @Grahamvs solution:
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
This worked for me:
I changed:
<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
End Function
to
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
and add (after SendMessage(hText, WM_SETFONT, mFont.ToHfont(), New IntPtr(1))
)
' Get text
Dim WM_GETTEXT As Integer = &HD
' Alloc memory for the buffer that recieves the text
Dim Hndl As IntPtr = Marshal.AllocHGlobal(200)
' Send The WM_GETTEXT Message
Dim NumText As Integer = SendMessage(hText, WM_GETTEXT, 200, Hndl)
' Copy the characters from the unmanaged memory to a managed string
Dim MSGText As String = Marshal.PtrToStringUni(Hndl)
' Measure the text
Dim TextSize As SizeF
Using G As Graphics = mOwner.CreateGraphics
TextSize = G.MeasureString(MSGText & "MMM", mFont)
End Using
and changed MoveWindow
to:
MoveWindow(hWnd, frmRect.Left + (frmRect.Width - dlgRect.Right + dlgRect.Left) \ 2, frmRect.Top + (frmRect.Height - dlgRect.Bottom + dlgRect.Top) \ 2, CInt(TextSize.Width) + Messagebox_window_extra_width, (dlgRect.Bottom - dlgRect.Top) + Messagebox_window_extra_height, True)
Hope this helps
Edit: Here is the full code
' [ Centered MessageBox ]'
' The author of the original code is Hans Passant: 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 As Integer = 0 Private mOwner As Form Private mFont As Font Dim Text_width As Integer = 0 Dim Text_height As Integer = 0 Dim Text_Container_width As Integer = 0 Dim Text_Container_height As Integer = 0 Dim Messagebox_window_extra_width As Integer = 0 Dim Messagebox_window_extra_height As Integer = 0 Dim Button_1_Pos_X As Integer = 0 ' "OK" Button Dim Button_1_Pos_Y As Integer = 0 ' "OK" Button Dim Button_2_Pos_X As Integer = 0 ' This button could not exist Dim Button_2_Pos_Y As Integer = 0 ' This button could not exist Dim Button_3_Pos_X As Integer = 0 ' This button could not exist Dim Button_3_Pos_Y As Integer = 0 ' This button could not exist ' P/Invoke declarations Private Const WM_SETFONT As Integer = &H30 Private Const WM_GETFONT As Integer = &H31 Private Delegate Function EnumThreadWndProc(ByVal hWnd As IntPtr, ByVal lp As IntPtr) As Boolean <DllImport("user32.dll")> _ Private Shared Function EnumThreadWindows(ByVal tid As Integer, ByVal callback As EnumThreadWndProc, ByVal lp As IntPtr) As Boolean End Function <DllImport("kernel32.dll")> _ Private Shared Function GetCurrentThreadId() As Integer End Function <DllImport("user32.dll")> _ Private Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal buffer As StringBuilder, ByVal buflen As Integer) As Integer End Function <DllImport("user32.dll")> _ Private Shared Function GetDlgItem(ByVal hWnd As IntPtr, ByVal item As Integer) As IntPtr End Function '<DllImport("user32.dll")> _ 'Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr 'End Function <DllImport("user32.dll")> _ Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef rc As RECT) As Boolean End Function <DllImport("user32.dll")> _ Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal repaint As Boolean) As Boolean End Function Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure Friend Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As UInt32) As Boolean Public Sub New(ByVal owner As Form, Optional ByVal Custom_Font As Font = Nothing) mOwner = owner mFont = Custom_Font owner.BeginInvoke(New MethodInvoker(AddressOf findDialog)) End Sub Private Sub findDialog() ' Enumerate windows to find the message box If mTries < 0 Then Return End If Dim callback As New EnumThreadWndProc(AddressOf checkWindow) If EnumThreadWindows(GetCurrentThreadId(), callback, IntPtr.Zero) Then If System.Threading.Interlocked.Increment(mTries) < 10 Then mOwner.BeginInvoke(New MethodInvoker(AddressOf findDialog)) End If End If End Sub Private Function checkWindow(ByVal hWnd As IntPtr, ByVal lp As IntPtr) As Boolean ' Checks if <hWnd> is a dialog Dim sb As New StringBuilder(260) GetClassName(hWnd, sb, sb.Capacity) If sb.ToString() <> "#32770" Then Return True ' Got it, get the STATIC control that displays the text Dim hText As IntPtr = GetDlgItem(hWnd, &HFFFF) ' Get the messagebox button elements Dim button_Ok As IntPtr = GetDlgItem(hWnd, 1) ' The 'Ok' button. Dim button_Cancel As IntPtr = GetDlgItem(hWnd, 2) ' the 'Cancel' button. Dim button_Abort As IntPtr = GetDlgItem(hWnd, 3) ' the 'Abort' button. Dim button_Retry As IntPtr = GetDlgItem(hWnd, 4) ' the 'Retry' button. Dim button_Ignore As IntPtr = GetDlgItem(hWnd, 5) ' the 'Ignore' button. Dim button_Yes As IntPtr = GetDlgItem(hWnd, 6) ' the 'Yes' button. Dim button_No As IntPtr = GetDlgItem(hWnd, 7) ' the 'NO' button. Dim frmRect As New Rectangle(mOwner.Location, mOwner.Size) Dim dlgRect As RECT GetWindowRect(hWnd, dlgRect) If hText <> IntPtr.Zero Then If mFont Is Nothing Then ' Get the current font mFont = Font.FromHfont(SendMessage(hText, WM_GETFONT, IntPtr.Zero, IntPtr.Zero)) End If SendMessage(hText, WM_SETFONT, mFont.ToHfont(), New IntPtr(1)) ' Get text Dim WM_GETTEXT As Integer = &HD ' Alloc memory for the buffer that recieves the text Dim Hndl As IntPtr = Marshal.AllocHGlobal(200) ' Send The WM_GETTEXT Message Dim NumText As Integer = SendMessage(hText, WM_GETTEXT, 200, Hndl) ' Copy the characters from the unmanaged memory to a managed string Dim MSGText As String = Marshal.PtrToStringUni(Hndl) ' Measure the text Dim TextSize As SizeF Using G As Graphics = mOwner.CreateGraphics TextSize = G.MeasureString(MSGText & "MMM", mFont) End Using ' Just here is an empty space where I can test some operations: ' ' Messagebox_window_extra_width = (mFont.Height \ mFont.Size) + (dlgRect.Right) ' Messagebox_window_extra_height = mFont.Height + ' This is to resize and positionate the 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) + Messagebox_window_extra_width, (dlgRect.Bottom - dlgRect.Top) + Messagebox_window_extra_height, True) MoveWindow(hWnd, frmRect.Left + (frmRect.Width - dlgRect.Right + dlgRect.Left) \ 2, frmRect.Top + (frmRect.Height - dlgRect.Bottom + dlgRect.Top) \ 2, Math.Max(CInt(TextSize.Width), 200) + Messagebox_window_extra_width, (dlgRect.Bottom - dlgRect.Top) + Messagebox_window_extra_height, True) ' And this is to resize and positionate the rest elements: ' ' Text container: SetWindowPos(hText, 0, 70, 30, 1920, 1080, 0) ' ' Messagebox buttons: ' SetWindowPos(button_Ok, 0, 0, 0, 0, 0, 0) ' SetWindowPos(button_Cancel, 0, 0, 0, 0, 0, 0) ' SetWindowPos(button_Abort, 0, 0, 0, 0, 0, 0) ' SetWindowPos(button_Retry, 0, 0, 0, 0, 0, 0) ' SetWindowPos(button_Ignore, 0, 0, 0, 0, 0, 0) ' SetWindowPos(button_Yes, 0, 0, 0, 0, 0, 0) ' SetWindowPos(button_No, 0, 0, 0, 0, 0, 0) End If ' Done Return False End Function Public Sub Dispose() Implements IDisposable.Dispose mTries = -1 mOwner = Nothing If mFont IsNot Nothing Then mFont.Dispose() End Sub
End Class
#End Region
这篇关于这是数学公式来计算这个窗口的大小和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!