更改其形状后,按钮单击事件不起作用 [英] Button click event not functioning after changing its shape

查看:80
本文介绍了更改其形状后,按钮单击事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以告诉为什么当按钮的形状从其常规形状改变时按钮点击事件不会发生。就像我使用paint事件属性将其形状从矩形更改为八边形。我在表单中添加了一个按钮并初始化了大小,之后使用paint事件我创建了它的八边形。以下是我实施的代码:



Hi all,

Can anybody tell why the button click event doesn''t occur when the shape of the button is changed form its regular shape. Like i have changed its shape from rectangle to Octagonal shape using paint event properties. I have added a button in the form and initialized the size, after that using paint event i have created octagonal shape of it. Here is the code that i have implemented:

public Form1()
{
    InitializeComponent();  
    button1.Size = new Size(150, 130);
}

protected override void OnPaint(PaintEventArgs e)
{
     System.Drawing.Drawing2D.GraphicsPath g_path = new System.Drawing.Drawing2D.GraphicsPath();
     Point[] myarr = { new Point(10, 35), new Point(10, 95), new Point(30, 115), new Point(110, 115), new Point(130, 95), new Point(130, 35), new Point(110, 15), new Point(30, 15) };
     button1.BackColor = System.Drawing.Color.Green;
     button1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
     g_path.AddPolygon(myarr);
     button1.Region = new Region(g_path);
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("HI");
}

推荐答案

它没有停止工作:它只是剩下的区域非常小,除非你已经从它的标准尺寸放大了按钮,所以你必须在正确的位置点击。



但你为什么要以OnPaint覆盖的形式这样做?你确实意识到每次绘制表单时都会这样做,你只需要做一次吗?把它放在Form Load事件处理程序中......



就个人而言,我根本不会这样做 - 我会创建一个派生自的新类按钮(也称为HexButton?)将所有内容保留在控件本身内。 (并没有你的大利润)
It doesn''t stop working: it''s just that the remaining region is pretty small unless you have enlarged the button from it''s standard size, so you have to click in the right place.

But why are you doing that in the form OnPaint override? You do realize that that is done every time the form is painted, and you only need to do it once? Put it in the Form Load event handler instead...

Personally, I wouldn''t do it this way at all - I would create a new class derived from Button (called HexButton perhaps?) to keep all that within the control itself. (And not have your big margins, either)


试试这个:



protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

System.Drawing.Drawing2D.GraphicsPath g_path = new System.Drawing.Drawing2D.GraphicsPath();

Point [] myarr = {new Point(10 ,35),新点(10,95),新点(30,115),新点(110,115),新点(130,95),新点(130,35),新点(110,15) ),new Point(30,15)};

button1.BackColor = System.Drawing.Color.Green;

button1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;

g_path.AddPolygon(myarr);

button1.Region = new Region(g_path);

}
Try this:

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
System.Drawing.Drawing2D.GraphicsPath g_path = new System.Drawing.Drawing2D.GraphicsPath();
Point[] myarr = { new Point(10, 35), new Point(10, 95), new Point(30, 115), new Point(110, 115), new Point(130, 95), new Point(130, 35), new Point(110, 15), new Point(30, 15) };
button1.BackColor = System.Drawing.Color.Green;
button1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
g_path.AddPolygon(myarr);
button1.Region = new Region(g_path);
}


这篇关于更改其形状后,按钮单击事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆