110,533
社区成员
发帖
与我相关
我的任务
分享
private void pbMet_MouseUp(object sender, MouseEventArgs e)
{
if (this.create_Meter)
{
Meter lmet = null;
lmet = new Meter();
lmet.Name = "Meter" + MetCount;
lmet.Width = 130;
lmet.Height = 130;
lmet.Location = new Point(e.X - 108, e.Y + 30);
lmet.MouseDown += new System.Windows.Forms.MouseEventHandler(lmet_MouseDown);
lmet.MouseMove += new System.Windows.Forms.MouseEventHandler(lmet_MouseMove);
lmet.MouseUp += new System.Windows.Forms.MouseEventHandler(lmet_MouseUp);
lmet.MouseLeave += new EventHandler(MyMouseLeave);
lmet.Enter += new EventHandler(lmet_Enter);
lmet.KeyDown += new KeyEventHandler(MykeyDown);
lmet.KeyUp += new KeyEventHandler(MykeyUp);
this.panel1.Controls.Add(lmet);
this.listMet.Add(lmet);
this.create_Meter = false;
MetCount++;
this.PreventMantle(lmet);
this.comboBox1.Items.Add(lmet.Name);
this.comboBox1.SelectedItem = lmet.Name;
this.propertyGrid1.SelectedObject = lmet;
lmet.ContextMenuStrip = this.cmsCn;
}
}
private void lmet_MouseDown(object sender, MouseEventArgs e)
{
p.X = e.X;
p.Y = e.Y;
p1.X = e.X;
p1.Y = e.Y;
Meter lmet = (Meter)sender;
this.comboBox1.SelectedItem = lmet.Name;
this.propertyGrid1.Visible = true;
this.propertyGrid1.SelectedObject = lmet;
}
private void lmet_MouseMove(object sender, MouseEventArgs e)
{
Meter met1 = (Meter)sender;
Control lCtrl = (sender as Control);
if (e.Button == MouseButtons.Left)
{
switch (m_MousePointPosition)
{
case EnumMousePointPosition.MouseDrag:
{
lCtrl.Left = lCtrl.Left + e.X - p.X;
lCtrl.Top = lCtrl.Top + e.Y - p.Y;
break;
}
case EnumMousePointPosition.MouseSizeBottom:
{
lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
}
case EnumMousePointPosition.MouseSizeBottomRight://右下
{
lCtrl.Width = lCtrl.Width + e.X - p1.X;
lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
}
case EnumMousePointPosition.MouseSizeRight:
{
lCtrl.Width = lCtrl.Width + e.X - p1.X;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
}
case EnumMousePointPosition.MouseSizeTop:
{
lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
break;
}
case EnumMousePointPosition.MouseSizeLeft:
{
lCtrl.Left = lCtrl.Left + e.X - p.X;
lCtrl.Width = lCtrl.Width - (e.X - p.X);
break;
}
case EnumMousePointPosition.MouseSizeBottomLeft://左下
{
lCtrl.Left = lCtrl.Left + e.X - p1.X;
lCtrl.Width = lCtrl.Width - (e.X - p1.X);
lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
}
case EnumMousePointPosition.MouseSizeTopRight://右上
{
lCtrl.Top = lCtrl.Top + (e.Y - p1.Y);
lCtrl.Width = lCtrl.Width + (e.X - p1.X);
lCtrl.Height = lCtrl.Height - (e.Y - p1.Y);
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
}
case EnumMousePointPosition.MouseSizeTopLeft://左上
{
lCtrl.Left = lCtrl.Left + e.X - p.X;
lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
lCtrl.Width = lCtrl.Width - (e.X - p.X);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
break;
}
default:
break;
}
if (lCtrl.Width < BtnMinWidth)
{
lCtrl.Width = BtnMinWidth;
}
if (lCtrl.Height < BtnMinHeight)
{
lCtrl.Height = BtnMinHeight;
}
}
else
{
m_MousePointPosition = MousePointPosition(lCtrl.Size, e);//'判断光标的位置状态
switch (m_MousePointPosition) //'改变光标
{
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;
}
}
}
private void lmet_MouseUp(object sender, MouseEventArgs e)
{
Meter lmet = (Meter)sender;
if (lmet.Width > this.panel1.Width)
{
lmet.Location = new Point(5, lmet.Location.Y);
lmet.Width = this.panel1.Width - 100;
}
this.PreventMantle(lmet);
if (this.blChoose)
{
if (lmet.ForeColor != Color.CornflowerBlue)
{
lmet.ForeColor = Color.CornflowerBlue;
this.cmsCn.Items[0].Enabled = true;
}
else
{
lmet.ForeColor = Color.Black;
this.cmsCn.Items[0].Enabled = false;
}
}
}
public Form1()
{
InitializeComponent();
}
private void user_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseDown");
}
private void user_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseMove");
}
private void user_MouseUp(object sender, MouseEventArgs e)
{
Console.WriteLine("MouseUp");
}
private void pbMet_MouseUp(object sender, MouseEventArgs e)
{
Meter lmet = null;
lmet = new Meter();
lmet.Name = "Meter" + 1;
lmet.Width = 400;
lmet.Height = 300;
lmet.Location = new Point(0, 0);
lmet.MouseDown += new System.Windows.Forms.MouseEventHandler(user_MouseDown);
lmet.MouseMove += new System.Windows.Forms.MouseEventHandler(user_MouseMove);
lmet.MouseUp += new System.Windows.Forms.MouseEventHandler(user_MouseUp);
this.panel1.Controls.Add(lmet);
}
这是我测试代码,不知道是不是有什么没有模拟出来,但是我这里的output窗体会有输出,程序运行正确。
界面就两个panel
你也可以自己新建个项目测试下
OnMouseUp(e);
//显示当前压力
public void DrawString(Graphics g)
{
int cer = _diameter / 2;
//F0表示浮点数小数点后面没有数字 F2表示精确到小数点后2位
string str = _changeValue.ToString("F0");
if (Convert.ToInt16(str) >= safeValue)
{
safeColor = Color.Red;
}
else
{
safeColor = Color.Green;
}
g.DrawString(str, new Font("宋体", typeSize), new SolidBrush(safeColor), new PointF(cer, (float)(cer + cer * 0.3)), strFormat);
}
//框架
//背景
public void DrawBackImg()
{
Bitmap bit = new Bitmap(this.Width, this.Height);
Graphics gp = Graphics.FromImage(bit);
gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (_isDrawBackGround)
{
//背景渐变色
drawBachground(gp);
}
//外框 一个圆圈
drawFrame(gp);
// 画刻度
DrawRuling(gp);
//单位
DrawUnitStr(gp);
//画点
//drawPoint(gp);
this.BackgroundImage = bit;
}
//单位
public void DrawUnitStr(Graphics gp)
{
int cer = _diameter / 2;
gp.DrawString(_unitStr, new Font("宋体", 10), new SolidBrush(_frameColor), new PointF(cer, (float)(cer - cer * 0.3)), strFormat);
}
/// <summary>
/// 画外框
/// </summary>
/// <param name="gp"></param>
public void drawFrame(Graphics gp)
{
Pen p = new Pen(Color.Black, 3);
Rectangle rec = new Rectangle(5, 5, _diameter - 10, _diameter - 10);
//SolidBrush sdBrush2 = new SolidBrush(Color.Black);
//gp.DrawEllipse(p, rec);
//gp.FillEllipse(sdBrush2, 5, 5, _diameter - 10, _diameter - 10);
Pen pgreen = new Pen(Color.Green, 2);
Pen pred = new Pen(Color.Red, 2);
gp.DrawArc(pgreen, rec, sAngleGreen, sweepAngleGreen);
gp.DrawArc(pred, rec, sAngleRed, 360 - sweepAngleGreen);
//引用的图片
/*Rectangle rec = new Rectangle(0, 0, _diameter, _diameter);
TextureBrush pic = new TextureBrush(Image.FromFile(@"f:\picture\121-5.png"));
gp.FillRectangle(pic, rec);*/
}
/// <summary>
/// 画刻度
/// </summary>
/// <param name="gp"></param>
public void DrawRuling(Graphics gp)
{
//刻度
bool isGreen = true;
int cerX = _diameter / 2;
int cerY = _diameter / 2;
int dx = 0;
int dy = 0;
//刻度长度
int soildLenght = 8;
//刻度下数字位置
float start = 135;
float sweepShot = 0;
float span = (float)(_maxValue / 30);
float sp = 0;
//刻度颜色
Pen linePen = new Pen(_frameColor, 1);
//用于右边数字右对齐
StringFormat stf = new StringFormat();
stf.Alignment = StringAlignment.Far;
StringFormat stfMid = new StringFormat();
stfMid.Alignment = StringAlignment.Center;
stfMid.LineAlignment = StringAlignment.Center;
for (int i = 0; i <= 30; i++)
{
double rad = (sweepShot + start) * Math.PI / 180;
float radius = _diameter / 2 - 5;
int px = (int)(cerX + radius * Math.Cos(rad));
int py = (int)(cerY + radius * Math.Sin(rad));
if (sweepShot % 5 == 0)
{
linePen.Width = 2;
dx = (int)(cerX + (radius - soildLenght) * Math.Cos(rad));
dy = (int)(cerY + (radius - soildLenght) * Math.Sin(rad));
string str = sp.ToString("f0");
if (sweepShot <= 45)
{
gp.DrawString(str, new Font("宋体", MarkSize), new SolidBrush(Color.Green), new PointF(dx, dy - 5));
isGreen = true;
}
//else if (sweepShot == 90)
else if (sweepShot > 45 && sweepShot < 135)
{
gp.DrawString(str, new Font("宋体", MarkSize), new SolidBrush(Color.Green), new PointF(dx, dy));
isGreen = true;
}
else if (sweepShot == 135)
{
//仪表盘正上方
gp.DrawString(str, new Font("宋体", MarkSize), new SolidBrush(Color.Green), new PointF(dx, dy + 10), stfMid);
isGreen = true;
}
else if (sweepShot > 135 && sweepShot < 225)
{
//仪表盘第五个数字
gp.DrawString(str, new Font("宋体", MarkSize), new SolidBrush(Color.Red), new PointF(dx, dy), stf);
isGreen = false;
}
else if (sweepShot >= 225)
{
//仪表盘第六第七个
gp.DrawString(str, new Font("宋体", MarkSize), new SolidBrush(Color.Red), new PointF(dx, dy - 5), stf);
isGreen = false;
}
}
else
{
linePen.Width = 1;
dx = (int)(cerX + (radius - soildLenght + 2) * Math.Cos(rad));
dy = (int)(cerY + (radius - soildLenght + 2) * Math.Sin(rad));
}
if (isGreen)
{
Pen p = new Pen(Color.Green, 1);
gp.DrawLine(p, new Point(px, py), new Point(dx, dy));
sp += span;
sweepShot += 9;
}
else
{
Pen p = new Pen(Color.Red, 1);
gp.DrawLine(p, new Point(px, py), new Point(dx, dy));
sp += span;
sweepShot += 9;
}
}
}
//画中间的点
public void drawPoint(Graphics gp)
{
Color circlrColor = Color.Black;
Pen pG = new Pen(Color.Green);
Pen pR = new Pen(Color.Red);
int tmpWidth = 7;
int px = _diameter / 2 - tmpWidth;
float sweepShot = (float)(_changeValue / _maxValue * 270.0);
//总共6等分的sweepShot=0的时候就是仪表盘上的0
//然后依次加45就是第二个刻度数,总共270,正上方为135
if (sweepShot < 180)
{
gp.DrawEllipse(pG, new Rectangle(px, px, 2 * tmpWidth, 2 * tmpWidth));
gp.FillEllipse(new SolidBrush(Color.Green), new Rectangle(px + 2, px + 2, 2 * tmpWidth - 4, 2 * tmpWidth - 4));
//gp.FillEllipse(new SolidBrush(Color.Green), new Rectangle(px, px, 2 * tmpWidth, 2 * tmpWidth));
}
else
{
gp.DrawEllipse(pR, new Rectangle(px, px, 2 * tmpWidth, 2 * tmpWidth));
gp.FillEllipse(new SolidBrush(Color.Red), new Rectangle(px + 2, px + 2, 2 * tmpWidth - 4, 2 * tmpWidth - 4));
//gp.FillEllipse(new SolidBrush(Color.Red), new Rectangle(px, px, 2 * tmpWidth, 2 * tmpWidth));
}
//gp.FillEllipse(new SolidBrush(Color.Red), new Rectangle(px, px, 2 * tmpWidth, 2 * tmpWidth));
//gp.DrawRectangle(p, new Rectangle(px, px, 2 * tmpWidth, 2 * tmpWidth));
}
// 设置为双缓冲,防止刷新时闪烁
public void EnableDoubleBuffering()
{
// Set the value of the double-buffering style bits to true.
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();
}
//画背景渐变色
public void drawBachground(Graphics gp)
{
GraphicsPath path = new GraphicsPath();
path.AddEllipse(3, 3, _diameter - 6, _diameter - 6);
PathGradientBrush pthGrBrush = new PathGradientBrush(path);
pthGrBrush.CenterColor = _centerColor;
Color[] colors = { _borderColor };
pthGrBrush.SurroundColors = colors;
gp.FillEllipse(pthGrBrush, 3, 3, _diameter - 6, _diameter - 6);
}
public void pic_Paint(object sender, PaintEventArgs e)
{
DrawForeImg(e.Graphics);
}
}
这是控件的全部代码,我就添加了一个pic_Paint事件,鼠标的没添加,我不知道该怎么操作呀,求助大侠~namespace MyMeter
{
public partial class Meter : UserControl
{
string _unitStr = "";
int _diameter = 130;
int _PinLen = 50;
int safeValue = 80;
int typeSize = 12;
int markSize = 10;
float sAngleGreen = 45;
float sAngleRed = 315;
float sweepAngleGreen = 270;
double _maxValue = 120;
double _changeValue = 0;
Color safeColor = Color.Green;
Color _pinColor = Color.Black;
Color _frameColor = Color.Black;
StringFormat strFormat = new StringFormat();
public Meter()
{
InitializeComponent();
UpdateStyles();
DrawBackImg();
pic.Invalidate();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
}
//直径
public int DiaMeter
{
get { return _diameter; }
set
{
_diameter = value - 1;
this.Width = this.Height = value;
DrawBackImg();
pic.Invalidate();
}
}
//最大值
public double MaxValue
{
get { return _maxValue; }
set
{
_maxValue = value;
DrawBackImg();
}
}
public double ChangeValue
{
get { return _changeValue; }
set
{
if (value > _maxValue)
{
_maxValue = value;
}
this.tmChangvalue = value;
startDraw();
}
}
//针的长度
public int PinLen
{
get { return _PinLen; }
set { _PinLen = value; pic.Invalidate(); }
}
public int NxPinLen
{
get { return (int)(_PinLen / 5); }
}
public Color PinColor
{
get { return _pinColor; }
set { _pinColor = value; pic.Invalidate(); DrawBackImg(); }
}
public Color FrameColor
{
get { return _frameColor; }
set { _frameColor = value; DrawBackImg(); }
}
public string UnitStr
{
get { return _unitStr; }
set { _unitStr = value; DrawBackImg(); pic.Invalidate(); }
}
public int SafeValue
{
get { return safeValue; }
set
{
safeValue = value;
startDraw();
}
}
public Color SafeColor
{
get { return safeColor; }
set
{
safeColor = value;
DrawBackImg();
pic.Invalidate();
}
}
//字体大小
public int TypeSize
{
get { return typeSize; }
set
{
typeSize = value;
}
}
public int MarkSize
{
get { return markSize; }
set
{
markSize = value;
}
}
public float startAngleGreen
{
get { return sAngleGreen; }
set
{
sAngleGreen = value;
DrawBackImg();
}
}
public float startAngleRed
{
get { return sAngleRed; }
set
{
sAngleRed = value;
DrawBackImg();
}
}
public float SweepAngleGreen
{
get { return sweepAngleGreen; }
set
{
sweepAngleGreen = value;
DrawBackImg();
}
}
//背景颜色
public bool _isDrawBackGround = false;
public bool IsDrawBackGround
{
get { return _isDrawBackGround; }
set { _isDrawBackGround = value; DrawBackImg(); }
}
//渐变颜色
public Color _centerColor = Color.White;
public Color CenterColor
{
get { return _centerColor; }
set { _centerColor = value; DrawBackImg(); }
}
//外部颜色
public Color _borderColor = Color.White;
public Color BorderColor
{
get { return _borderColor; }
set { _borderColor = value; DrawBackImg(); }
}
public void Clear()
{
_changeValue = 0;
this.Invalidate();
}
double tmChangvalue = 0;
double valueSpan = 0.05;
public void startDraw()
{
Thread threadDraw = new Thread(new ThreadStart(drawTrhead));
threadDraw.IsBackground = false;
threadDraw.Start();
}
public void drawTrhead()
{
//改变量
double tmp = tmChangvalue - _changeValue;
if (tmp > 0)
{
for (double j = 0; j <= tmp; )
{
j += valueSpan;
if (_changeValue + valueSpan < _maxValue)
{
_changeValue += valueSpan;
}
Thread.Sleep(1);
this.Invalidate();
}
}
else
{
for (double j = 0; j < -tmp; )
{
j += valueSpan;
if (_changeValue - valueSpan > 0)
{
_changeValue -= valueSpan;
}
Thread.Sleep(1);
this.Invalidate();
}
}
}
public void DrawForeImg(Graphics gp)
{
Bitmap bit = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bit);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//画针
DrawPin(g);
//画点
drawPoint(g);
//显示示数
DrawString(g);
gp.DrawImage(bit, new Point(0, 0));
g.Dispose();
}
//画针
public void DrawPin(Graphics g)
{
int cer = _diameter / 2;
float start = 135;
float sweepShot = (float)(_changeValue / _maxValue * 270.0);
//float sweepShot = (float)(55 / _maxValue * 270.0);
Pen lineGPen = new Pen(Color.Green, 1);
Pen NxGPen = new Pen(Color.Green, 3);
Pen xGPen = new Pen(Color.Green, 5);
Pen lineRPen = new Pen(Color.Red, 1);
Pen NxRPen = new Pen(Color.Red, 3);
Pen xRPen = new Pen(Color.Red, 5);
double rad = (sweepShot + start) * Math.PI / 180;
//float radius = _diameter / 2 - 5;
int dx = (int)(cer + (_PinLen) * Math.Cos(rad));
int dy = (int)(cer + (_PinLen) * Math.Sin(rad));
int px = (int)(cer + (_PinLen * 0.4) * Math.Cos(rad));
int py = (int)(cer + (_PinLen * 0.4) * Math.Sin(rad));
int nx = (int)(cer - (NxPinLen) * Math.Sin(rad));
int ny = (int)(cer - (NxPinLen) * Math.Cos(rad));
/*SolidBrush brushRed = new SolidBrush(Color.Red);
SolidBrush brushGreen = new SolidBrush(Color.Green);
//b1x=68
int b1x = (int)(cer - 5 * Math.Cos(rad));
//b1y=62
int b1y = (int)(cer - 4 * Math.Sin(rad));
int b2x = (int)(cer + 4 * Math.Cos(rad));
int b2y = (int)(cer + 5 * Math.Sin(rad));
PointF point1 = new PointF(b2y, b1x);
PointF point2 = new PointF(b1y, b2x);
PointF point3 = new PointF(dx, dy);
PointF[] curvePoints = { point1, point2, point3 };*/
//总共6等分的sweepShot=0的时候就是仪表盘上的0
//然后依次加45就是第二个刻度数,总共270,正上方为135
if (sweepShot < 180)
{
g.DrawLine(lineGPen, new Point(cer, cer), new Point(dx, dy));
g.DrawLine(NxGPen, new Point(cer, cer), new Point(px, py));
g.DrawLine(xGPen, new Point(cer, cer), new Point(ny, nx));
//g.FillPolygon(brushGreen, curvePoints);
}
else
{
g.DrawLine(lineRPen, new Point(cer, cer), new Point(dx, dy));
g.DrawLine(NxRPen, new Point(cer, cer), new Point(px, py));
g.DrawLine(xRPen, new Point(cer, cer), new Point(ny, nx));
//g.FillPolygon(brushRed, curvePoints);
}
//设置清理缓存的
//EnableDoubleBuffering();
}