转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
另类Msgbox         ★★★★

另类Msgbox

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1293 更新时间:2009/4/23 16:37:56


写过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

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台