設(shè)置全局變量:

創(chuàng)新互聯(lián)客戶idc服務(wù)中心,提供服務(wù)器托管機柜、成都服務(wù)器、成都主機托管、成都雙線服務(wù)器等業(yè)務(wù)的一站式服務(wù)。通過各地的服務(wù)中心,我們向成都用戶提供優(yōu)質(zhì)廉價的產(chǎn)品以及開放、透明、穩(wěn)定、高性價比的服務(wù),資深網(wǎng)絡(luò)工程師在機房提供7*24小時標(biāo)準(zhǔn)級技術(shù)保障。
Dim?drag?As?Boolean
Dim?mousex?As?Integer
Dim?mousey?As?Integer
假設(shè)你想拖動的是Panel1控件,以及此控件上的?Label1(用于顯示標(biāo)題)和PictureBox4(用于顯示圖標(biāo)):
Private?Sub?TitleMove_MouseDown(sender?As?Object,?e?As?System.Windows.Forms.MouseEventArgs)?Handles?Panel1.MouseDown,?Label1.MouseDown,?PictureBox4.MouseDown
drag?=?True
mousex?=?Windows.Forms.Cursor.Position.X?-?Me.Left
mousey?=?Windows.Forms.Cursor.Position.Y?-?Me.Top
End?Sub
Private?Sub?TitleMove_MouseMove(sender?As?Object,?e?As?System.Windows.Forms.MouseEventArgs)?Handles?Panel1.MouseMove,?Label1.MouseMove,?PictureBox4.MouseMove
If?drag?Then
Me.Top?=?Windows.Forms.Cursor.Position.Y?-?mousey
Me.Left?=?Windows.Forms.Cursor.Position.X?-?mousex
End?If
End?Sub
Private?Sub?TitleMove_MouseUp(sender?As?Object,?e?As?System.Windows.Forms.MouseEventArgs)?Handles?Panel1.MouseUp,?Label1.MouseUp,?PictureBox4.MouseUp
drag?=?False
End?Sub
調(diào)用系統(tǒng)API使窗體下?lián)碛嘘幱靶Ч?/p>
using System.Runtime.InteropServices;
然后再窗口類的隨便哪個地方加上:
const int CS_DROPSHADOW = 0x20000;
const int GCL_STYLE = (-26);
//聲明Win32 API
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetClassLong(IntPtr hwnd,int nIndex,int dwNewLong);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassLong(IntPtr hwnd, int nIndex);
最后在窗體的構(gòu)造函數(shù)中加上:
SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DROPSHADOW);
? private const int GWL_STYLE = (-16);
? private const int GWL_EXSTYLE = (-20);
? private const uint WS_EX_LAYERED = 0x80000;
? private const uint WS_EX_TRANSPARENT = 0x20;
? private const uint WS_THICKFRAME = 262144;
? private const uint WS_BORDER = 8388608;
/// summary使指定 「 see cref="IntPtr"/ 句柄」 窗體 邊框樣式變?yōu)闊o邊框。/summary
? public static uint 無邊框窗體(IntPtr 句柄) {
? ? ? uint style = API_窗口.GetWindowLong(句柄, GWL_STYLE);
? ? ? style = ~WS_BORDER;
? ? ? style = ~WS_THICKFRAME;
? ? ? return API_窗口.SetWindowLong(句柄, GWL_STYLE, style); ;
? }
API窗口靜態(tài)類
? [DllImport("user32", EntryPoint = "SetWindowLong")]
? public static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
? [DllImport("user32", EntryPoint = "GetWindowLong")]
? public static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
修改窗口位置
/// summary
? /// 設(shè)置窗體的位置和大小。
? /// /summary
? /// param name="hWnd"/param
? /// param name="hWndInsertAfter"用于標(biāo)識在z-順序的此 CWnd 對象之前的 CWnd 對象。
? /// para/para如果uFlags參數(shù)中設(shè)置了SWP_NOZORDER標(biāo)記則本參數(shù)將被忽略。可為下列值之一:
? /// para/paraHWND_BOTTOM:值為1,將窗體置于Z序的底部。如果參數(shù)hWnd標(biāo)識了一個頂層窗體,則窗體失去頂級位置,并且被置在其他窗體的底部。
? /// para/paraHWND_NOTOPMOST:值為-2,將窗體置于所有非頂層窗體之上(即在所有頂層窗體之后)。如果窗體已經(jīng)是非頂層窗體則該標(biāo)志不起作用。
? /// para/paraHWND_TOP:值為0,將窗體置于Z序的頂部。
? /// para/paraHWND_TOPMOST:值為-1,將窗體置于所有非頂層窗體之上。即使窗體未被激活窗體也將保持頂級位置。/param
? /// param name="x"窗體新的x坐標(biāo)。如hwnd是一個子窗體,則x用父窗體的客戶區(qū)坐標(biāo)表示/param
? /// param name="y"窗體新的y坐標(biāo)。如hwnd是一個子窗體,則y用父窗體的客戶區(qū)坐標(biāo)表示/param
/// param name="Width"指定新的窗體寬度/param
/// param name="Height"指定新的窗體高度/param
? /// param name="wFlags"/param
? /// returns/returns
? [DllImport("user32.dll", CharSet = CharSet.Ansi, EntryPoint = "SetWindowPos")]
? public static extern int SetWindowPos(IntPtr hWnd, hWndInsertAfter hWndInsertAfter, int x, int y, int Width, int Height, wFlags wFlags);
? /// summary
? /// 調(diào)整指定 「 see cref="IntPtr"/ 句柄」 窗體的位置和尺寸。
? /// /summary
? /// param name="句柄"指定 「 see cref="IntPtr"/ 句柄」 窗體/param
? /// param name="x"橫坐標(biāo)/param
? /// param name="y"縱坐標(biāo)/param
? /// param name="w"寬/param
? /// param name="h"高/param
? public static int 調(diào)整窗體(IntPtr 句柄, int x, int y, int w, int h) {
? ? ? return API_窗口.SetWindowPos(句柄, 0, x, y, w, h, wFlags.SWP_NOZORDER);
? }
? /// summary
? /// 調(diào)整指定 「 see cref="IntPtr"/ 句柄」 窗體的位置。
? /// /summary
? /// param name="句柄"指定 「 see cref="IntPtr"/ 句柄」 窗體/param
? /// param name="x"橫坐標(biāo)/param
? /// param name="y"縱坐標(biāo)/param
? public static int 調(diào)整窗體位置(IntPtr 句柄, int x, int y) {
? ? ? return API_窗口.SetWindowPos(句柄, 0, x, y, 0, 0, wFlags.SWP_NOSIZE | wFlags.SWP_NOZORDER);
? }
在VB中,BorderStyle屬性為0的窗體沒有邊框,并且也沒有與邊框相關(guān)的元素。這種窗體具有簡潔、占用空間少等優(yōu)點,用它可以設(shè)計出某些富有個性的窗體。但是,由于它沒有標(biāo)題欄,窗體不能移動,同時也不能改變大小,在某些情況下會給使用者造成一定的麻煩。本文介紹在VB中如何用API函數(shù)操作無邊框窗體。
移動窗體
新建一標(biāo)準(zhǔn)工程,設(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)
按下鼠標(biāo)左鍵
If Button = vbcenterButton Then
為當(dāng)前的應(yīng)用程序釋放鼠標(biāo)捕獲
ReleaseCapture
移動窗體
SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE, 0
End If
End Sub
注意:此時窗體上不能放置除Shape控件以外的任何控件,否則,在被控件遮住的地方點按鼠標(biāo)還是無法移動窗體。要使點按控件也能移動窗體,需再添加一個該控件的MouseDown事件過程,代碼與上述過程代碼相似。
改變窗體的大小
為了改變窗體的大小,需要添加一個Timer控件,以定時捕獲鼠標(biāo)在窗體中的位置。當(dāng)鼠標(biāo)位于窗體邊緣時,改變鼠標(biāo)的形狀,以通知用戶可以進行改變大小的操作。為此,將Timer控件的Interval屬性設(shè)為100(即每過100毫秒檢測一下鼠標(biāo)位置),其他取默認(rèn)值。
在Form1的代碼窗口中再添加下列兩個函數(shù),并定義兩個自定義變量和一個字符串變量:
取得窗體位置的函數(shù)
Private Declare Function GetWindowRect Lib “user32” (ByVal hwndAsLong, lpRect As RECT) As Long
取得鼠標(biāo)位置的函數(shù)
Private Declare Function GetCursorPos Lib “user32” (lpPointAsPOINTAPI) As Long
鼠標(biāo)位置變量
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返回當(dāng)前窗口位置
Call GetWindowRect(Me.hwnd, MyRect)
MyPoint返回當(dāng)前鼠標(biāo)位置
Call GetCursorPos(MyPoint)
Select Case True
鼠標(biāo)位于窗體左上方
Case MyPoint.x MyRect.center + 5 And MyPoint.y ="" p=""
Screen.MousePointer = vbSizeNWSE
Action = “centerUp”
鼠標(biāo)位于窗體右下方
Case MyPoint.x MyRect.center - 5 And MyPoint.yMyRect.Bottom - 5
Screen.MousePointer = vbSizeNWSE
Action = “centerDown”
鼠標(biāo)位于窗體右上方
Case MyPoint.x MyRect.center - 5 And MyPoint.y="" p="" +=""
’45度雙向鼠標(biāo)指針
Screen.MousePointer = vbSizeNESW
Action = “centerUp”
鼠標(biāo)位于窗體左下方
Case MyPoint.x MyRect.center + 5 And MyPoint.yMyRect.Bottom - 5
Screen.MousePointer = vbSizeNESW
Action = “centerDown”
鼠標(biāo)位于窗體左邊
Case MyPoint.x MyRect.center + 5
水平雙向鼠標(biāo)指針
Screen.MousePointer = vbSizeWE
Action = “center”
鼠標(biāo)位于窗體右邊熱門推薦: C++程序設(shè)計之四書五經(jīng) 談?wù)凧AVA程序的反編譯1 2
MyRect.center - 5
Screen.MousePointer = vbSizeWE
Action = “center”
鼠標(biāo)位于窗體上方
Case MyPoint.y MyRect.Top + 5
垂直雙向鼠標(biāo)指針
Screen.MousePointer = vbSizeNS
Action = “Up”
鼠標(biāo)位于窗體下方
Case MyPoint.y MyRect.Bottom - 5
Screen.MousePointer = vbSizeNS
Action = “Down”
鼠標(biāo)位于窗體其他位置
Case Else
默認(rèn)鼠標(biāo)指針
Screen.MousePointer = 0
Action = “Move”
End Select
End Sub
當(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ù)):
Private Sub Form_MouseDown(Button As Integer, Shift As Integer,xAs Single, y As Single)
按下鼠標(biāo)左鍵
If Button = vbcenterButton Then
為當(dāng)前的應(yīng)用程序釋放鼠標(biāo)捕獲
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
當(dāng)用戶按下左鍵時,為按下對象的MouseMove事件綁定處理方法,并記錄鼠標(biāo)坐標(biāo)(窗體左上角為原點,在事件的MouseEventArgs類型的e參數(shù)中提供)。此時用戶移動鼠標(biāo),保持窗體原點與鼠標(biāo)新坐標(biāo)的相對位置不變。當(dāng)用戶釋放左鍵時,撤銷按下對象的MouseMove事件處理方法
設(shè)置窗體的text為空,設(shè)置窗體的controlbox屬性為false,設(shè)置窗體的FormBorderStyle 屬性為Sizable,就可以改變窗體大小了,并且可以在任務(wù)欄點擊。
當(dāng)前標(biāo)題:vb.net無邊框 c#無邊框窗口
文章路徑:http://chinadenli.net/article10/hijcdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計、網(wǎng)站策劃、營銷型網(wǎng)站建設(shè)、商城網(wǎng)站、小程序開發(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)