C# WinForm中自定义了一个控件,在Form中放了一个button,如何实现按下button之后,让自定义的控件响应button事件进行缩放?
解决方案 »
- 报错:未处理FormatException Input string was not in a correct format.
- winform 从上到下 从右到左 显示文字
- 問下aspx網頁中怎么把login控件放在頁面中間?菜菜的問題
- TabPage的批量关闭方法
- 新手有问题请教。。。
- 关与SQL Server数据库同一程序多连接的问题,问题解决就结帖,在线等……
- 图片放大、缩小问题。
- 传说现在不推荐API,因为多被封装到.net框架去了。可如何知道被封装到哪去了?有不有对照表类似的东东?
- 苦恼的问题!!!!!
- 急!!!高分求教!SqlDataAdapter.Update(DataSet,TableName)更新datagrid删除数据的问题
- C#调用C++ dll提示:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
- WPF,关于控件可视状态,麻烦看下这段代码
protected float m_Scale = 1F; protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// Don't paint background
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.graphics = e.Graphics;
this.graphics.Clear(this.BackColor);
OnDraw();
}
这是自定义控件的代码,有好的解决办法没?
还是说点另外一个按钮这个自定义控件会缩放?前者你可以在自定义控件重载OnMouseDown OnMouseUp
后者你可以在自定义控件中定义一个缩放函数,让button click事件调用
还是说点另外一个按钮这个自定义控件会缩放?前者你可以在自定义控件重载OnMouseDown OnMouseUp
后者你可以在自定义控件中定义一个缩放函数,让button click事件调用
你在button事件中调用 自定义控件的.Invalidate()函数使控件区域无效导致重新绘制;
abstract public class DrawingArea : Panel
{
protected Graphics graphics;
protected float m_Scale = 1F; protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// Don't paint background
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.graphics = e.Graphics;
this.graphics.Clear(this.BackColor);
OnDraw();
}
}其中一个子类:
class BianJingDian : DrawingArea
{
private float m_Scale = 0.1F;
protected override void OnDraw()
{
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Yellow);
//绘制阀门下半部分
this.graphics.DrawLine(pen, 0, 0, 20, 20);
this.graphics.DrawLine(pen, 0, 0, 40, 0);
this.graphics.DrawLine(pen, 20, 20, 40, 0);
//颜色填充
Point[] pntArr = new Point[3];
pntArr[0] = new Point(0, 0);
pntArr[1] = new Point(40, 0);
pntArr[2] = new Point(20, 20);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
this.graphics.ScaleTransform(this.m_Scale, this.m_Scale, MatrixOrder.Prepend);
path.AddLines(pntArr);
this.graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
this.graphics.FillPath(Brushes.Yellow, path);
this.graphics.ScaleTransform(this.m_Scale, this.m_Scale, MatrixOrder.Append);
}
}
缩放参数是m_Scale,具体缩放在子类中实现,只需想办法改变m_Scale就可以。
你不应该在子类再定义一个m_Scale,共享父类的就OK了,这样修改:
在父类DrawingArea中定义一个属性:public float Scale
{
get{return m_Scale;}
set
{
if(value!=m_Scale)
{
m_Scale=value;
this.Invalidate();
}
}
}
把子类的m_Scale变量删了在调用方调用这个属性设置值就可以了
还有一个问题,就是一个Form中这么多控件要一个个调用吗?
还有一个问题,就是一个Form中这么多控件要一个个调用吗?
11楼已经有一个解决方法,最简单的就是你把这些控件集合在list当中,遍历这个list执行更新