在VB中,BorderStyle屬性為0的窗體沒有邊框,并且也沒有與邊框相關(guān)的元素。這種窗體具有簡潔、占用空間少等優(yōu)點,用它可以設(shè)計出某些富有個性的窗體。但是,由于它沒有標題欄,窗體不能移動,同時也不能改變大小,在某些情況下會給使用者造成一定的麻煩。本文介紹在VB中如何用API函數(shù)操作無邊框窗體。
十余年的通道網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整通道建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“通道網(wǎng)站設(shè)計”,“通道網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
移動窗體
新建一標準工程,設(shè)置Form1的BorderStyle屬性為0。此時運行程序后,無法移動窗體。為能移動窗體,在Form1的代碼窗口聲明下列函數(shù)和常數(shù):
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屬性設(shè)為100(即每過100毫秒檢測一下鼠標位置),其他取默認值。
在Form1的代碼窗口中再添加下列兩個函數(shù),并定義兩個自定義變量和一個字符串變量:
取得窗體位置的函數(shù)
Private Declare Function GetWindowRect Lib “user32” (ByVal hwndAsLong, lpRect As RECT) As Long
取得鼠標位置的函數(shù)
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
所要執(zhí)行的動作變量,是移動還是改變大小及從哪個方向改變大小
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 MyRect.center + 5 And MyPoint.y ="" p=""
Screen.MousePointer = vbSizeNWSE
Action = “centerUp”
鼠標位于窗體右下方
Case MyPoint.x MyRect.center - 5 And MyPoint.yMyRect.Bottom - 5
Screen.MousePointer = vbSizeNWSE
Action = “centerDown”
鼠標位于窗體右上方
Case MyPoint.x MyRect.center - 5 And MyPoint.y="" p="" +=""
’45度雙向鼠標指針
Screen.MousePointer = vbSizeNESW
Action = “centerUp”
鼠標位于窗體左下方
Case MyPoint.x MyRect.center + 5 And MyPoint.yMyRect.Bottom - 5
Screen.MousePointer = vbSizeNESW
Action = “centerDown”
鼠標位于窗體左邊
Case MyPoint.x MyRect.center + 5
水平雙向鼠標指針
Screen.MousePointer = vbSizeWE
Action = “center”
鼠標位于窗體右邊熱門推薦: C++程序設(shè)計之四書五經(jīng) 談談JAVA程序的反編譯1 2
MyRect.center - 5
Screen.MousePointer = vbSizeWE
Action = “center”
鼠標位于窗體上方
Case MyPoint.y MyRect.Top + 5
垂直雙向鼠標指針
Screen.MousePointer = vbSizeNS
Action = “Up”
鼠標位于窗體下方
Case MyPoint.y MyRect.Bottom - 5
Screen.MousePointer = vbSizeNS
Action = “Down”
鼠標位于窗體其他位置
Case Else
默認鼠標指針
Screen.MousePointer = 0
Action = “Move”
End Select
End Sub
當利用SendMessage函數(shù)由系統(tǒng)向窗口發(fā)送改變大小的信息時,只要將上面移動窗體的語句“SendMessageMe.hwnd,WM_SYSCOMMAND, SC_MOVE, 0”中的第3個參數(shù)改為相應的常數(shù)即可。
VB中HF001~HF008分別是從左、右、上、左上、右上、下、左下、右下8個方向改變窗體大小的常數(shù)。結(jié)合移動窗體的代碼,將上述Form_MouseDown事件的代碼綜合如下(也可以把這8個常數(shù)聲明為自定義常數(shù)):
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
很簡單,通過WindowsAPI,刪除窗體菜單項就行了
首先在窗體類中聲明API:
Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
Declare Function RemoveMenu Lib "user32" (ByVal lngHmenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
其次聲明API參數(shù)常量:
Const MF_BYPOSITION As Integer = H400
然后在窗體類中寫入過程:
Private Sub UserForm_Initialize_stopmove() '禁止窗體移動
? Dim lngHwnd As IntPtr
? Dim lngHmenu As IntPtr
? lngHwnd = Me.Handle
? If lngHwnd 0 Then
? ? ? lngHmenu = GetSystemMenu(lngHwnd, False)
? ? ? RemoveMenu(lngHmenu, 0, MF_BYPOSITION) '這里的0代表菜單中從上往下數(shù)第一個菜單項
? End If
End Sub
Private Sub UserForm_Initialize_onmove() '恢復窗體移動
? Dim lngHwnd As IntPtr
? Dim lngHmenu As IntPtr
? lngHwnd = Me.Handle
? If lngHwnd 0 Then
? ? ? lngHmenu = GetSystemMenu(lngHwnd, True)
RemoveMenu(lngHmenu, 0, MF_BYPOSITION) '這里的0與禁止代碼中的數(shù)值同步,原因時雖然表面上刪除了菜單項,實則為隱藏了菜單項,各個菜單的索引值并沒有變,所以0依然代表初始菜單的第一個菜單項,即被刪除的那個菜單項
? End If
End Sub
然后如果你的窗口菜單是動態(tài)變化的,建議聲明常數(shù):
Const MF_BYPOSITION As Integer = H0
然后使用相關(guān)的Windows功能的常數(shù)進行刪除菜單。常數(shù)需要自行查看winuser.h頭文件
如果找不到該頭文件,可以看這里:網(wǎng)頁鏈接
其余信息詳見MSDN:網(wǎng)頁鏈接
最后說一下,不建議前面網(wǎng)友說的重寫WndProc的方法,因為這樣攔截標題欄點擊消息會導致窗體本身的菜單也無法顯示出來,有損窗體功能,并且像雙擊左上角圖標關(guān)閉窗體這樣的功能也會跟著攔截消息的操作一起被吞掉。
VB6.0寫的,代碼很簡單,無意中寫成的。應該可以參考。不需要任何api函數(shù)。在無邊框窗體頂部中放入一個label標簽。然后用label的 mouse down 和mouse move事件實現(xiàn)
Dim a, b As Single
Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
a = X
b = Y
End Sub
Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Form1.Move Left + X - a, Top + Y - b
End If
End Sub
Imports System Drawing Imports System Windows Forms ****************************************** Private oOriginalRegion As Region = Nothing 用于窗體移動 Private bFormDragging As Boolean = False Private oPointClicked As Point ****************************************** Private Sub Form _MouseDown(ByVal sender As Object ByVal e As System Windows Forms MouseEventArgs) Handles MyBase MouseDown Me bFormDragging = True Me oPointClicked = New Point(e X e Y) End Sub ****************************************** Private Sub Form _MouseUp(ByVal sender As Object ByVal e As System Windows Forms MouseEventArgs) Handles MyBase MouseUp Me bFormDragging = False End Sub ****************************************** Private Sub Form _MouseMove(ByVal sender As Object ByVal e As System Windows Forms MouseEventArgs) Handles MyBase MouseMove If Me bFormDragging Then Dim oMoveToPoint As Point 以當前鼠標位置為基礎(chǔ) 找出目標位置 oMoveToPoint = Me PointToScreen(New Point(e X e Y)) 根據(jù)開始位置作出調(diào)整 oMoveToPoint Offset(Me oPointClicked X * _ (Me oPointClicked Y + _ SystemInformation CaptionHeight + _ SystemInformation BorderSize Height) * ) 移動窗體 Me Location = oMoveToPoint End If
lishixinzhi/Article/program/ASP/201311/21755
網(wǎng)頁標題:vb點虐
拖動窗口 vb窗體移動到右上角
分享地址:http://jinyejixie.com/article42/ddisphc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、、App開發(fā)、小程序開發(fā)、品牌網(wǎng)站設(shè)計、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)