应用新的颜色(有梯度)来运表单按钮的onClick梯度、表单、按钮、颜色

2023-09-03 04:28:51 作者:不期而遇的温暖

我遇到应用渐变样式对象在Windows窗体应用程序的几种方法。所有的方法包括覆盖OnPaint方法。不过,我期待的改变风格在运行时根据验证。

如何申请新的渐变风格已经呈现按钮(像我可以与背景色)?

R, ℃。

更新:这是我目前使用的code。它似乎没有任何效果

 私人无效Button_Paint(对象发件人,System.Windows.Forms.PaintEventArgs E)
    {
        图形G = e.Graphics;
        g.DrawString(这是描绘在控制的对角线,
            新的字体(宋体,10),System.Drawing.Brushes.Blue,新的点(30,30));
        g.DrawLine(System.Drawing.Pens.Red,btn.Left,btn.Top,
            btn.Right,btn.Bottom);

        this.btn.Invalidate();
    }
 

被称为按

  btn.Paint + =新PaintEventHandler(this.Button_Paint);
 

进一步更新与当前code

 私人无效Button_Paint(对象发件人,PaintEventArgs的E)
{
图形G = e.Graphics;
g.DrawString(这是描绘在控制的对角线,
        新的字体(宋体,10),System.Drawing.Brushes.Blue,新的点(30,30));
g.DrawLine(System.Drawing.Pens.Red,btn.Left,btn.Top,
        btn.Right,btn.Bottom);
}

私人无效btn_Click(对象发件人,EventArgs的)
{
btn.Paint + =新PaintEventHandler(this.Button_Paint);();
btn.Invalidate();
}
 
使用html计算1 1 3 1 5 ... 1 2n 1 累加和

解决方案

有两个部分这一点。其中,作为SLaks说,你需要绘制渐变在油漆事件处理程序。这将是这个样子(我在这里的例子是有点混乱为了简洁起见):

 私人无效Button_Paint(对象发件人,PaintEventArgs的E)
{
    图形G = e.Graphics;
    如果(MyFormIsValid()){
        g.DrawString(这是描绘在控制的对角线,
            新的字体(宋体,10),System.Drawing.Brushes.Blue,新的点(30,30));
        g.DrawLine(System.Drawing.Pens.Red,btn.Left,btn.Top,
            btn.Right,btn.Bottom);
    }
    其他 {
        g.FillRectangle(
            新的一个LinearGradientBrush(PointF.Empty,新的PointF(0,btn.Height),Color.White,Color.Red)
            新的RectangleF(PointF.Empty,btn.Size));
    }
}
 

此外,你需要做的验证和重绘按钮被点击时:

  btn.Click + = Button_Click;
 

...

 私人无效Button_Click(对象发件人,EventArgs的)
{
    DoValidations();
    btn.Invalidate();
}
 

当然,你必须执行 DoValidations() MyFormIsValid()的方法。

下面是整个事情作为一个可运行示例程序: http://pastebin.com/cfXvtVwT

I've come across several methods of applying gradient styles to objects in a windows form application. All the methods involve overriding the OnPaint method. However, I am looking the change the style at runtime based on validation.

How can I apply the new gradient style to an already rendered button (like I can with BackColor)?

R, C.

UPDATE: This is the code I am currently using. It appears to have no effect

private void Button_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawString("This is a diagonal line drawn on the control",
            new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
        g.DrawLine(System.Drawing.Pens.Red, btn.Left, btn.Top,
            btn.Right, btn.Bottom);

        this.btn.Invalidate();
    }

Being called by

btn.Paint += new PaintEventHandler(this.Button_Paint);

FURTHER UPDATE WITH CURRENT CODE

private void Button_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("This is a diagonal line drawn on the control",
        new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
g.DrawLine(System.Drawing.Pens.Red, btn.Left, btn.Top,
        btn.Right, btn.Bottom);
}

private void btn_Click(object sender, EventArgs e)
{
btn.Paint += new PaintEventHandler(this.Button_Paint);();
btn.Invalidate();
}

解决方案

There are two parts to this. One, as SLaks said, you need to draw the gradient in your Paint event handler. This would look something like this (my example here is a bit messy for the sake of brevity):

private void Button_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    if (MyFormIsValid()) {
        g.DrawString("This is a diagonal line drawn on the control",
            new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
        g.DrawLine(System.Drawing.Pens.Red, btn.Left, btn.Top,
            btn.Right, btn.Bottom);
    }
    else {
        g.FillRectangle(
            new LinearGradientBrush(PointF.Empty, new PointF(0, btn.Height), Color.White, Color.Red),
            new RectangleF(PointF.Empty, btn.Size));
    }
}

Also, you need to do your validation and redraw the button when it is clicked:

btn.Click += Button_Click;

...

private void Button_Click(object sender, EventArgs e)
{
    DoValidations();
    btn.Invalidate();
}

Of course, you'll have to implement the DoValidations() and MyFormIsValid() methods.

Here's the whole thing as a runnable sample program: http://pastebin.com/cfXvtVwT

 
精彩推荐
图片推荐