# Re: 如何在视窗标题上加上一个按钮呢??

Posted by Windows NT user on January 31, 1999 at 21:42:38:

In Reply to: Re: 如何在视窗标题上加上一个按钮呢?? posted by Rong on January 31, 1999 at 14:58:13:

: : : 如何在视窗标题上加上一个按钮呢??
: : 请参巧 "Visual Basic 6.0 与 Windows API 讲座"
: 我还是不会, 可否教一下呢??

在form:
Private Sub Command1_Click()
'
End Sub

Private Sub Form_Load()
prevWndProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WndProc
End Sub

Private Sub Form_Unload(Cancel As Integer)
SetWindowLong Me.hwnd, GWL_WNDPROC, prevWndProc
End Sub

Function IsInImage(ByVal X As Integer, ByVal Y As Integer)
Dim r1 As RECT, r2 As RECT
Dim border As Integer
IsInImage = False
GetWindowRect Me.hwnd, r1
GetClientRect Me.hwnd, r2
border = (r1.Right - r1.Left - r2.Right) / 2
If X > r1.Left + border + Image1.Left And _
X < r1.Left + border + Image1.Left + Image1.Width And _
Y > r1.Top + border + Image1.Top And _
Y < r1.Top + border + Image1.Top + Image1.Height Then
IsInImage = True
End If
End Function
在module:
Option Explicit

Public Const GWL_WNDPROC = (-4)

Public Const WM_NCHITTEST = &H84
Public Const HTCLIENT = 1
Public Const HTCAPTION = 2

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) 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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 prevWndProc As Long

Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam)
If Msg = WM_NCHITTEST And WndProc = HTCLIENT Then
If Form1.IsInImage(lParam Mod 65536, lParam \ 65536) Then
WndProc = HTCAPTION
End If
End If
End Function
注:
image1 用来代替form的标题列
form 的属性:
controlBox=false
ScaleMode=3
在image1上不止可以放commandButton,基本上,任何控制项都可以放上.

Follow Ups: