您的位置:首页 > 其它

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);

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: