当form的formBorderStyle设置为None时,如何实现窗体拖动

blueteeth_yl 2010-01-27 09:13:34
就是自己重新弄一个标题栏的东西,然后实现拖动
...全文
325 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzy1842 2010-01-27
  • 打赏
  • 举报
回复
[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;
在窗体的mousedown事件中实现拖动。
private void frmWinCalendar_MouseDown(object sender, MouseEventArgs e)
{
//拖动窗体
this.Cursor = System.Windows.Forms.Cursors.Hand; //改变鼠标样式
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
this.Cursor = System.Windows.Forms.Cursors.Default;
}
ILOVE_ASPNET 2010-01-27
  • 打赏
  • 举报
回复
up
guanmingle 2010-01-27
  • 打赏
  • 举报
回复
private bool flagMove = false;   
public Form1()
{
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)
{
this.flagMove = true;
}
//左键松开时,设置不可移动
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
this.flagMove = false;
}
//移动
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.flagMove)
{
this.Location = new Point(e.X,e.Y);
}
}
ck11926375 2010-01-27
  • 打赏
  • 举报
回复

private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下

private void picCLose_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void fmLogin_MouseDown(object sender, MouseEventArgs e)
{
int xOffset;
int yOffset;

if (e.Button == MouseButtons.Left)
{
xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
yOffset = -e.Y - SystemInformation.CaptionHeight -
SystemInformation.FrameBorderSize.Height;
mouseOffset = new Point(xOffset, yOffset);
isMouseDown = true;
}
}

private void fmLogin_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mouseOffset.X+5, mouseOffset.Y+30);
Location = mousePos;
}
}

private void fmLogin_MouseUp(object sender, MouseEventArgs e)
{
// 修改鼠标状态isMouseDown的值
// 确保只有鼠标左键按下并移动时,才移动窗体
if (e.Button == MouseButtons.Left)
{
isMouseDown = false;
}
}
LoveLife_Go 2010-01-27
  • 打赏
  • 举报
回复
没这么做过,看看高手怎么处理的
在用C#做桌面程序,有后会碰到要修改WinForm 的系统边框样式(系统镶边样式)。如,改变标题栏 的背景,系统图标,系统按钮,或者要做一个自己的 ,有自己的背景。那么直接调用win32内部c语言函数 很不方便。怎样才能做有自己的系统边框的C#呢? 我想就是有几年工作经验的程序员,也会觉得那是很麻烦的事。那么,为什么不把这个工作封装成一个控件呢? 下面,是我自己根据以往的经验,做了一个自定义系统边框控件。 使用方法是,自己用图形编辑软件画好背景,然后把此图片作为的背景图片。 再把formFormBorderStyle属性设置None; 然后,在代码文件中定义一个控件类对象:public partial class Form1 : Form { CustomBorder.CustomBorderEx cbe = null; public Form1() { InitializeComponent(); cbe = new CustomBorder.CustomBorderEx(this); } } 就可以定义能实现拖动功能的边框。 public partial class Form1 : Form { CustomBorder.CustomBorderEx cbe = null; public Form1() { InitializeComponent(); cbe = new CustomBorder.CustomBorderEx(this, true,new Size(100,100)); } } 上面可以定义一个带拖动,拉伸,调整大小的边框。 有候需要在关联的上放一些可以随大小改变而自动调整成合适大小位置的控件容器,如,Panel控件。 那么应该把它的大小和位置定为一个客户区内。 得到客户区的方法是 Rectangle rt = cbe.ClientRegion;下面是一个可以正常工作的自定义。 public partial class Form1 : Form { CustomBorder.CustomBorderEx cbe = null; public Form1() { InitializeComponent(); cbe = new CustomBorder.CustomBorderEx(this, true,new Size(100,100)); Rectangle rt = cbe.ClientRegion; this.panel1.Location = new Point(rt.X, rt.Y); this.panel1.Size = new Size(rt.Width, rt.Height); } //的Resize事件处理程序 private void Form1_Resize(object sender, EventArgs e) { cbe.InvalidateCompute(); Rectangle rt = cbe.ClientRegion; this.panel1.Location = new Point(rt.X, rt.Y); this.panel1.Size = new Size(rt.Width, rt.Height); } } 上面代码是从边框控件得到可用客户区的位置和大小。 然后,在Resize事件相应方法中把panel1容器的 位置和大小设置成客户区的值。 注意,在调用cbe.ClientRegion之前,最好先调用cbe.InvalidateCompute();方法一下,重新计算一下边框和客户区的位置和大小。 那么,之后我们只要在设计器中,注意把我们的其他一些控件(如,Button 等),放到panel1里面就行。(如果不用上面办法,panel1的大小和位置覆盖住整个winform的话,会导致边框的事件捕捉不到,出现,不能拖动,不能调整大小的情况。) //------------------ 那么自定义系统边框的控件主要的方法有如下几个: 1构造方法: new CustomBorder.CustomBorderEx(this, true,new Size(100,100)); 第一个参数,要关联的winform;第二个参数,表示是否可以改变的 大小;第三个参数,的最小宽和高值。 2属性: cbe.ClientRegion 用来得到从新计算后的客户区大小。 3成员方法: cbe.InvalidateCompute();从新计算调整后的边框位置和大小,客户区位置和大小。 //----------------- 这个控件的版本是v1.0版的,由于工作忙,业余间少,可能还不够完美。相信以后的版本会好些。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Drawing.Drawing2D; namespace KRT.Component.Common { /// /// 停靠面板控件 /// public class DockPanel : Panel { /// /// 关闭按钮 /// DockPanelCloseButton closeButton = new DockPanelCloseButton(); /// /// 浮动口 /// Form flowForm = new Form(); /// /// 分割栏 /// Splitter splitter = new Splitter(); /// /// 标题栏 /// Label titleBar = new Label(); /// /// 标题栏背景色 /// public Color TitleBackColor //using System.Drawing; { get { return titleBar.BackColor; } set { titleBar.BackColor = value; closeButton.BackColor = value; } } /// /// 标题栏前景色 /// public Color TitleForeColor { get { return titleBar.ForeColor; } set { titleBar.ForeColor = value; } } /// /// 标题栏文本 /// [Localizable(true)] public string TitleText { get { return titleBar.Text; } set { titleBar.Text = value; } } /// /// 标题栏可见 /// public bool TitleVisible { get { return titleBar.Visible; } set { titleBar.Visible = value; closeButton.Visible = value; } } /// /// 浮动口最大尺寸 /// Size flowFormMaxSize = new Size(640, 480); public Size FlowFormMaxSize { get { return flowFormMaxSize; } set { flowFormMaxSize = value; if (this.VisibleAll) { flowForm.Size = flowFormMaxSize; } } } /// /// 整个控件的隐藏、显示,包括浮动口状态下 /// bool visibleAll = true; public bool VisibleAll { get { return visibleAll; } set { visibleAll = value; if (flowForm.Controls.Count > 0) { flowForm.Visible = visibleAll; } else { this.Visible = visibleAll; } } } /// /// 控件初始化 /// public DockPanel() { //InitializeComponent(); flowForm.ShowInTaskbar = false; // 关闭按钮 closeButton.Location = new System.Drawing.Point(0, 0); closeButton.Name = "Close"; closeButton.Size = new System.Drawing.Size(16, 16); closeButton.TabIndex = 0; closeButton.BackColor = SystemColors.ControlDark; closeButton.Click += new EventHandler(closeButton_Click); // 浮动口 flowForm.FormBorderStyle = FormBorderStyle.SizableToolWindow; //VS2008自带的设计器进行设置的边框。 flowForm.ShowInTaskbar = false; //任务栏 flowForm.MaximizeBox = false; //最大化按钮 flowForm.Move += new EventHandler(flowForm_Move); //EventHandler是asp.net内置的委托,事件是特殊的委托。 flowForm.MouseCaptureChanged += new EventHandler(flowForm_MouseCaptureChanged); // flowForm.FormClosing += new FormClosingEventHandler(flowForm_FormClosing); // 标题栏 titleBar.AutoSize = false; //自动调整大小 titleBar.Dock = DockStyle.Top;//控件顶端;DockStyle是个枚举,有none, top, bottom... titleBar.Height = 18; titleBar.Padding = new Padding(3); titleBar.MouseDown += new MouseEventHandler(titleBar_MouseDown); titleBar.MouseMove += new MouseEventHandler(titleBar_MouseMove); titleBar.MouseUp += new MouseEventHandler(titleBar_MouseUp); //this.Padding = new Padding(1); this.Controls.Add(closeButton); this.Controls.Add(titleBar); this.ParentChanged += new EventHandler(DockPanel_ParentChanged); this.VisibleChanged += new EventHandler(DockPanel_VisibleChanged); this.MouseDown += new MouseEventHandler(titleBar_MouseDown); this.MouseMove += new MouseEventHandler(titleBar_MouseMove); this.MouseUp += new MouseEventHandler(titleBar_MouseUp); this.ControlAdded += new ControlEventHandler(DockPanel_ControlAdded); } /// /// 如果是用户单击浮动口的关闭按钮,隐藏浮动口 /// /// /// void flowForm_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; flowForm.Visible = false; visibleAll = false; closeButton_Click(sender, e); } } /// /// 添加子控件调整顺序 /// /// /// void DockPanel_ControlAdded(object sender, ControlEventArgs e) { if (e.Control != titleBar && e.Control != closeButton) { e.Control.BringToFront(); } } /// /// 同修改Splitter的可视效果 /// /// /// void DockPanel_VisibleChanged(object sender, EventArgs e) { splitter.Visible = Visible; if (this.Parent != null && Visible) { if (!this.DesignMode) { if (this.Parent.Controls.IndexOf(splitter) < 0) { //splitter.Dock = this.Dock; //this.Parent.Controls.AddRange(new Control[] { splitter, this }); this.Parent.Controls.Add(splitter); int index = this.Parent.Controls.IndexOf(this); this.Parent.Controls.SetChildIndex(splitter, index); //this.Parent.Controls.SetChildIndex(this, index + 1); //foreach (Control item in this.Parent.Controls) //{ // Debug.WriteLine( // this.Parent.Controls.GetChildIndex(item).ToString() + " : " + // item.ToString()); //} } } } } /// /// 初始化Splitter /// /// /// void DockPanel_ParentChanged(object sender, EventArgs e) { //if (this.Parent != null) //{ // if (!this.DesignMode) // { // //Parent.Controls.Add(splitter); // splitter.Dock = this.Dock; // this.Parent.Controls.AddRange(new Control[] { splitter, this }); // } //} } /// /// 关闭事件 /// public event EventHandler CloseButtonClick; /// /// 单击关闭按钮,触发关闭事件 /// /// /// void closeButton_Click(object sender, EventArgs e) { VisibleAll = false; if (null != CloseButtonClick) { CloseButtonClick(sender, e); } } /// /// 浮动口开始拖动触发 /// /// /// void flowForm_MouseCaptureChanged(object sender, EventArgs e) { if (!flowFormCapture) { flowFormCapture = true; } else { flowFormCapture = false; flowFormDock = true; } } bool flowFormDock = false; // 浮动口可以被停靠 bool flowFormCapture = false; // 浮动口开始拖动 /// /// 浮动口移动过程 /// /// /// void flowForm_Move(object sender, EventArgs e) { if (flowFormDock) { flowFormDock = false; switch (this.Dock) { case DockStyle.Left: if (flowForm.Location.X < Parent.Location.X) { ShowDockPanel(); } break; case DockStyle.Top: if (flowForm.Location.Y Parent.Location.X + Parent.Width) { ShowDockPanel(); } break; case DockStyle.Bottom: if (flowForm.Location.Y + flowForm.Height > Parent.Location.Y + Parent.Height) { ShowDockPanel(); } break; } } } /// /// 控件尺寸改变 /// /// protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); closeButton.Location = new Point( ClientRectangle.Right - closeButton.Width - 1, 1); closeButton.Refresh(); } Point oldMouseLocation; // 鼠标位置 Rectangle rect; // 面板区域 bool mouseDown = false; // 鼠标按下 /// /// 鼠标按下事件 /// /// /// void titleBar_MouseDown(object sender, MouseEventArgs e) { if (this.Dock != DockStyle.None && this.Dock != DockStyle.Fill) { oldMouseLocation = e.Location; mouseDown = true; rect = this.RectangleToScreen(ClientRectangle); ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); } } /// /// 鼠标移动事件 /// /// /// void titleBar_MouseMove(object sender, MouseEventArgs e) { if (mouseDown) { ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); rect.Offset(e.X - oldMouseLocation.X, e.Y - oldMouseLocation.Y); oldMouseLocation = e.Location; ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); } } /// /// 鼠标弹起事件 /// /// /// void titleBar_MouseUp(object sender, MouseEventArgs e) { if (mouseDown) { mouseDown = false; ControlPaint.DrawReversibleFrame(rect, Color.Black, FrameStyle.Thick); Rectangle rc = this.RectangleToScreen(ClientRectangle); Point pt = this.PointToScreen(e.Location); if (!rc.Contains(pt)) { ShowFlowForm(); } } } //protected override void OnPaint(PaintEventArgs e) //{ // base.OnPaint(e); // Pen borderPen = new Pen(borderColor); // Rectangle rc = new Rectangle( // e.ClipRectangle.Left, e.ClipRectangle.Top, // e.ClipRectangle.Right - 1, e.ClipRectangle.Bottom - 1); // e.Graphics.DrawRectangle(borderPen, rc); //} /// /// 显示浮动口 /// void ShowFlowForm() { flowForm.Show(Parent); flowForm.Location = new Point(rect.Left, rect.Top); Size newSize = new Size(rect.Width, rect.Height); if (newSize.Width > FlowFormMaxSize.Width) { newSize.Width = FlowFormMaxSize.Width; } newSize.Height = newSize.Height + (this.TitleVisible ? SystemInformation.CaptionHeight - this.titleBar.Height : SystemInformation.CaptionHeight); if (newSize.Height > FlowFormMaxSize.Height) { newSize.Height = FlowFormMaxSize.Height; } flowForm.Size = newSize; flowForm.Text = TitleText; while (this.Controls.Count > 2) { for (int i = 0; i < this.Controls.Count; i++) { if (this.Controls[i] != closeButton && this.Controls[i] != titleBar) { flowForm.Controls.Add(this.Controls[i]); break; } } } this.Visible = false; } /// /// 显示停靠面板 /// void ShowDockPanel() { this.Visible = true; while (flowForm.Controls.Count > 0) { this.Controls.Add(flowForm.Controls[0]); } flowForm.Hide(); } } }

110,578

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧