移动无标题栏的窗口
2007-04-25 15:04 ruinet 阅读(1429) 评论(1) 收藏 举报本来是想在vb.net中移动无标题栏的窗口的位置,找了很多资料,还是没能解决.
第1种方法:这种方法在vs2003中好象还可以,但是在vs 2005中会报错,实现代码如下
Public Const WM_NCLBUTTONDOWN = &HA1S
Public Const HTCAPTION = 2
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Long
Public Declare Function ReleaseCapture Lib "user32" () As Integer
'在要移动的控件上处理MouseDown事件
Private Sub titleBar1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles titleBar1.MouseDown
Try
If e.Button = MouseButtons.Left Then
ReleaseCapture()
SendMessage(Me.pnlInfo.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End If
Catch ex As Exception
MsgShow(txtInfo, ex.Message)
End Try
End Sub
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
private static extern int ReleaseCapture();
public const int WM_SysCommand = 0x0112;
public const int SC_MOVE = 0xF012;用C#写一个DLL,在VB中调用
实现如下:
C#类库:
namespace MoveWindow
{
public class ClassMoveWindow
{

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
private static extern int ReleaseCapture();
private const int WM_SysCommand = 0x0112;
private const int SC_MOVE = 0xF012;
public void SendMessage(Int32 Handle)
{
ReleaseCapture();
SendMessage(Handle , WM_SysCommand, SC_MOVE, 0);
}
}
}
Private Sub titleBar1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles titleBar1.MouseDown
Dim MoveObj As New MoveWindow.ClassMoveWindow
Try
If e.Button = Windows.Forms.MouseButtons.Left Then
MoveObj.SendMessage(Me.pnlInfo.Handle.ToInt32)
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
MoveObj = Nothing
End Try

End Sub如果你是直接在在窗体上移动位置,则可以加入下面的代码,但如果窗体上有控件则不能响应到MouseDown,这个道理很简单,因为被别的控件挡住了
Public Const WM_NCLBUTTONDOWN = &HA1S
Public Const HTCAPTION = 2
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Long
Public Declare Function ReleaseCapture Lib "user32" () As Integer
'在要移动的控件上处理MouseDown事件
Private Sub titleBar1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles titleBar1.MouseDown
Try
If e.Button = MouseButtons.Left Then
ReleaseCapture()
SendMessage(Me.pnlInfo.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End If
Catch ex As Exception
MsgShow(txtInfo, ex.Message)
End Try
End Sub


浙公网安备 33010602011771号