c# 窗体上控件如何多选并且可移动 好比窗口上有2个按钮控件,我们知道,在设计状态下,可以同时选中这2个按钮并且移动他们。现在我想问,在程序运行状态下,如果达到同样效果。各位高手,给点意见,最好给个例程序给小弟我研究研究。邮箱:[email protected]谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Collections;using System.Text;using System.Windows.Forms;using System.Drawing;namespace testmovecontrol{class MoveableControl:Button{internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息internal static int WM_NCACTIVATE = 0x86;//窗体的激活状态发生改变的消息internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏internal static IntPtr HTLEFT = (IntPtr)10;//向左internal static IntPtr HTRIGHT = (IntPtr)11;//向右internal static IntPtr HTTOP = (IntPtr)12;//向上internal static IntPtr HTTOPLEFT = (IntPtr)13;//向左上internal static IntPtr HTTOPRIGHT = (IntPtr)14;//向右上internal static IntPtr HTBOTTOM = (IntPtr)15;//向下internal static IntPtr HTBOTTOMLEFT = (IntPtr)16;//向左下internal static IntPtr HTBOTTOMRIGHT = (IntPtr)17;//向右下private int m_BorderWidth = 4;private bool m_Sizeable = true;protected override void WndProc(ref Message m){if (m.Msg == WM_NCHITTEST){base.WndProc(ref m);if (DesignMode){return;}if (m.Result == HTCLIENT){m.HWnd = this.Handle;System.Drawing.Rectangle rect = this.RectangleToScreen(this.ClientRectangle);Point C_Pos = Cursor.Position;if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth) && this.m_Sizeable)m.Result = HTTOPLEFT;//左上else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth) && this.m_Sizeable)m.Result = HTTOPRIGHT;//右上else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)m.Result = HTBOTTOMLEFT;//左下else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)m.Result = HTBOTTOMRIGHT;//右下else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1) && this.m_Sizeable)m.Result = HTLEFT;//左else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && this.m_Sizeable)m.Result = HTRIGHT;//右else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1) && this.m_Sizeable)m.Result = HTTOP;//上else if ((C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)m.Result = HTBOTTOM;//下else{m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体}}return;}base.WndProc(ref m);}}} using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Drawing;namespace EastTicket.CustotmCol{ class MovePanel:Panel { bool whetherSelected = false; Point p = new Point(); public bool WhetherSelected { get { return whetherSelected; } } protected override void OnCreateControl() { base.OnCreateControl(); SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.DoubleBuffer, true); } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); whetherSelected = true; p.X = Cursor.Position.X; p.Y = Cursor.Position.Y; } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (whetherSelected == true) { this.Left = this.Left + (Cursor.Position.X - p.X); this.Top = this.Top + (Cursor.Position.Y - p.Y); p.X = Cursor.Position.X; p.Y = Cursor.Position.Y; } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); whetherSelected = false; this.BringToFront(); } }} 我邮箱是[email protected]谢谢啊 也要发给我哦.[email protected] 各位好心人,请帮帮忙,遇到分页上的一个问题 winform程序用post调用Java提供的接口问题 如何在aspx文件内使用JS的属性. 如何让两个程序同时访问一个数据库文件 求:人工智能算法 接口不是不可以实例化吗,那么IRingSequence rs又是什么意思啊? DataGridView排序后ErrorText消失 谁有C#开发的MP4? richTextBox里文字颜色切换的问题 征求C#的编码规范 【只剩下两个月的爱情】第一帖 请牛人帮我看看这个问题!
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.Drawing;namespace testmovecontrol
{
class MoveableControl:Button
{
internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
internal static int WM_NCACTIVATE = 0x86;//窗体的激活状态发生改变的消息internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏internal static IntPtr HTLEFT = (IntPtr)10;//向左
internal static IntPtr HTRIGHT = (IntPtr)11;//向右
internal static IntPtr HTTOP = (IntPtr)12;//向上
internal static IntPtr HTTOPLEFT = (IntPtr)13;//向左上
internal static IntPtr HTTOPRIGHT = (IntPtr)14;//向右上
internal static IntPtr HTBOTTOM = (IntPtr)15;//向下
internal static IntPtr HTBOTTOMLEFT = (IntPtr)16;//向左下
internal static IntPtr HTBOTTOMRIGHT = (IntPtr)17;//向右下private int m_BorderWidth = 4;
private bool m_Sizeable = true;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if (DesignMode)
{
return;
}
if (m.Result == HTCLIENT)
{
m.HWnd = this.Handle;System.Drawing.Rectangle rect = this.RectangleToScreen(this.ClientRectangle);
Point C_Pos = Cursor.Position;
if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth) && this.m_Sizeable)
m.Result = HTTOPLEFT;//左上
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth) && this.m_Sizeable)
m.Result = HTTOPRIGHT;//右上
else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)
m.Result = HTBOTTOMLEFT;//左下
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)
m.Result = HTBOTTOMRIGHT;//右下
else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1) && this.m_Sizeable)
m.Result = HTLEFT;//左
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && this.m_Sizeable)
m.Result = HTRIGHT;//右
else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1) && this.m_Sizeable)
m.Result = HTTOP;//上
else if ((C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)
m.Result = HTBOTTOM;//下
else
{
m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
}
}
return;
}
base.WndProc(ref m);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;namespace EastTicket.CustotmCol
{
class MovePanel:Panel
{
bool whetherSelected = false;
Point p = new Point();
public bool WhetherSelected
{
get { return whetherSelected; }
}
protected override void OnCreateControl()
{
base.OnCreateControl();
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
whetherSelected = true;
p.X = Cursor.Position.X;
p.Y = Cursor.Position.Y;
} protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (whetherSelected == true)
{
this.Left = this.Left + (Cursor.Position.X - p.X);
this.Top = this.Top + (Cursor.Position.Y - p.Y);
p.X = Cursor.Position.X;
p.Y = Cursor.Position.Y; } } protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
whetherSelected = false;
this.BringToFront();
}
}
}
谢谢啊
[email protected]