FormBorderStyle设为None,移动Winform窗口的两种方法
来源:互联网 发布:ape转mp3软件 编辑:程序博客网 时间:2024/06/06 09:10
第一种,利用windows的消息机制来实现:
首先﹐.定义鼠標左鍵按下時的Message标识﹔其次﹐在Form1_MouseDown方法﹐讓操作系統誤以為是按下标题栏。
1.定义鼠標左鍵按下時的Message标识
private const int WM_NCLBUTTONDOWN = 0XA1; //.定义鼠標左鍵按下
private const int HTCAPTION = 2;
2.讓操作系統誤以為是按下标题栏
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//為當前的應用程序釋放鼠標鋪獲
ReleaseCapture();
//發送消息﹐讓系統誤以為在标题栏上按下鼠標
SendMessage((int)this.Handle,WM_NCLBUTTONDOWN,HTCAPTION,0);
}
整理为:
//FormBorderStyle设为了None,设置移动Winform窗口
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParan);
public const int HTCAPTION = 2;
public const int WM_SYSCOMMAND = 0x112;
public const int SC_MOVE = 0xf010;
// ////////////////////////////////////
public MainFrm()
{
InitializeComponent();
}
////左键按下时,设置可移动
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
3.申明程序中所Windows的API函數
[DllImport("user32.dll",EntryPoint="SendMessageA")]
private static extern int SendMessage(int hwnd,int wMsg,int wParam,int lParam);
[DllImport("user32.dll")]
private static extern int ReleaseCapture();
第二种,通过自定鼠标左键按下时产生的事件:
* 首先将窗体的边框样式修改为None,让窗体没有标题栏
* 实现这个效果使用了三个事件:鼠标按下、鼠标弹起、鼠标移动
* 鼠标按下时更改变量isMouseDown标记窗体可以随鼠标的移动而移动
* 鼠标移动时根据鼠标的移动量更改窗体的location属性,实现窗体移动
* 鼠标弹起时更改变量isMouseDown标记窗体不可以随鼠标的移动而移动
*/
private bool flagMove = false;
public MainFrm()
{
InitializeComponent();
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)//鼠标按下
{
formPoint = new Point();
int xOffset;
int yOffset;
if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;
formPoint = new Point(xOffset, yOffset);
formMove = true;//开始移动
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)//鼠标移动
{
if (formMove == true)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(formPoint.X, formPoint.Y);
Location = mousePos;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)//鼠标松开
{
if (e.Button == MouseButtons.Left)//按下的是鼠标左键
{
formMove = false;//停止移动
}
}
- FormBorderStyle设为None,移动Winform窗口的两种方法
- FormBorderStyle设为None,移动Winform窗口的两种方法
- winform 窗体属性FormBorderStyle 为none时使窗体可以被移动
- winform 窗体属性FormBorderStyle 为none时使窗体可以被移动
- FormBorderStyle.None 改变窗口大小
- FormBorderStyle.None 改变窗口大小
- FormBorderStyle为None时拖动窗体
- Winform FormBorderStyle.None时,支持改变窗体大小
- WINFORM.FormBorderStyle
- WinForm 代码设置FormBorderStyle的属性
- shell设为NONE后的一些操作
- WinForm设置FormBorderStyle=none后仍可以使用任务栏左键和右键功能
- WinForm设置FormBorderStyle=none后仍可以使用任务栏左键和右键功能
- WinForm设置FormBorderStyle=none后仍可以使用任务栏左键和右键功能
- c# winform最大化覆盖全屏幕(formborderstyle=none)怎么处理?
- Render的mode可以设为两种模式
- winform 快捷键的两种实现方法
- winfrom属性formborderstyle:none以后
- 较好的视频分辨率与码率组合,里面的码率写得偏高
- 简单计算器
- URAL 1960 Palindromes and Super Abilities (Palindromic Tree)
- 排序方法之快速排序
- java 初识 java平台无关性
- FormBorderStyle设为None,移动Winform窗口的两种方法
- Android-ActivityManager框架解析
- 从1到n的数中总共包含1的个数
- hdu 1546 Idiomatic Phrases Game 最短路
- 我们会使用BI吗?
- Set Matrix zeroes--LeetCode
- MoreWindows 微软认证专家博客目录(白话算法,C++ STL,windows编程)
- App Store审核指南中文版(2014.10.11更新)
- poj 2386 Lake counting