原弹窗是这样的 public sub command1_click; msgbox "你好' end sub 我想在你好的窗体背景是图片的,请教VB高手,要怎么写?
最新回答
月舞兮颜
2024-10-17 08:00:23
1、这个窗体的背景图片不可以直接修改,VB不支持这么做 2、如果你非要改的话,需要API钩子来完成,具体方法如下: 先创建一个窗体,窗体上添加一个picturebox并把这个picturebox的picture属性设置成你要的图片背景,然后粘贴如下代码 Private Sub Form_Load() Me.Show MsgBoxEx "你好" End Sub
第二部,增加一个模块,添加如下代码
Private Type CWPSTRUCT lParam As Long wParam As Long message As Long hWnd As Long End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length 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 CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam 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 SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long Private Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long '透明处理 Public Const TRANSPARENT = 1
Public Function SubMsgBox(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim sText As String Select Case Msg '对话框颜色和标签颜色Message Case WM_CTLCOLORDLG, WM_CTLCOLORSTATIC Debug.Print wParam & ":Wparam" 'Set Font Back 透明 和改变颜色。 If Msg = WM_CTLCOLORSTATIC Then Call SetBkMode(wParam, TRANSPARENT) End If Call SetTextColor(wParam, lForecolor) 'Set BackGround Picture。 SubMsgBox = CreatePatternBrush(Form1.Picture1.Picture.Handle) Exit Function Case WM_DESTROY 'Remove the MsgBox Subclassing Call SetWindowLong(hWnd, GWL_WNDPROC, lPrevWnd) End Select SubMsgBox = CallWindowProc(lPrevWnd, hWnd, Msg, wParam, ByVal lParam) End Function
Private Function HookWindow(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim tCWP As CWPSTRUCT Dim sClass As String 'This is where you need to Hook the Messagebox CopyMemory tCWP, ByVal lParam, Len(tCWP) If tCWP.message = WM_CREATE Then sClass = Space(255) sClass = Left(sClass, GetClassName(tCWP.hWnd, ByVal sClass, 255)) If sClass = "#32770" Then 'Subclass the Messagebox as it's created lPrevWnd = SetWindowLong(tCWP.hWnd, GWL_WNDPROC, AddressOf SubMsgBox) End If End If HookWindow = CallNextHookEx(lHook, nCode, wParam, ByVal lParam) End Function
Public Function MsgBoxEx(ByVal Prompt As String, Optional ByVal Buttons As VbMsgBoxStyle, Optional ByVal Title As String, Optional ByVal HelpFile As String, Optional ByVal Context As Long, Optional ByVal ForeColor As ColorConstants = -1) As Long Dim lReturn As Long lHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookWindow, App.hInstance, App.ThreadID) 'Set the Defaults If Len(Title) = 0 Then Title = App.Title lForecolor = GetSysColor(COLOR_BTNTEXT) If ForeColor >= 0 Then lForecolor = ForeColor 'Show the Modified MsgBox lReturn = MsgBox(Prompt, Buttons, Title, HelpFile, Context) Call UnhookWindowsHookEx(lHook) MsgBoxEx = lReturn End Function