如何在窗口的标题栏上加一个自己的按钮?
如何在窗口的标题栏上加一个自己的按钮?就象“最大”“恢复”和“最小化”按钮一样?
问题点数:20、回复次数:2Top
1 楼rainstormmaster(暴风雨 v2.0)回复于 2004-08-03 12:30:40 得分 5
需要自绘标题栏
或者cut掉标题栏,然后自己模拟标题栏Top
2 楼BlueBeer(1win)回复于 2004-08-03 12:31:39 得分 15
用
SetParent Command1.hwnd, 0
SetWindowLong Command1.hwnd, -8, Me.hwnd
把command1分离窗口出来,使之能自由移动
对me做子类处理,捕获WM_MOVE消息,处理command1的位置
需要注意的是如果希望command1响应事件,需要把它放在一个容器内
如frame1,然后对frame1使用上面的那两个函数。
可以参考
http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=7691
下面是个简单的例子:
在窗体中放上一个frame,在frame中放上一个commandbutton
'窗体代码
Private Sub Command1_Click()
Debug.Print "Click the title button"
End Sub
Private Sub Form_Load()
Frame1.BorderStyle = 0
Command1.Left = 0
Command1.Top = 0
Command1.Width = 240
Command1.Height = 210
moveHwnd = Frame1.hwnd
SetParent moveHwnd, 0
SetWindowLong moveHwnd, -8, Me.hwnd
gHW = Me.hwnd
hook
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unhook
End Sub
'模块部分的代码
Option Explicit
Public moveHwnd As Long
Public Const GWL_WNDPROC = -4
Global lpPrevWndProc As Long
Global gHW As Long
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
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Const WM_MOVE As Long = &H3
Public 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
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case WM_MOVE
Dim t As RECT
GetWindowRect hw, t
MoveWindow moveHwnd, t.Right - 72, t.Top + 6, 16, 14, 1
Case Else
End Select
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Function
Public Sub hook()
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub Unhook()
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub
'方法二==========================================================
'模块
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 GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As Rect) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal dwThreadId&) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook&) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
'*********************
'* Type Declarations *
'*********************
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type CWPSTRUCT
lParam As Long
wParam As Long
Message As Long
hwnd As Long
End Type
'*********************
'* Consts *
'*********************
Const WM_MOVE = &H3
Const WM_SETCURSOR = &H20
Const WM_NCPAINT = &H85
Const WM_COMMAND = &H111
Const SWP_FRAMECHANGED = &H20
Const GWL_EXSTYLE = -20
'*********************
'* Vars *
'*********************
Private WHook&
Private ButtonHwnd As Long
Public Sub Init()
'Create the button that is going to be placed in the Titlebar
ButtonHwnd& = CreateWindowEx(0&, "Button", "?", &H40000000, 50, 50, 14, 14, Form1.hwnd, 0&, App.hInstance, 0&)
'Show the button cause it磗 invisible
Call ShowWindow(ButtonHwnd&, 1)
'Initialize the window hooking for the button
WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID)
Call SetWindowLong(ButtonHwnd&, GWL_EXSTYLE, &H80)
Call SetParent(ButtonHwnd&, GetParent(Form1.hwnd))
End Sub
Public Sub Terminate()
'Terminate the window hooking
Call UnhookWindowsHookEx(WHook)
Call SetParent(ButtonHwnd&, Form1.hwnd)
End Sub
Public Function HookProc&(ByVal nCode&, ByVal wParam&, Inf As CWPSTRUCT)
Dim FormRect As Rect
Static LastParam&
If Inf.hwnd = GetParent(ButtonHwnd&) Then
If Inf.Message = WM_COMMAND Then
Select Case LastParam
'If the LastParam is cmdInTitlebar call the Click-Procedure
'of the button
Case ButtonHwnd&: Call Form1.cmdInTitlebar_Click
End Select
ElseIf Inf.Message = WM_SETCURSOR Then
LastParam = Inf.wParam
End If
ElseIf Inf.hwnd = Form1.hwnd Then
If Inf.Message = WM_NCPAINT Or Inf.Message = WM_MOVE Then
'Get the size of the Form
Call GetWindowRect(Form1.hwnd, FormRect)
'Place the button int the Titlebar
Call SetWindowPos(ButtonHwnd&, 0, FormRect.Right - 75, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)
End If
End If
End Function
'窗体
Public Sub cmdInTitlebar_Click()
MsgBox "Example created by Druid Developing", vbInformation, "About this program"
End Sub
Private Sub Form_Load()
Call Init
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Terminate
End SubTop




