通常我们是拖动标题栏来移动窗口,当没有标题栏时,该如何来拖动窗口呢?方法是 当我们在窗口区按下鼠标左键时,调用ReleaseCapture函数,释放鼠标的俘获,同时 发送移动标题栏的消息,就可以实现窗口的拖动。 下面的例子实现了该功能。 >>步骤1----建立新工程,在窗体上放置CommandButton按钮。 >>步骤2----改变窗体的外观,使BorderStyle = 0(None) >>步骤3----编写如下代码:Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function SendMessage Lib "user32" Alias _ "SendMessageA" (ByVal hwnd As Long, ByVal _ wMsg As Long, ByVal wParam As Long, _ lParam As Any) As LongPrivate Const WM_SYSCOMMAND = &H112 Private Const SC_MOVE = &HF010& Private Const WM_NCLBUTTONDOWN = &HA1 Private Const HTCAPTION = 2Private Sub Command1_Click() End End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ReleaseCapture SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0 'SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0& '上述两种方法都能实现该功能。 End Sub>>步骤4----编译运行,在窗体上按下鼠标左键,是不是可以拖动了?按下Command1结束。
用API做的 代码记不住了 在VB代码大全里面有个完整的例子
如何用VB做异形窗体? http://www.minisoft.d2g.com/ --------------------------------------------------------------- 感谢您使用微软产品。 你需要先创建一个Region, 然后用SetWindowRgn设置窗体的形状成这个Region. 你可以用下面的API创建Region: CreateEllipticRgn CreatePolygonRgn CreateEllipticRgnIndirect CreatePolygonRgn CreatePolyPolygonRgn CreateRectRgn CreateRectRgnIndirect CreateRoundRectRgn CombineRgn 下面是一段将窗体的形状设置成椭圆的代码 1). Start a Standard EXE, Form1 is added by default. 2). Change the Form1's BorderStyle property to 0-None 3). Put a command button in the center of the Form1. 4). Paste the following code in the code section of Form1 Option Explicit Private Declare Function CreateEllipticRgn Lib "gdi32" _ (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _ ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" _ (ByVal hwnd As Long, ByVal hRgn As Long, _ ByVal bRedraw As Boolean) As Long
Private MoveNow As Boolean Private StartX As Single Private StartY As Single Private Sub Command1_Click() Unload Me End Sub Private Sub Form_Load() Dim rgnhandle As Long
Command1.Caption = "Close Me" MoveNow = False Me.Height = 8000 Me.Width = 8000 rgnhandle = CreateEllipticRgn(0, 0, 400, 200) SetWindowRgn hwnd, rgnhandle, True End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) MoveNow = True StartX = X StartY = Y End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If MoveNow Then Me.Move Me.Left + (X - StartX), Me.Top + (Y - StartY) End If End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) MoveNow = False End Sub - 微软亚洲技术中心 VB技术支持 本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款 (http://www.csdn.net/microsoft/terms.shtm)。
下面的例子实现了该功能。
>>步骤1----建立新工程,在窗体上放置CommandButton按钮。
>>步骤2----改变窗体的外观,使BorderStyle = 0(None)
>>步骤3----编写如下代码:Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, _
lParam As Any) As LongPrivate Const WM_SYSCOMMAND = &H112
Private Const SC_MOVE = &HF010&
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2Private Sub Command1_Click()
End
End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0
'SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
'上述两种方法都能实现该功能。
End Sub>>步骤4----编译运行,在窗体上按下鼠标左键,是不是可以拖动了?按下Command1结束。
代码记不住了
在VB代码大全里面有个完整的例子
CreatePolygonRgn
CreateEllipticRgnIndirect
CreatePolygonRgn
CreatePolyPolygonRgn
CreateRectRgn
CreateRectRgnIndirect
CreateRoundRectRgn
CombineRgn
下面是一段将窗体的形状设置成椭圆的代码 1). Start a Standard EXE, Form1 is added by default.
2). Change the Form1's BorderStyle property to 0-None
3). Put a command button in the center of the Form1.
4). Paste the following code in the code section of Form1 Option Explicit Private Declare Function CreateEllipticRgn Lib "gdi32" _
(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" _
(ByVal hwnd As Long, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long
Private MoveNow As Boolean
Private StartX As Single
Private StartY As Single Private Sub Command1_Click()
Unload Me
End Sub Private Sub Form_Load() Dim rgnhandle As Long
Command1.Caption = "Close Me"
MoveNow = False
Me.Height = 8000
Me.Width = 8000
rgnhandle = CreateEllipticRgn(0, 0, 400, 200)
SetWindowRgn hwnd, rgnhandle, True
End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y
As Single) MoveNow = True
StartX = X
StartY = Y
End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y
As Single) If MoveNow Then Me.Move Me.Left + (X - StartX), Me.Top + (Y - StartY) End If End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As
Single) MoveNow = False End Sub - 微软亚洲技术中心 VB技术支持 本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款 (http://www.csdn.net/microsoft/terms.shtm)。