欢迎光临:
非常感谢您光临枕善居。本站是一个免费的基于VB,VB.NET源代码交流的平台,为大家提供优质的专业的源代码,如果您有需要,本站可以帮助在业余时间里给您寻找代码。当然,如果您有好的代码也可以在本站发布,共享给大家。
专业VB和.NET源码、编程开发教程、图标资源、USB电脑遥控器、智能家电控制开关....更多东东请进入我的淘宝小店--->
VB及.NET新源码2011(3DVD,控件+资源)
智能多路控制(串口编程开关) 带源码!
05-03
08
改变Messagebox按钮的文字.
作者:枕善居主 / 查看次数: 9834 / 评论: 4
'********************************************
'这是添加到模块的代码
'********************************************
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&, ByVal uElapse&, ByVal lpTimerFunc&)
Private Declare Function KillTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&)
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SetWindowText& Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String)
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const NV_MSGBOX As Long = &H5000&
Public sOKText As String, sCancelText As String, sTitle As String
'以下过程和函数
Public Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)
Dim h As Long, hMsg As Long
hMsg = FindWindow("#32770", sTitle)
If hMsg Then
KillTimer hwnd, idEvent
h = GetWindow(hMsg, GW_CHILD)
Do
If GetWndClass(h) = "Button" Then
If GetWndText(h) = "确定" Then SetWindowText h, sOKText
If GetWndText(h) = "取消" Then SetWindowText h, sCancelText
End If
h = GetWindow(h, GW_HWNDNEXT)
Loop While h <> 0
End If
End Sub
Private Function GetWndClass(hwnd As Long) As String
Dim k As Long, sName As String
sName = Space$(128)
k = GetClassName(hwnd, sName, 128)
If k > 0 Then sName = Left$(sName, k) Else sName = "无类"
GetWndClass = sName
End Function
Private Function GetWndText(hwnd As Long) As String
Dim k As Long, sName As String
sName = Space$(128)
k = GetWindowText(hwnd, sName, 128)
If k > 0 Then sName = Left$(sName, k) Else sName = "无标题"
GetWndText = sName
End Function
'********************************************
'这是添加到窗体中的示例代码
'********************************************
Dim WhatDidIchoose As Long
sTitle = "Windows"
sOKText = "我点击"
sCancelText = "我闪t"
SetTimer hwnd, NV_MSGBOX, 10, AddressOf TimerProc
WhatDidIchoose = MsgBox("这是一个对话框示例程序", vbOKCancel, sTitle)
Select Case WhatDidIchoose
Case vbOK
'action here
Debug.Print "改为 : " & sOKText
Case vbCancel
'action here
Debug.Print "改为 : " & sCancelText
End Select
'这是添加到模块的代码
'********************************************
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SetTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&, ByVal uElapse&, ByVal lpTimerFunc&)
Private Declare Function KillTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&)
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SetWindowText& Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String)
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const NV_MSGBOX As Long = &H5000&
Public sOKText As String, sCancelText As String, sTitle As String
'以下过程和函数
Public Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)
Dim h As Long, hMsg As Long
hMsg = FindWindow("#32770", sTitle)
If hMsg Then
KillTimer hwnd, idEvent
h = GetWindow(hMsg, GW_CHILD)
Do
If GetWndClass(h) = "Button" Then
If GetWndText(h) = "确定" Then SetWindowText h, sOKText
If GetWndText(h) = "取消" Then SetWindowText h, sCancelText
End If
h = GetWindow(h, GW_HWNDNEXT)
Loop While h <> 0
End If
End Sub
Private Function GetWndClass(hwnd As Long) As String
Dim k As Long, sName As String
sName = Space$(128)
k = GetClassName(hwnd, sName, 128)
If k > 0 Then sName = Left$(sName, k) Else sName = "无类"
GetWndClass = sName
End Function
Private Function GetWndText(hwnd As Long) As String
Dim k As Long, sName As String
sName = Space$(128)
k = GetWindowText(hwnd, sName, 128)
If k > 0 Then sName = Left$(sName, k) Else sName = "无标题"
GetWndText = sName
End Function
'********************************************
'这是添加到窗体中的示例代码
'********************************************
Dim WhatDidIchoose As Long
sTitle = "Windows"
sOKText = "我点击"
sCancelText = "我闪t"
SetTimer hwnd, NV_MSGBOX, 10, AddressOf TimerProc
WhatDidIchoose = MsgBox("这是一个对话框示例程序", vbOKCancel, sTitle)
Select Case WhatDidIchoose
Case vbOK
'action here
Debug.Print "改为 : " & sOKText
Case vbCancel
'action here
Debug.Print "改为 : " & sCancelText
End Select
发表评论
您没有权限发表评论!
上一篇
下一篇
相关日志:
文章来自:
Tags:
评论: 4 |
回复


SetWindowText API声明改为:
Private Declare Function SetWindowText& Lib "user32" Alias "SetWindowTextW" (ByVal hwnd As Long, ByVal lpString As Long)
SetWindowTextW是WideChar宽字节字符集Unicode的API,基本设计到字符的API都有*A和*W的版本,而这里用A版本Set了会乱码,因此使用W版本,另外最后一个参数lpString改为了传指针,防止VB把字符串从宽字符转成多字节字符导致仍然乱码。
TimerProc函数:
If GetWndText(h) = "确定" Then SetWindowText h, sOKText
If GetWndText(h) = "取消" Then SetWindowText h, sCancelText
改为:
If Left(GetWndText(h), 2) = "确定" Then
SetWindowText h, StrPtr(sOKText)
ElseIf Left(GetWndText(h), 2) = "取消" Then
SetWindowText h, StrPtr(sCancelText)
End If
原因是:经过调试发现确定和取消后面都有一个空格,因此判断前2个字符是否为确定或取消,也更通用些,或许有些系统带个(&Y)(&N)也不定。
而StrPtr()就是对API声明改成了ByVal Long的对应修改了,取字符串的地址传给API。