请问一个关于button的有关问题
请教一个关于button的问题
想重绘下button,让button有立体感一点
于是放到一个panel里面测试,发现button在左上角的时候如图1,看上去没问题。
button拖动一下位置,如图2,就不行了,不理解问题。
代码如下,图片见回帖的图片,不知道为什么****非要回帖才能发图。
------解决思路----------------------
坐标其实有3种,是3个坐标系,原点不同
1.相对坐标,是图形(控件)相对于它的父控件的位置
2.工作区坐标,是控件在窗体中的位置
3.屏幕坐标,是控件在显示器上显示的绝对坐标值
绘图的时候,一般都使用相对坐标;
偶尔需要在鼠标点击事件里先把鼠标点击的相对坐标转换成工作区坐标再判断到底点中了整个窗体中的什么控件;
屏幕坐标一般只在截屏的时候能用上,绘图的时候使用屏幕坐标绘图完全是在给自己找麻烦,因为你还需要先确定窗体到底被拖到哪里了
想重绘下button,让button有立体感一点
于是放到一个panel里面测试,发现button在左上角的时候如图1,看上去没问题。
button拖动一下位置,如图2,就不行了,不理解问题。
代码如下,图片见回帖的图片,不知道为什么****非要回帖才能发图。
public class CustomButton : System.Windows.Forms.Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
DrawButton(pevent);
}
private void DrawButton(PaintEventArgs pevent)
{
int X = this.Width;
int Y = this.Height;
Point[] points = {
new Point(0, 0),
new Point(X, 0),
new Point(X, Y),
new Point(0, Y),
new Point(0, 0)};
GraphicsPath path = new GraphicsPath();
path.AddLines(points);
this.Region = new Region(path);
SolidBrush sBrush = new SolidBrush(SystemColors.Control);
pevent.Graphics.FillPath(sBrush, path);
Graphics g = pevent.Graphics;
g.DrawLine(new Pen(SystemColors.ControlLightLight, 2.0f), new Point(this.Location.X, this.Location.Y), new Point(this.Location.X + this.Width, this.Location.Y));
g.DrawLine(new Pen(Color.Gray, 4.0f), new Point(this.Location.X + this.Width, this.Location.Y), new Point(this.Location.X + this.Width, this.Location.Y + this.Height));
g.DrawLine(new Pen(Color.Gray, 4.0f), new Point(this.Location.X + this.Width, this.Location.Y + this.Height), new Point(this.Location.X, this.Location.Y + this.Height));
g.DrawLine(new Pen(SystemColors.ControlLightLight, 2.0f), new Point(this.Location.X, this.Location.Y + this.Height), new Point(this.Location.X, this.Location.Y));
}
}
------解决思路----------------------
坐标其实有3种,是3个坐标系,原点不同
1.相对坐标,是图形(控件)相对于它的父控件的位置
2.工作区坐标,是控件在窗体中的位置
3.屏幕坐标,是控件在显示器上显示的绝对坐标值
绘图的时候,一般都使用相对坐标;
偶尔需要在鼠标点击事件里先把鼠标点击的相对坐标转换成工作区坐标再判断到底点中了整个窗体中的什么控件;
屏幕坐标一般只在截屏的时候能用上,绘图的时候使用屏幕坐标绘图完全是在给自己找麻烦,因为你还需要先确定窗体到底被拖到哪里了