142 lines
5.5 KiB
C#
142 lines
5.5 KiB
C#
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;
|
|
|
|
|
|
namespace NewMyFormDesigner
|
|
{
|
|
/// <summary>
|
|
/// 设计面板
|
|
/// </summary>
|
|
public partial class DesignerControl : UserControl
|
|
{
|
|
public HostFrame _hostFrame; //所有控件的容器
|
|
public Overlayer _overlayer; //遮罩层
|
|
|
|
bool MouseIsDown = false;
|
|
Rectangle MouseRect = Rectangle.Empty;
|
|
|
|
public DesignerControl()
|
|
{
|
|
InitializeComponent();
|
|
_hostFrame = new HostFrame();
|
|
_hostFrame.TopLevel = false; //当做子控件添加到设计面板
|
|
_hostFrame.Location = new Point(10, 10);
|
|
_hostFrame.Text = "商品表单设计";
|
|
Controls.Add(_hostFrame);
|
|
_hostFrame.Show();
|
|
_overlayer = new Overlayer(_hostFrame);
|
|
_overlayer.Location = new Point(0, 0);
|
|
_overlayer.Dock = DockStyle.Fill;
|
|
Controls.Add(_overlayer);
|
|
_overlayer.Show();
|
|
_overlayer.BringToFront(); //将其设置Z轴最上,接受所有的用户操作,底层的其他控件无法接受用户输入。各位可以注释这条试一下,底层的其他控件(窗体)就能接受鼠标点击
|
|
Application.AddMessageFilter(new MessageFilter(_hostFrame, this)); // 过滤控件容器中所有子控件的WM_PAINT消息 减少重绘操作
|
|
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
|
|
|
|
this.MouseDown += new MouseEventHandler(frmMain_MouseDown);
|
|
this.MouseMove += new MouseEventHandler(frmMain_MouseMove);
|
|
this.MouseUp += new MouseEventHandler(frmMain_MouseUp);
|
|
}
|
|
|
|
void frmMain_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
this.Capture = false;
|
|
Cursor.Clip = Rectangle.Empty;
|
|
MouseIsDown = false;
|
|
DrawRectangle();
|
|
|
|
foreach (Control ct in this.Controls)
|
|
{
|
|
// Point ctrlCenter = new Point(ct.Location.X + ct.Width / 2, ct.Location.Y + ct.Height / 2);
|
|
|
|
//if (ctrlCenter.X > MouseRect.Location.X
|
|
// && ctrlCenter.X < MouseRect.Location.X + MouseRect.Width
|
|
// && ctrlCenter.Y > MouseRect.Location.Y && ctrlCenter.Y > MouseRect.Location.Y + MouseRect.Height)
|
|
if (MouseRect.Contains(ct.Location))
|
|
{
|
|
if (ct is CheckBox)
|
|
{
|
|
if (((CheckBox)ct).Checked) //值选中
|
|
{
|
|
((CheckBox)ct).Checked = false;
|
|
}
|
|
else
|
|
{
|
|
((CheckBox)ct).Checked = true;
|
|
}
|
|
//控件选中
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
MouseRect = Rectangle.Empty;
|
|
}
|
|
void frmMain_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
|
|
if (MouseIsDown)
|
|
ResizeToRectangle(e.Location);
|
|
}
|
|
void frmMain_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
MouseIsDown = true;
|
|
DrawStart(e.Location);
|
|
|
|
}
|
|
private void ResizeToRectangle(Point p)
|
|
{
|
|
DrawRectangle();
|
|
MouseRect.Width = p.X - MouseRect.Left;
|
|
MouseRect.Height = p.Y - MouseRect.Top;
|
|
DrawRectangle();
|
|
}
|
|
private void DrawRectangle()
|
|
{
|
|
Rectangle rect = this.RectangleToScreen(MouseRect);
|
|
ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
|
|
}
|
|
private void DrawStart(Point StartPoint)
|
|
{
|
|
this.Capture = true;
|
|
Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
|
|
MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
|
|
}
|
|
|
|
public DesignerControl(int Width, int Height)
|
|
{
|
|
_hostFrame = new HostFrame();
|
|
_hostFrame.TopLevel = false; //当做子控件添加到设计面板
|
|
_hostFrame.Location = new Point(10, 10);
|
|
_hostFrame.Height = Height;
|
|
_hostFrame.Width = Width;
|
|
_hostFrame.Text = "表单设计";
|
|
Controls.Add(_hostFrame);
|
|
_hostFrame.Show();
|
|
|
|
_overlayer = new Overlayer(_hostFrame);
|
|
_overlayer.Location = new Point(0, 0);
|
|
_overlayer.Dock = DockStyle.Fill;
|
|
Controls.Add(_overlayer);
|
|
_overlayer.Show();
|
|
_overlayer.BringToFront(); //将其设置Z轴最上,接受所有的用户操作,底层的其他控件无法接受用户输入。各位可以注释这条试一下,底层的其他控件(窗体)就能接受鼠标点击
|
|
Application.AddMessageFilter(new MessageFilter(_hostFrame, this)); // 过滤控件容器中所有子控件的WM_PAINT消息 减少重绘操作
|
|
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
|
|
|
|
this.MouseDown += new MouseEventHandler(frmMain_MouseDown);
|
|
this.MouseMove += new MouseEventHandler(frmMain_MouseMove);
|
|
this.MouseUp += new MouseEventHandler(frmMain_MouseUp);
|
|
}
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
|
{
|
|
return base.ProcessCmdKey(ref msg, keyData);
|
|
}
|
|
}
|
|
}
|