C#中实现窗口可拖动动的代码~!
窗体没有导航栏·~~就是希望实现鼠标左键按住窗体的任何一个部位都可以拖动窗体~!//移动窗体constintWM_NCLBUTTONDOWN=0xA1;constintH...
窗体没有导航栏·~~ 就是希望实现鼠标左键按住窗体的任何一个部位都可以拖动窗体~!
// 移动窗体
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 0x2;
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left & this.WindowState == FormWindowState.Normal)
{
// 移动窗体
this.Capture = false;
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
我找到了系统自带的这个~! 大家看看 展开
// 移动窗体
const int WM_NCLBUTTONDOWN = 0xA1;
const int HT_CAPTION = 0x2;
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left & this.WindowState == FormWindowState.Normal)
{
// 移动窗体
this.Capture = false;
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
我找到了系统自带的这个~! 大家看看 展开
3个回答
展开全部
嘻嘻,刚做完的项目里面也有这个功能,看好了,源代码来了,不过我这里你说的窗体时自定义的控件,也算是个没导航栏的窗体,看好了哦。源代码
private void ControlArtiAdd_MouseDown(object sender, MouseEventArgs e)
{
conArtiAdd0 = (sender as ControlArtiAdd);//获取开始选中的控件
conArtiAdd0.BringToFront();
if (e.Button == MouseButtons.Left)
{
MX = e.X;
MY = e.Y;
X = conArtiAdd0.Left;
Y = conArtiAdd0.Top;
}
}
//按住鼠标左键拖拽的时候改变控件的位置
private void ControlArtiAdd_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Panel ParentControl = (Panel)this.Parent;
int horizontalScroll = ParentControl.HorizontalScroll.Value;
(sender as ControlArtiAdd).Left += e.X - MX;//目前位置等于,原有位子加上鼠标位子减去原来的位子的移动距离
if ((sender as ControlArtiAdd).Left > (((sender as ControlArtiAdd).Parent.Controls.Count - 1) * (sender as ControlArtiAdd).Width - horizontalScroll))
{
(sender as ControlArtiAdd).Left = X;
return;
}
(sender as ControlArtiAdd).Top = 0;//目前控件的位置更改
}
}
//int count = 0;
//松开控件后确定控件坐标
private void ControlArtiAdd_MouseUp(object sender, MouseEventArgs e)
{
Panel ParentControl = (Panel)this.Parent;
int horizontalScroll = ParentControl.HorizontalScroll.Value;
foreach (ControlArtiAdd conArtiAdd1 in (sender as ControlArtiAdd).Parent.Controls)//遍历父容器中的商品(层)
{
if ((sender as ControlArtiAdd).Left > (((sender as ControlArtiAdd).Parent.Controls.Count - 1) * (sender as ControlArtiAdd).Width - horizontalScroll))
{
(sender as ControlArtiAdd).Left = X;
return;
}
if (conArtiAdd1.Name != conArtiAdd0.Name && conArtiAdd1.Left < conArtiAdd0.Left + conArtiAdd0.Size.Width && conArtiAdd0.Left < conArtiAdd1.Left + conArtiAdd1.Size.Width && conArtiAdd0.Top < conArtiAdd1.Top + conArtiAdd1.Size.Height && conArtiAdd0.Top + conArtiAdd0.Size.Height > conArtiAdd1.Top)//鼠标坐标位子判断(e.x>conArtiAdd1.Left&&e.x<(conArtiAdd1.Left+conArtiAdd1.Width))
{
conArtiAdd0.Left = conArtiAdd1.Left;
conArtiAdd0.Top = conArtiAdd1.Top;
conArtiAdd1.Left = X;
conArtiAdd1.Top = Y;
string ConArtiAdd0LblIndext = conArtiAdd0.lblIndext.Text.Trim().ToString();
conArtiAdd0.lblIndext.Text = conArtiAdd1.lblIndext.Text.Trim().ToString();
conArtiAdd0.TabIndex = Convert.ToInt32(conArtiAdd0.lblIndext.Text.Trim().ToString());
conArtiAdd1.lblIndext.Text = ConArtiAdd0LblIndext;
conArtiAdd1.TabIndex = Convert.ToInt32(conArtiAdd1.lblIndext.Text.Trim().ToString());
}
conArtiAdd0.Top = conArtiAdd1.Top;
}
ParentControl.HorizontalScroll.Value = 0;
for (int i = 0; i < ParentControl.Controls.Count; i++)
{
Control item = ParentControl.Controls[i];
ControlArtiAdd con = item as ControlArtiAdd;
int j = Convert.ToInt32(con.lblIndext.Text);
item.Top = 0;
item.Left = item.Width * (j - 1);
}
}
}
你只需要将自定义控件改成窗体对象就可以了,这段代码涉及到布局问题,你现在只是窗体会更简单。
希望帮到你。
private void ControlArtiAdd_MouseDown(object sender, MouseEventArgs e)
{
conArtiAdd0 = (sender as ControlArtiAdd);//获取开始选中的控件
conArtiAdd0.BringToFront();
if (e.Button == MouseButtons.Left)
{
MX = e.X;
MY = e.Y;
X = conArtiAdd0.Left;
Y = conArtiAdd0.Top;
}
}
//按住鼠标左键拖拽的时候改变控件的位置
private void ControlArtiAdd_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Panel ParentControl = (Panel)this.Parent;
int horizontalScroll = ParentControl.HorizontalScroll.Value;
(sender as ControlArtiAdd).Left += e.X - MX;//目前位置等于,原有位子加上鼠标位子减去原来的位子的移动距离
if ((sender as ControlArtiAdd).Left > (((sender as ControlArtiAdd).Parent.Controls.Count - 1) * (sender as ControlArtiAdd).Width - horizontalScroll))
{
(sender as ControlArtiAdd).Left = X;
return;
}
(sender as ControlArtiAdd).Top = 0;//目前控件的位置更改
}
}
//int count = 0;
//松开控件后确定控件坐标
private void ControlArtiAdd_MouseUp(object sender, MouseEventArgs e)
{
Panel ParentControl = (Panel)this.Parent;
int horizontalScroll = ParentControl.HorizontalScroll.Value;
foreach (ControlArtiAdd conArtiAdd1 in (sender as ControlArtiAdd).Parent.Controls)//遍历父容器中的商品(层)
{
if ((sender as ControlArtiAdd).Left > (((sender as ControlArtiAdd).Parent.Controls.Count - 1) * (sender as ControlArtiAdd).Width - horizontalScroll))
{
(sender as ControlArtiAdd).Left = X;
return;
}
if (conArtiAdd1.Name != conArtiAdd0.Name && conArtiAdd1.Left < conArtiAdd0.Left + conArtiAdd0.Size.Width && conArtiAdd0.Left < conArtiAdd1.Left + conArtiAdd1.Size.Width && conArtiAdd0.Top < conArtiAdd1.Top + conArtiAdd1.Size.Height && conArtiAdd0.Top + conArtiAdd0.Size.Height > conArtiAdd1.Top)//鼠标坐标位子判断(e.x>conArtiAdd1.Left&&e.x<(conArtiAdd1.Left+conArtiAdd1.Width))
{
conArtiAdd0.Left = conArtiAdd1.Left;
conArtiAdd0.Top = conArtiAdd1.Top;
conArtiAdd1.Left = X;
conArtiAdd1.Top = Y;
string ConArtiAdd0LblIndext = conArtiAdd0.lblIndext.Text.Trim().ToString();
conArtiAdd0.lblIndext.Text = conArtiAdd1.lblIndext.Text.Trim().ToString();
conArtiAdd0.TabIndex = Convert.ToInt32(conArtiAdd0.lblIndext.Text.Trim().ToString());
conArtiAdd1.lblIndext.Text = ConArtiAdd0LblIndext;
conArtiAdd1.TabIndex = Convert.ToInt32(conArtiAdd1.lblIndext.Text.Trim().ToString());
}
conArtiAdd0.Top = conArtiAdd1.Top;
}
ParentControl.HorizontalScroll.Value = 0;
for (int i = 0; i < ParentControl.Controls.Count; i++)
{
Control item = ParentControl.Controls[i];
ControlArtiAdd con = item as ControlArtiAdd;
int j = Convert.ToInt32(con.lblIndext.Text);
item.Top = 0;
item.Left = item.Width * (j - 1);
}
}
}
你只需要将自定义控件改成窗体对象就可以了,这段代码涉及到布局问题,你现在只是窗体会更简单。
希望帮到你。
展开全部
新建个工程试一试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point MyOffset;//记录窗体位置
private void Form1_MouseDown(object sender, MouseEventArgs e)
{//通过拖动窗体的工作区来移动应用程序窗体
MyOffset = new Point(-e.X, -e.Y);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{//重置窗体位置
if( e.Button == MouseButtons.Left )
{
Point MyPos= Control.MousePosition;
MyPos.Offset(MyOffset.X, MyOffset.Y);
//this.Location =MyPos;
this.DesktopLocation = MyPos;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point MyOffset;//记录窗体位置
private void Form1_MouseDown(object sender, MouseEventArgs e)
{//通过拖动窗体的工作区来移动应用程序窗体
MyOffset = new Point(-e.X, -e.Y);
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{//重置窗体位置
if( e.Button == MouseButtons.Left )
{
Point MyPos= Control.MousePosition;
MyPos.Offset(MyOffset.X, MyOffset.Y);
//this.Location =MyPos;
this.DesktopLocation = MyPos;
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
javascript和webpart都可以实现!
代码比较复杂!
建议去google搜一下实例代码!
代码比较复杂!
建议去google搜一下实例代码!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询