博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#winform自定义控件模拟设计时界面鼠标移动和调节大小、选中效果
阅读量:4540 次
发布时间:2019-06-08

本文共 15249 字,大约阅读时间需要 50 分钟。

要想玩转Winform自定义控件需要对GDI+非常熟悉,对常用的控件有一些了解,好选择合适的基类控件来简化。

要点说明及代码

1)定义接口:

using System;using System.Windows.Forms;namespace GDIPrinterDriver{    ///     /// 模板元素接口    ///     public interface ILabelDesignElement    {        ///         /// PrintData/codeContext里的字段,{} []         ///         string 动态内容 { get; set; }        ///         /// 是否被选中        ///         bool DesignSelected { get; set; }        ///         /// 选择状态发生改变        ///         event Action
SelectedStatusChange; ///
/// 本控件被选中时键盘方向键被按下 /// ///
void KeysChangedLocation(Keys keyData); }}

2)控件基类实现:

using GDIPrinterDriver;using System;using System.ComponentModel;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.Windows.Forms;namespace GDILabelDesigner{    ///     /// 设计时控件基类    ///     public abstract class DesignCellControl : PictureBox, ILabelDesignElement    {        #region 鼠标移动和缩放        private enum EnumMousePointPosition        {            MouseSizeNone = 0, //'无                MouseSizeRight = 1, //'拉伸右边框                MouseSizeLeft = 2, //'拉伸左边框                MouseSizeBottom = 3, //'拉伸下边框                MouseSizeTop = 4, //'拉伸上边框                MouseSizeTopLeft = 5, //'拉伸左上角                MouseSizeTopRight = 6, //'拉伸右上角                MouseSizeBottomLeft = 7, //'拉伸左下角                MouseSizeBottomRight = 8, //'拉伸右下角                MouseDrag = 9   // '鼠标拖动            }        const int Band = 5;        const int MinWidth = 10;        const int MinHeight = 10;        private EnumMousePointPosition m_MousePointPosition;        private Point p, p1;        private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)        {            if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))            {                if (e.X < Band)                {                    if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; }                    else                    {                        if (e.Y > -1 * Band + size.Height)                        { return EnumMousePointPosition.MouseSizeBottomLeft; }                        else                        { return EnumMousePointPosition.MouseSizeLeft; }                    }                }                else                {                    if (e.X > -1 * Band + size.Width)                    {                        if (e.Y < Band)                        { return EnumMousePointPosition.MouseSizeTopRight; }                        else                        {                            if (e.Y > -1 * Band + size.Height)                            { return EnumMousePointPosition.MouseSizeBottomRight; }                            else                            { return EnumMousePointPosition.MouseSizeRight; }                        }                    }                    else                    {                        if (e.Y < Band)                        { return EnumMousePointPosition.MouseSizeTop; }                        else                        {                            if (e.Y > -1 * Band + size.Height)                            { return EnumMousePointPosition.MouseSizeBottom; }                            else                            { return EnumMousePointPosition.MouseDrag; }                        }                    }                }            }            else            { return EnumMousePointPosition.MouseSizeNone; }        }        #endregion        public bool DesignSelected        {            get            {                return designSelected;            }            set            {                designSelected = value;                Invalidate();            }        }        private bool designSelected = false;        public DynamicMapProperty DynamicMapProperty { get; set; }        public StaticMapProperty StaticMapProperty { get; set; }        public string 动态内容 { get; set; }        public RoteDescription RoteDescription { get; set; }        ///         /// 被选中,获取到焦点的事件        ///         public event Action
SelectedStatusChange; protected override void OnClick(EventArgs e) { DesignSelected = true; SelectedStatusChange?.Invoke(this, DesignSelected); } protected override void OnMouseWheel(MouseEventArgs e) { base.OnMouseWheel(e); double d = 1.068D; if (e.Delta > 0 && DesignSelected) { this.Size = new Size((int)(this.Size.Width * d), (int)(this.Size.Height * d)); } else { this.Size = new Size((int)(this.Size.Width / d), (int)(this.Size.Height / d)); } } protected override void OnMouseDown(MouseEventArgs e) { p.X = e.X; p.Y = e.Y; p1.X = e.X; p1.Y = e.Y; } protected override void OnMouseUp(MouseEventArgs e) { m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; this.Cursor = Cursors.Arrow; } protected override void OnMouseMove(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { switch (m_MousePointPosition) { #region 位置计算 case EnumMousePointPosition.MouseDrag: Left = Left + e.X - p.X; Top = Top + e.Y - p.Y; break; case EnumMousePointPosition.MouseSizeBottom: Height = Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点     break; case EnumMousePointPosition.MouseSizeBottomRight: Width = Width + e.X - p1.X; Height = Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点     break; case EnumMousePointPosition.MouseSizeRight: Width = Width + e.X - p1.X; Height = Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点     break; case EnumMousePointPosition.MouseSizeTop: Top = Top + (e.Y - p.Y); Height = Height - (e.Y - p.Y); break; case EnumMousePointPosition.MouseSizeLeft: Left = Left + e.X - p.X; Width = Width - (e.X - p.X); break; case EnumMousePointPosition.MouseSizeBottomLeft: Left = Left + e.X - p.X; Width = Width - (e.X - p.X); Height = Height + e.Y - p1.Y; p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点     break; case EnumMousePointPosition.MouseSizeTopRight: Top = Top + (e.Y - p.Y); Width = Width + (e.X - p1.X); Height = Height - (e.Y - p.Y); p1.X = e.X; p1.Y = e.Y; //'记录光标拖动的当前点     break; case EnumMousePointPosition.MouseSizeTopLeft: Left = Left + e.X - p.X; Top = Top + (e.Y - p.Y); Width = Width - (e.X - p.X); Height = Height - (e.Y - p.Y); break; default: break; #endregion } if (Width < MinWidth) Width = MinWidth; if (Height < MinHeight) Height = MinHeight; if (Tag != null) { if (Tag is ImageElementNode) { var tag = Tag as ImageElementNode; tag.Location = Location; } else if (Tag is BarcodeElementNode) { var tag = Tag as BarcodeElementNode; tag.Location = Location; } else if (Tag is TextBoxElementNode) { var tag = Tag as TextBoxElementNode; tag.Location = Location; } } } else { m_MousePointPosition = MousePointPosition(Size, e); switch (m_MousePointPosition) { #region 改变光标 case EnumMousePointPosition.MouseSizeNone: this.Cursor = Cursors.Arrow; //'箭头     break; case EnumMousePointPosition.MouseDrag: this.Cursor = Cursors.SizeAll; //'四方向     break; case EnumMousePointPosition.MouseSizeBottom: this.Cursor = Cursors.SizeNS; //'南北     break; case EnumMousePointPosition.MouseSizeTop: this.Cursor = Cursors.SizeNS; //'南北     break; case EnumMousePointPosition.MouseSizeLeft: this.Cursor = Cursors.SizeWE; //'东西     break; case EnumMousePointPosition.MouseSizeRight: this.Cursor = Cursors.SizeWE; //'东西     break; case EnumMousePointPosition.MouseSizeBottomLeft: this.Cursor = Cursors.SizeNESW; //'东北到南西     break; case EnumMousePointPosition.MouseSizeBottomRight: this.Cursor = Cursors.SizeNWSE; //'东南到西北     break; case EnumMousePointPosition.MouseSizeTopLeft: this.Cursor = Cursors.SizeNWSE; //'东南到西北     break; case EnumMousePointPosition.MouseSizeTopRight: this.Cursor = Cursors.SizeNESW; //'东北到南西     break; default: break; #endregion } } } ///
/// 绘制方框 /// ///
protected void DrawSelectedStatus(Graphics g) { Rectangle rect = ClientRectangle; rect.Inflate(-6, -6); using (Pen p = new Pen(Brushes.Black, 1)) { p.DashStyle = DashStyle.Dot; p.DashStyle = DashStyle.Solid; //8个方块 g.FillRectangle(Brushes.White, new Rectangle(rect.Left - 4, rect.Top - 4, 4, 4)); g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top - 4, 4, 4)); g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width, rect.Top - 4, 4, 4)); g.FillRectangle(Brushes.White, new Rectangle(rect.Left - 4, rect.Top + rect.Height / 2 - 3, 4, 4)); g.FillRectangle(Brushes.White, new Rectangle(rect.Left - 4, rect.Top + rect.Height, 4, 4)); g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height / 2 - 3, 4, 4)); g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top + rect.Height, 4, 4)); g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left - 4, rect.Top - 4, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top - 4, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width, rect.Top - 4, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left - 4, rect.Top + rect.Height / 2 - 3, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left - 4, rect.Top + rect.Height, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height / 2 - 3, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top + rect.Height, 4, 4)); g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height, 4, 4)); } } ///
/// 旋转图片 /// ///
源图 ///
角度 ///
背景色 ///
protected Bitmap ImageRotate(Bitmap bmp, float angle, Color bkColor) { int w = bmp.Width + 2; int h = bmp.Height + 2; PixelFormat pf; if (bkColor == Color.Transparent) { pf = PixelFormat.Format32bppArgb; } else { pf = bmp.PixelFormat; } Bitmap tmp = new Bitmap(w, h, pf); Graphics g = Graphics.FromImage(tmp); g.Clear(bkColor); g.DrawImageUnscaled(bmp, 1, 1); g.Dispose(); GraphicsPath path = new GraphicsPath(); path.AddRectangle(new RectangleF(0f, 0f, w, h)); Matrix mtrx = new Matrix(); mtrx.Rotate(angle); RectangleF rct = path.GetBounds(mtrx); Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf); g = Graphics.FromImage(dst); g.Clear(bkColor); g.TranslateTransform(-rct.X, -rct.Y); g.RotateTransform(angle); g.InterpolationMode = InterpolationMode.HighQualityBilinear; g.DrawImageUnscaled(tmp, 0, 0); g.Dispose(); tmp.Dispose(); return dst; } ///
/// 响应键盘光标键 /// ///
public virtual void KeysChangedLocation(Keys keyData) { if (keyData == Keys.Up) { Top -= 1; } if (keyData == Keys.Down) { Top += 1; } if (keyData == Keys.Left) { Left -= 1; } if (keyData == Keys.Right) { Left += 1; } if (Tag != null) { if (Tag is ImageElementNode) { var tag = Tag as ImageElementNode; tag.Location = Location; } else if (Tag is BarcodeElementNode) { var tag = Tag as BarcodeElementNode; tag.Location = Location; } else if (Tag is TextBoxElementNode) { var tag = Tag as TextBoxElementNode; tag.Location = Location; } } } }}

 

转载于:https://www.cnblogs.com/datacool/p/datacool_2017_gdi.html

你可能感兴趣的文章
纪录jmeter loop controller 使用中的一个坑
查看>>
spring读取配置文件,且获取bean实例
查看>>
Xcode7 免证书真机测试
查看>>
史上最简单MySQL教程详解(基础篇)之数据类型
查看>>
802.11 帧封装细节
查看>>
【BZOJ2739】—最远点(决策单调性+分治)
查看>>
微信开发-ACCESS TOKEN 过期失效解决方案
查看>>
转:Python自省(反射)指南
查看>>
Python 流程控制:for
查看>>
我的轮播练习
查看>>
js中index()的四种经典用法111
查看>>
flask 基本配置和参数解释
查看>>
HDMI转EDP芯片NCS8803简介
查看>>
Git查看、删除、重命名远程分支和tag
查看>>
nexus4/5/6/7/9/10设备谷歌安卓5.1.1系统底包下载
查看>>
子界类型的应用
查看>>
ubuntu系统中查看本机cpu和内存信息的命令和用法
查看>>
es6 箭头函数
查看>>
python装饰器的作用
查看>>
[bzoj2510]弱题 (循环矩阵优化dp)
查看>>