请教下,鼠标事件里怎么让鼠标在绘制的仪表上移动,仪表指针也会随着旋转啊,请求指导。。。谢谢
我是在图片上获取鼠标的位置的,把这横轴范围映射到对应0到100的范围里,发现这样会出现误差。。。;有好点的实现方法吗
你看看这个是否符合你需要
win10 uwp 车表盘 径向规
按照盘子的形状调整长度(圆上的点),资源下载这里省事就按鼠标位置画了个线,就这么个意思了
public
Form1()
{
InitializeComponent();
bool
press =
false
;
Point[] line_point =
new
Point[] {
new
Point(100, 100),
new
Point(100, 20) };
Panel panel =
new
Panel()
{
Size =
new
Size(200, 200),
Location =
new
Point(10,10),
BackColor = Color.White,
};
panel.Paint += (s,e)=>
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
Pen pen =
new
Pen(Color.SteelBlue, 2);
g.DrawLine(pen, line_point[0], line_point[1]);
};
panel.MouseDown += (s, e) => { press =
true
; line_point[1] =
new
Point(e.X, e.Y); panel.Invalidate(); };
panel.MouseUp += (s, e) => { press =
false
; };
panel.MouseMove += (s, e) => {
if
(press) { line_point[1] =
new
Point(e.X, e.Y); panel.Invalidate(); } };
this
.Controls.Add(panel);
}
哎,。。。看看这个水平移动条呢?我画了一个
是在控件里画的,我移动鼠标得到的绿色部分无法刷新呢
我的代码如下:
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g;
g = panel1.CreateGraphics();
Pen pn1 = new Pen(Color.FromArgb(200, 200, 0), panel1.Height);
Point cer = new Point(0, 0);
Size re = new Size(panel1.Width / 16, panel1.Height);
g.DrawRectangle(pn1, cer.X, cer.Y, re.Width, re.Height);
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//鼠标在该控件上按下
{
Graphics g = panel1.CreateGraphics();
Pen pn1 = new Pen(Color.FromArgb(90, 200, 80), panel1.Height);
Point cer = new Point(0, 0);
newX = cer.X+e.X;
newY = cer.Y;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{//自定义重绘
base.OnPaint(e);
Graphics g;
g = panel1.CreateGraphics();
Pen pn1 = new Pen(Color.FromArgb(90, 200, 80), panel1.Height);
Size re = new Size(panel1.Width / 16, panel1.Height);
g.DrawRectangle(pn1, newX, newY, re.Width, re.Height);
}
如果我用Refresh()也不会刷新呢,绿色部分只会增加不会减少呢?
按照盘子的形状调整长度(圆上的点),这里省事就按鼠标位置画了个线,就这么个意思了
麻烦看看我这个是什么原因啊
只要这一个事件
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//鼠标在该控件上按下
{
Graphics g = panel1.CreateGraphics();
g.Clear(this.panel1.BackColor);
g.FillRectangle(new SolidBrush(Color.FromArgb(90, 200, 80)), 0, 0, e.X, this.panel1.Height);
}
}
啊,你都不重绘吗?我现在可以了,让重绘函数实现了动作,我再想把它作为滑块,显示数值就可以关联仪表指针的值