WinForm 无边框窗体 拖动工作区移动窗体
2013-01-18 12:21
344 查看
方案1 : 通过重载消息处理实现。重写窗口过程(WndProc),处理一些非客户区消息(WM_NCxxxx),C#中重写窗口过程不用再调用SetWindowLong API了,直接overide一个WndProc就可以了,不用声明api函数。
鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动
[c-sharp] view plaincopyprint?
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x84)
{
switch (m.Result.ToInt32())
{
case 1:
m.Result = new IntPtr(2);
break;
}
}
}
方案2 : 调用非托管的动态链接库,通过控件的鼠标按下事件(MouseDown)发送一个拖动的消息,可以给控件添加MouseDown事件后,拖动这个控件来移动窗体
[c-sharp] view plaincopyprint?
using System.Runtime.InteropServices;
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("User32.DLL")]
public static extern bool ReleaseCapture();
public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 61456;
public const int HTCAPTION = 2;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
}
方案3 : 直接在控件上写事件,朋友的是一个PictureBox 停靠在主窗体,然后主窗体设置的无边框,用的是这中方法
[c-sharp] view plaincopyprint?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestShow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point downPoint;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
downPoint = new Point(e.X, e.Y);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - downPoint.X,
this.Location.Y + e.Y - downPoint.Y);
}
}
}
}
鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动
[c-sharp] view plaincopyprint?
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x84)
{
switch (m.Result.ToInt32())
{
case 1:
m.Result = new IntPtr(2);
break;
}
}
}
方案2 : 调用非托管的动态链接库,通过控件的鼠标按下事件(MouseDown)发送一个拖动的消息,可以给控件添加MouseDown事件后,拖动这个控件来移动窗体
[c-sharp] view plaincopyprint?
using System.Runtime.InteropServices;
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("User32.DLL")]
public static extern bool ReleaseCapture();
public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 61456;
public const int HTCAPTION = 2;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
}
方案3 : 直接在控件上写事件,朋友的是一个PictureBox 停靠在主窗体,然后主窗体设置的无边框,用的是这中方法
[c-sharp] view plaincopyprint?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestShow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point downPoint;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
downPoint = new Point(e.X, e.Y);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - downPoint.X,
this.Location.Y + e.Y - downPoint.Y);
}
}
}
}
相关文章推荐
- WinForm 无边框窗体 拖动工作区移动窗体
- WinForm 无边框窗体 拖动工作区移动窗体
- .Net winform拖动工作区移动窗体
- winform拖动无边框窗体
- C# WinForm 拖动无边框窗体 改变无边框窗体尺寸
- c# 移动拖动无边框窗体
- [Winform/C#]点击工作区域移动窗体
- Winform无边框窗体(FormBorderStyle属性设为None)自定义移动
- C# WinForm 拖动无边框窗体
- 拖动窗体工作区移动
- WinForm 移动无边框窗体
- C# WinForm开发系列之自定义无边框窗体(最大化,最小化,关闭,拉伸和移动的相关知识)
- C# winform 无边框窗体鼠标点击移动事件
- c# winform移动无边框窗体
- winform拖动无边框窗体
- winform无系统边框时移动窗体
- winform 可拖动无边框窗体解决办法
- winform 拖动无边框窗体(调用Windows API)
- winForm pictureBox拖动图片and动态new 窗体&移动Panel窗体头
- 无边框Winform窗体移动的几种方式