C#中的自定义控件 [英] Custom controls in c#
本文介绍了C#中的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我创建了一个自定义的圆形按钮.现在,我希望在鼠标单击按钮时更改按钮的颜色.
Hi all
I have created a custom buttom round in shape .Now i want the color of the button to be changed at the mouse click of the button .
Designer.cs
using System.Windows.Forms;
using System.Drawing;
using System;
using MyNamespace;
public class MyForm : Form
{
public MyForm()
{
RoundButton roundButton = new RoundButton();
MouseEventHandler handler = new MouseEventHandler(roundButton_MouseClick);
roundButton.MouseClick += handler;
roundButton.backgroundColor = System.Drawing.Color.White;
roundButton.Size = new System.Drawing.Size(50, 50);
roundButton.Location = new System.Drawing.Point(100, 30);
this.Controls.Add(roundButton);
}
public void roundButton_MouseClick(Object source, System.Windows.Forms.MouseEventArgs e)
{
Color backgroundColor = Color.Red;
Graphics graphics = CreateGraphics();
int penWidth = 4;
Pen pen = new Pen(Color.Green, 4);
SolidBrush brush = new SolidBrush(backgroundColor);
graphics.FillEllipse(brush, 0, 0, Width, Height);
graphics.DrawEllipse(pen, (int)penWidth / 2,
(int)penWidth / 2, Width - penWidth, Height - penWidth);
graphics.DrawLine(pen, 26, 15, 26, 30);
graphics.DrawArc(pen, (this.Width / 4), (this.Height / 2), (this.Width / 2), (this.Height / 4), 0, 180); // Mouth
}
}
form.cs
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace MyNamespace
{
[Description("Button Control")]
[ToolboxBitmap(typeof(Button))]
[Designer(typeof(RoundButton))]
public class RoundButton : UserControl
{
public Color backgroundColor = Color.Blue;
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
int penWidth = 4;
Pen pen = new Pen(Color.Yellow, 4);
SolidBrush brush = new SolidBrush(backgroundColor);
graphics.FillEllipse(brush, 0, 0, Width, Height);
graphics.DrawEllipse(pen, (int)penWidth / 2,(int)penWidth / 2, Width - penWidth, Height - penWidth);
graphics.DrawLine(pen, 26, 15, 26, 30);
e.Graphics.DrawArc(pen, (this.Width / 4), (this.Height / 2), (this.Width / 2), (this.Height / 4), 0, 180); // Mouth
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// RoundButton
//
this.Name = "RoundButton";
//this.Load += new System.EventHandler(this.RoundButton_Load);
this.ResumeLayout(false);
}
}
}
在我的代码中,这里发生的是按钮尺寸很大并且旧按钮仍然可见.它没有被重新粉刷.任何人都可以帮助我.是否需要它.
Here in my code what it happens is the button size is large and the old button is still visible .Its not getting repainted.Can anyone help me .Am in need of it .
推荐答案
您无需重新进行所有绘画.只需将源转换为RoundButton,更改背景色,然后就需要像这样在控件上调用Invalidate():
You do not need to do all the painting all over again. Just cast source as a RoundButton, change the background colour, and then you need to call Invalidate() on the control like this:
void roundButton_MouseClick(object source, MouseEventArgs e)
{
RoundButton button = source as RoundButton;
button.backgroundColor = Color.Red;
button.Invalidate();
}
那应该起作用.
希望这对您有帮助
That should work.
Hope this helps
否您没有创建圆形按钮.您可能会创建一个看上去只有圆形的东西,但是使用鼠标单击会很快发现它不是.
若要进行真正的回合(任何非矩形的形状控件),您需要使用System.Windows.Forms.Control.Region
属性,请参见 ^ ],代码示例.
现在,您使用Graphics
的工作全错了.您永远不应创建类Graphics
的实例.相反,您应该使用在事件Paint
的处理程序的事件参数中传递的实例,或者最好在重写的方法OnPaint
中使用.实际渲染将由Control.Invalidate
触发.
我在过去的回答中提供了更多细节:
Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在mdi子窗体之间画线 [在面板上捕获图形 [
No you did not create a round button. You may be created something which only looks round, but using mouse click will quickly reveal it is not.
To make a true round (a any non-rectangular shape control) you need to useSystem.Windows.Forms.Control.Region
property, see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx[^], with code sample.
Now, your work withGraphics
is all wrong. You should never create an instance of the classGraphics
. Instead, you should use the instance passed in event arguments of the handler of the eventPaint
, or, better in the overridden methodOnPaint
. The actual rendering will be triggered byControl.Invalidate
.
I put more detail in my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].
—SA
这篇关于C#中的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文