移動窗體
新建一標(biāo)準(zhǔn)工程,設(shè)置Form1的BorderStyle屬性為0。此時運行程序后,無法移動窗體。為能移動窗體,在Form1的代碼窗口聲明下列函數(shù)和常數(shù):OptionExplicit
PrivateDeclareFunctionGetCursorPosLib“user32”(lpPointaspOINTAPI)AsLong'鼠標(biāo)位置變量PrivateTypePOINTAPI
xAsLongyAsLong
EndType'窗體位置變量
PrivateTypeRECTLeftAsLong
TopAsLongRightAsLong
BottomAsLongEndType
'所要執(zhí)行的動作變量,是移動還是改變大小及從哪個方向改變大小DimActionAsString
在Timer1控件的Timer事件過程中添加以下代碼:PrivateSubTimer1_Timer()
DimMyRectAsRECTDimMyPointAsPOINTAPI
'MyRect返回當(dāng)前窗口位置CallGetWindowRect(Me.hwnd,MyRect)
'MyPoint返回當(dāng)前鼠標(biāo)位置CallGetCursorPos(MyPoint)
SelectCaseTrue'鼠標(biāo)位于窗體左上方
CaseMyPoint.x<MyRect.Left+5AndMyPoint.y<MyRect.Top+5Screen.MousePointer=vbSizeNWSE
Action=“LeftUp”'鼠標(biāo)位于窗體右下方
CaseMyPoint.x>MyRect.Right-5AndMyPoint.y>MyRect.Bottom-5Screen.MousePointer=vbSizeNWSE
Action=“RightDown”'鼠標(biāo)位于窗體右上方
CaseMyPoint.x>MyRect.Right-5AndMyPoint.y<MyRect.Top+5'45度雙向鼠標(biāo)指針
Screen.MousePointer=vbSizeNESWAction=“RightUp”
'鼠標(biāo)位于窗體左下方CaseMyPoint.x<MyRect.Left+5AndMyPoint.y>MyRect.Bottom-5
Screen.MousePointer=vbSizeNESWAction=“LeftDown”
'鼠標(biāo)位于窗體左邊CaseMyPoint.x<MyRect.Left+5
'水平雙向鼠標(biāo)指針Screen.MousePointer=vbSizeWE
Action=“Left”'鼠標(biāo)位于窗體右邊
CaseMyPoint.x>MyRect.Right-5Screen.MousePointer=vbSizeWE
Action=“Right”'鼠標(biāo)位于窗體上方
CaseMyPoint.y<MyRect.Top+5'垂直雙向鼠標(biāo)指針
Screen.MousePointer=vbSizeNSAction=“Up”
'鼠標(biāo)位于窗體下方CaseMyPoint.y>MyRect.Bottom-5
Screen.MousePointer=vbSizeNSAction=“Down”
'鼠標(biāo)位于窗體其他位置CaseElse
'默認(rèn)鼠標(biāo)指針Screen.MousePointer=0
Action=“Move”EndSelect
EndSub當(dāng)利用SendMessage函數(shù)由系統(tǒng)向窗口發(fā)送改變大小的信息時,只要將上面移動窗體的語句“SendMessageMe.hwnd,WM_SYSCOMMAND,SC_MOVE,0”中的第3個參數(shù)改為相應(yīng)的常數(shù)即可。
VB中&HF001~&HF008分別是從左、右、上、左上、右上、下、左下、右下8個方向改變窗體大小的常數(shù)。結(jié)合移動窗體的代碼,將上述Form_MouseDown事件的代碼綜合如下(也可以把這8個常數(shù)聲明為自定義常數(shù)):PrivateSubForm_MouseDown(ButtonAsInteger,ShiftAsInteger,xAsSingle,yAsSingle)
'按下鼠標(biāo)左鍵IfButton=vbLeftButtonThen
'為當(dāng)前的應(yīng)用程序釋放鼠標(biāo)捕獲ReleaseCapture
SelectCaseActionCase“Left”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF001,0Case“Right”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF002,0Case“Up”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF003,0Case“LeftUp”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF004,0Case“RightUp”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF005,0Case“Down”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF006,0Case“LeftDown”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF007,0Case“RightDown”
SendMessageMe.hwnd,WM_SYSCOMMAND,&HF008,0Case“Move”
SendMessageMe.hwnd,WM_SYSCOMMAND,SC_MOVE,0EndSelect
EndIfEndSub
->新聞熱點
疑難解答