分享
 
 
 

另类Msgbox

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

写过VB的人都知道Msgbox函数弹出系统提示对话框,这个对话框既然是Windows给我们使用的那么我们就可以通过别的方式改变它。

下面我就会调用MessageBox的Api来改变VB的对话框函数,创造出我们自己风格的Msgbox!

该例程是将Msgbox弹出,并且总是位于窗口的中央;而且修改了Msgbox中的“确定”按钮上的文字。程序中简单的使用了Windows的钩子。

1·加入一个模块:

Option Explicit

'--------------------API声明部分--------------------

Private Const WH_CBT = 5

Private Const HCBT_ACTIVATE = 5

Private Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

'使用API的MessageBox替代VB系统的MsgBox

Private Declare Function MessageBox Lib "user32" _

Alias "MessageBoxA" _

(ByVal hWnd As Long, _

ByVal lpText As String, _

ByVal lpCaption As String, _

ByVal wType As Long) As Long

Private Declare Function SetWindowsHookEx Lib "user32" _

Alias "SetWindowsHookExA" _

(ByVal idHook As Long, _

ByVal lpfn As Long, _

ByVal hmod As Long, _

ByVal dwThreadId As Long) As Long

Private Declare Function UnhookWindowsHookEx Lib "user32" _

(ByVal hHook As Long) As Long

Private Declare Function MoveWindow Lib "user32" _

(ByVal hWnd As Long, _

ByVal X As Long, _

ByVal Y As Long, _

ByVal nWidth As Long, _

ByVal nHeight As Long, _

ByVal bRepaint As Long) As Long

Private Declare Function GetWindowRect Lib "user32" _

(ByVal hWnd As Long, _

lpRect As RECT) As Long

Public Declare Function GetDlgItem Lib "user32" _

(ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long

Private Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" _

(ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long

Private Declare Function GetDlgItemText Lib "user32" Alias "GetDlgItemTextA" _

(ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long

Private hHook As Long

Private Const IDOK = 1

Private Const IDCANCEL = 2

Private Const IDABORT = 3

Private Const IDRETRY = 4

Private Const IDIGNORE = 5

Private Const IDYES = 6

Private Const IDNO = 7

Private Const IDPROMPT = &HFFFF&

'----------------------窗体句柄----------------------'

Private hFormhWnd As Long

'''''''''''''''''''''''''''''''''''''''''''''''''''''

'替代VB中的Msgbox函数

'''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function Msgbox(hWnd As Long, sPrompt As String, _

Optional dwStyle As Long, _

Optional sTitle As String) As Long

Dim hInstance As Long

Dim hThreadId As Long

hInstance = App.hInstance

hThreadId = App.ThreadID

If dwStyle = 0 Then dwStyle = vbOKOnly

If Len(sTitle) = 0 Then sTitle = App.EXEName

'将当前窗口的句柄付给变量

hFormhWnd = hWnd

'设置钩子

hHook = SetWindowsHookEx(WH_CBT, _

AddressOf CBTProc, _

hInstance, hThreadId)

'调用MessageBox API

Msgbox = MessageBox(hWnd, sPrompt, sTitle, dwStyle)

End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''

'HOOK处理

'''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Function CBTProc(ByVal nCode As Long, _

ByVal wParam As Long, _

ByVal lParam As Long) As Long

'变量声明

Dim rc As RECT

Dim rcFrm As RECT

Dim newLeft As Long

Dim newTop As Long

Dim dlgWidth As Long

Dim dlgHeight As Long

Dim scrWidth As Long

Dim scrHeight As Long

Dim frmLeft As Long

Dim frmTop As Long

Dim frmWidth As Long

Dim frmHeight As Long

Dim hwndMsgBox As Long

' Dim lngHwnd As Long

'当MessageBox出现时,将Msgbox对话框居中与所在的窗口

If nCode = HCBT_ACTIVATE Then

'消息为HCBT_ACTIVATE时,参数wParam包含的是MessageBox的句柄

hwndMsgBox = wParam

'得到MessageBox对话框的Rect

Call GetWindowRect(hwndMsgBox, rc)

Call GetWindowRect(hFormhWnd, rcFrm)

'使MessageBox居中

frmLeft = rcFrm.Left

frmTop = rcFrm.Top

frmWidth = rcFrm.Right - rcFrm.Left

frmHeight = rcFrm.Bottom - rcFrm.Top

dlgWidth = rc.Right - rc.Left

dlgHeight = rc.Bottom - rc.Top

scrWidth = Screen.Width \ Screen.TwipsPerPixelX

scrHeight = Screen.Height \ Screen.TwipsPerPixelY

newLeft = frmLeft + ((frmWidth - dlgWidth) \ 2)

newTop = frmTop + ((frmHeight - dlgHeight) \ 2)

'修改确定按钮的文字

Call SetDlgItemText(hwndMsgBox, IDOK, "这是确定按钮")

'Msgbox居中

Call MoveWindow(hwndMsgBox, newLeft, newTop, dlgWidth, dlgHeight, True)

'卸载钩子

UnhookWindowsHookEx hHook

End If

CBTProc = False

End Function

2·窗体中的代码:

Form1中的-----

Option Explicit

Private Sub Command1_Click()

'变量声明

Dim strTitle As String

Dim strPrompt As String

Dim lngStyle As Long

'MessageBox的标题

strTitle = "我的应用"

'MessageBox的内容

strPrompt = "这是 hook MessageBox 的演示" & vbCrLf & vbCrLf & _

"MessageBox的对话框将会居中在Form中"

'MessageBox样式

lngStyle = vbAbortRetryIgnore Or vbInformation

Select Case Msgbox(hWnd, strPrompt, lngStyle, strTitle)

Case vbRetry: Text1.Text = "Retry button 按下"

Case vbAbort: Text1.Text = "Abort button 按下"

Case vbIgnore: Text1.Text = "Ignore button 按下"

End Select

End Sub

Private Sub Command2_Click()

Form2.Show

End Sub

Form2中的-----

Option Explicit

Private Sub Command1_Click()

Call Msgbox(Me.hWnd, "确定按钮展示!", 0, "")

End Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有