C# 去掉标题栏后,这样用鼠标移动窗体?
我想做一个类似输入法窗体的小时钟,可是用 this.FormBorderStyle = FormBorderStyle.None; 去掉标题栏后无法用鼠标移动窗体!!
特来请教解决法案,(最好就像,搜狗输入法那样,鼠标放到边上的时候,变成“十字”可以移动) 顺便说一下本人很菜,希望详细说一下谢谢!
我想做一个类似输入法窗体的小时钟,可是用 this.FormBorderStyle = FormBorderStyle.None; 去掉标题栏后无法用鼠标移动窗体!!
特来请教解决法案,(最好就像,搜狗输入法那样,鼠标放到边上的时候,变成“十字”可以移动) 顺便说一下本人很菜,希望详细说一下谢谢!
protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x01;
const int HTCAPTION = 0x02;
if (m.Msg == WM_NCHITTEST)
{
this.DefWndProc(ref m);
if (m.Result.ToInt32() == HTCLIENT)
m.Result = new IntPtr(HTCAPTION);
else
base.WndProc(ref m);
}
else
{
base.WndProc(ref m);
}
}
虽然是fm程序 但是打个比方就好像web里面的div一样
楼主应该换个思路,方法 不能只看到这个窗体,办法很多的,隐藏边框,标题兰之类是为了美观,但是我们必须给鼠标一个停放点 才能拖动!
鼠标放上去的时候将cursor属性换一下就行了,移开了再换回来。
DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002; private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
窗体也是相当的。自己照着改下就行 private bool _bIsMouseHold; //鼠标按住控件
private int _iMouseDownXPos; //记录按下时光标位置
private int _iMouseDownYPos;
private int _iCtrlXPos; //记录按下时控件位置
private int _iCtrlYPos; #region 鼠标点击移动控件
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this._iMouseDownXPos = Cursor.Position.X;
this._iMouseDownYPos = Cursor.Position.Y;
this._iCtrlXPos = this.Left;
this._iCtrlYPos = this.Top; this._bIsMouseHold = true;
this.Cursor = Cursors.SizeAll;
this.BringToFront();
} protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this._bIsMouseHold = false;
this.Cursor = Cursors.Default;
} protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this._bIsMouseHold)
{
if (this.Parent != null)
{
this.Left = this._iCtrlXPos + Cursor.Position.X - this._iMouseDownXPos;
this.Top = this._iCtrlYPos + Cursor.Position.Y - this._iMouseDownYPos;
}
}
}
#endregion