在VB中,BorderStyle屬性為0的窗體沒有邊框,並且也沒有與邊框相關的元素。這種窗體具有簡潔、佔用空間少等優點,用它可以設計出某些富有個性的窗體。但是,由於它沒有標題欄,窗體不能移動,同時也不能改變大小,在某些情況下會給使用者造成一定的麻煩。本文介紹在VB中如何用API函式操作無邊框窗體。移動窗體 新建一標準工程,設定Form1的BorderStyle屬性為0。此時執行程式後,無法移動窗體。為能移動窗體,在Form1的程式碼視窗宣告下列函式和常數: Option Explicit Private Declare Function ReleaseCapture Lib “user32” ()AsLong Private Declare Function SendMessage Lib “user32”Alias“SendMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const WM_SYSCOMMAND = &H112 Const SC_MOVE = &HF012 在Form_MouseDown事件中輸入以下程式碼: Private Sub Form_MouseDown(Button As Integer, Shift As Integer,XAs Single, Y As Single) 按下滑鼠左鍵 If Button = vbcenterButton Then 為當前的應用程式釋放滑鼠捕獲 ReleaseCapture 移動窗體 SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE, 0 End If End Sub 注意:此時窗體上不能放置除Shape控制元件以外的任何控制元件,否則,在被控制元件遮住的地方點按滑鼠還是無法移動窗體。要使點按控制元件也能移動窗體,需再新增一個該控制元件的MouseDown事件過程,程式碼與上述過程程式碼相似。 改變窗體的大小 為了改變窗體的大小,需要新增一個Timer控制元件,以定時捕獲滑鼠在窗體中的位置。當滑鼠位於窗體邊緣時,改變滑鼠的形狀,以通知使用者可以進行改變大小的操作。為此,將Timer控制元件的Interval屬性設為100(即每過100毫秒檢測一下滑鼠位置),其他取預設值。 在Form1的程式碼視窗中再新增下列兩個函式,並定義兩個自定義變數和一個字串變數: 取得窗體位置的函式 Private Declare Function GetWindowRect Lib “user32” (ByVal hwndAsLong, lpRect As RECT) As Long 取得滑鼠位置的函式 Private Declare Function GetCursorPos Lib “user32” (lpPointAsPOINTAPI) As Long 滑鼠位置變數 Private Type POINTAPI x As Long y As Long End Type 窗體位置變數 Private Type RECT center As Long Top As Long center As Long Bottom As Long End Type 所要執行的動作變數,是移動還是改變大小及從哪個方向改變大小 Dim Action As String 在Timer1控制元件的Timer事件過程中新增以下程式碼: Private Sub Timer1_Timer() Dim MyRect As RECT Dim MyPoint As POINTAPI MyRect返回當前視窗位置 Call GetWindowRect(Me.hwnd, MyRect) MyPoint返回當前滑鼠位置 Call GetCursorPos(MyPoint) Select Case True 滑鼠位於窗體左上方 Case MyPoint.x Screen.MousePointer = vbSizeNWSE Action = “centerUp” 滑鼠位於窗體右下方 Case MyPoint.x > MyRect.center - 5 And MyPoint.y>MyRect.Bottom - 5 Screen.MousePointer = vbSizeNWSE Action = “centerDown” 滑鼠位於窗體右上方 Case MyPoint.x > MyRect.center - 5 And MyPoint.y ’45度雙向滑鼠指標 Screen.MousePointer = vbSizeNESW Action = “centerUp” 滑鼠位於窗體左下方 Case MyPoint.x MyRect.Bottom - 5 Screen.MousePointer = vbSizeNESW Action = “centerDown” 滑鼠位於窗體左邊 Case MyPoint.x MyRect.Bottom - 5 Screen.MousePointer = vbSizeNS Action = “Down” 滑鼠位於窗體其他位置 Case Else 預設滑鼠指標 Screen.MousePointer = 0 Action = “Move” End Select End Sub 當利用SendMessage函式由系統向視窗傳送改變大小的資訊時,只要將上面移動窗體的語句“SendMessageMe.hwnd,WM_SYSCOMMAND, SC_MOVE, 0”中的第3個引數改為相應的常數即可。 VB中&HF001~&HF008分別是從左、右、上、左上、右上、下、左下、右下8個方向改變窗體大小的常數。結合移動窗體的程式碼,將上述Form_MouseDown事件的程式碼綜合如下(也可以把這8個常數宣告為自定義常數): Private Sub Form_MouseDown(Button As Integer, Shift As Integer,xAs Single, y As Single) 按下滑鼠左鍵 If Button = vbcenterButton Then 為當前的應用程式釋放滑鼠捕獲 ReleaseCapture Select Case Action Case “center” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF001, 0 Case “center” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF002, 0 Case “Up” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF003, 0 Case “centerUp” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF004, 0 Case “centerUp” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF005, 0 Case “Down” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF006, 0 Case “centerDown” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF007, 0 Case “centerDown” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF008, 0 Case “Move” SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE, 0 End Select End If End Sub
在VB中,BorderStyle屬性為0的窗體沒有邊框,並且也沒有與邊框相關的元素。這種窗體具有簡潔、佔用空間少等優點,用它可以設計出某些富有個性的窗體。但是,由於它沒有標題欄,窗體不能移動,同時也不能改變大小,在某些情況下會給使用者造成一定的麻煩。本文介紹在VB中如何用API函式操作無邊框窗體。移動窗體 新建一標準工程,設定Form1的BorderStyle屬性為0。此時執行程式後,無法移動窗體。為能移動窗體,在Form1的程式碼視窗宣告下列函式和常數: Option Explicit Private Declare Function ReleaseCapture Lib “user32” ()AsLong Private Declare Function SendMessage Lib “user32”Alias“SendMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const WM_SYSCOMMAND = &H112 Const SC_MOVE = &HF012 在Form_MouseDown事件中輸入以下程式碼: Private Sub Form_MouseDown(Button As Integer, Shift As Integer,XAs Single, Y As Single) 按下滑鼠左鍵 If Button = vbcenterButton Then 為當前的應用程式釋放滑鼠捕獲 ReleaseCapture 移動窗體 SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE, 0 End If End Sub 注意:此時窗體上不能放置除Shape控制元件以外的任何控制元件,否則,在被控制元件遮住的地方點按滑鼠還是無法移動窗體。要使點按控制元件也能移動窗體,需再新增一個該控制元件的MouseDown事件過程,程式碼與上述過程程式碼相似。 改變窗體的大小 為了改變窗體的大小,需要新增一個Timer控制元件,以定時捕獲滑鼠在窗體中的位置。當滑鼠位於窗體邊緣時,改變滑鼠的形狀,以通知使用者可以進行改變大小的操作。為此,將Timer控制元件的Interval屬性設為100(即每過100毫秒檢測一下滑鼠位置),其他取預設值。 在Form1的程式碼視窗中再新增下列兩個函式,並定義兩個自定義變數和一個字串變數: 取得窗體位置的函式 Private Declare Function GetWindowRect Lib “user32” (ByVal hwndAsLong, lpRect As RECT) As Long 取得滑鼠位置的函式 Private Declare Function GetCursorPos Lib “user32” (lpPointAsPOINTAPI) As Long 滑鼠位置變數 Private Type POINTAPI x As Long y As Long End Type 窗體位置變數 Private Type RECT center As Long Top As Long center As Long Bottom As Long End Type 所要執行的動作變數,是移動還是改變大小及從哪個方向改變大小 Dim Action As String 在Timer1控制元件的Timer事件過程中新增以下程式碼: Private Sub Timer1_Timer() Dim MyRect As RECT Dim MyPoint As POINTAPI MyRect返回當前視窗位置 Call GetWindowRect(Me.hwnd, MyRect) MyPoint返回當前滑鼠位置 Call GetCursorPos(MyPoint) Select Case True 滑鼠位於窗體左上方 Case MyPoint.x Screen.MousePointer = vbSizeNWSE Action = “centerUp” 滑鼠位於窗體右下方 Case MyPoint.x > MyRect.center - 5 And MyPoint.y>MyRect.Bottom - 5 Screen.MousePointer = vbSizeNWSE Action = “centerDown” 滑鼠位於窗體右上方 Case MyPoint.x > MyRect.center - 5 And MyPoint.y ’45度雙向滑鼠指標 Screen.MousePointer = vbSizeNESW Action = “centerUp” 滑鼠位於窗體左下方 Case MyPoint.x MyRect.Bottom - 5 Screen.MousePointer = vbSizeNESW Action = “centerDown” 滑鼠位於窗體左邊 Case MyPoint.x MyRect.Bottom - 5 Screen.MousePointer = vbSizeNS Action = “Down” 滑鼠位於窗體其他位置 Case Else 預設滑鼠指標 Screen.MousePointer = 0 Action = “Move” End Select End Sub 當利用SendMessage函式由系統向視窗傳送改變大小的資訊時,只要將上面移動窗體的語句“SendMessageMe.hwnd,WM_SYSCOMMAND, SC_MOVE, 0”中的第3個引數改為相應的常數即可。 VB中&HF001~&HF008分別是從左、右、上、左上、右上、下、左下、右下8個方向改變窗體大小的常數。結合移動窗體的程式碼,將上述Form_MouseDown事件的程式碼綜合如下(也可以把這8個常數宣告為自定義常數): Private Sub Form_MouseDown(Button As Integer, Shift As Integer,xAs Single, y As Single) 按下滑鼠左鍵 If Button = vbcenterButton Then 為當前的應用程式釋放滑鼠捕獲 ReleaseCapture Select Case Action Case “center” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF001, 0 Case “center” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF002, 0 Case “Up” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF003, 0 Case “centerUp” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF004, 0 Case “centerUp” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF005, 0 Case “Down” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF006, 0 Case “centerDown” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF007, 0 Case “centerDown” SendMessage Me.hwnd, WM_SYSCOMMAND, &HF008, 0 Case “Move” SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE, 0 End Select End If End Sub