如何在C#上绘制具有渐变背景颜色的标签上的形状? [英] how to draw shapes on label which have gradient background color in C#?

查看:64
本文介绍了如何在C#上绘制具有渐变背景颜色的标签上的形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序,它在标签背景上显示渐变效果。与此同时,我想用图形类在该标签上绘制一些形状。但是标签背景上看不到形状,如果我从标签中删除了渐变效果,那么它就是可见的..



那么,如何绘制形状在标签上有渐变背景颜色(没有从标签上删除渐变效果)??



我的渐变效果代码是 - :



 使用系统; 
使用 System.Drawing;
使用 System.Windows.Forms;

命名空间 ForPrintPre
{
public partial class GradientEffect:Form
{
Color c1 = Color.Yellow,c2 = Color 。绿色;
Graphics grx;
int countInt = 0 ;
public GradientEffect()
{
InitializeComponent();
grx = label1.CreateGraphics();
}

private void button1_Click( object sender,EventArgs e)
{
colorDialog1.ShowDialog();
c1 = colorDialog1.Color;
label1.Invalidate();
}

private void button2_Click( object sender,EventArgs e)
{
colorDialog1.ShowDialog();
c2 = colorDialog1.Color;
label1.Invalidate();
}

private void button3_Click( object sender,EventArgs e)
{
grx.Clear(label1.BackColor);
if (comboBox1.Text == 矩形
{
grx.DrawRectangle( new Pen(Brushes.Black), new 矩形( 10 10 ,label1.Width-30,label1.Height -30));
}
else if (comboBox1.Text == Circle
{
grx.DrawArc(新的笔(Brushes.Black), 10 10 ,label1.Width-30, label1.Height-30, 0 360 );
}
else if (comboBox1.Text ==
{
grx.DrawLine(新的笔(Brushes.Black), 10 10 ,label1.Width - 30 10 );
}
}

private void label1_Paint( object sender,PaintEventArgs e)
{
Color tem1 = Color.FromArgb( 150 ,c1);
颜色tem2 = Color.FromArgb( 150 ,c2);
刷b = new System.Drawing.Drawing2D.LinearGradientBrush(label1.ClientRectangle,c1,c2, 20 < /跨度>);
e.Graphics.FillRectangle(b,label1.ClientRectangle);
}
}
}





提前谢谢!!!

解决方案

请看我对这个问题的评论。



问题非常明显。代码不正确,因为您在点击事件上进行渲染。绘图发生了,但它不会持续存在。您应该在事件 Paint 的处理程序中执行所有渲染,或者在重写的 OnPaint 方法中执行。仅使用从事件参数中获取的 Graphics 的实例。



渐变是正确完成的,形状不是。鼠标事件处理程序的处理程序应该只修改一些数据(例如,将它存储在控件类中),并通过调用 Control.Invalidate 来触发重新呈现。 />


请查看我过去的答案:

mdi子表单之间的绘制线 [ ^ ],

在面板上捕获绘图 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ]。







我我并不是说你永远不应该在鼠标事件处理程序中绘图。有时你可以,但这是一种先进的技术。您应该了解,除非您在 OnPaint 中重现绘图,否则渲染的绘图将不会持久存在。我的过去的答案解释 WM_PAINT 的工作原理应该为你解释。



-SA

I created a application, which show the gradient effect on label background. At the meantime I want to draw some shapes on that label with graphic class. but the shapes are not visible on label background,If I removed the gradient effect from the label then it''s visible..

So, how to draw the shapes on label which have gradient background color(without removing the gradient effect from label)??

My code for gradient effect is -:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ForPrintPre
{
    public partial class GradientEffect : Form
    {
        Color c1=Color.Yellow, c2=Color.Green;
        Graphics grx;
        int countInt=0;
        public GradientEffect()
        {
            InitializeComponent();
            grx = label1.CreateGraphics();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            c1 = colorDialog1.Color;            
            label1.Invalidate();
        }             

        private void button2_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            c2 = colorDialog1.Color;
            label1.Invalidate();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            grx.Clear(label1.BackColor);
            if (comboBox1.Text == "Rectangle")
            {                
                grx.DrawRectangle(new Pen(Brushes.Black),new Rectangle(10,10,label1.Width-30,label1.Height-30));
            }
            else if(comboBox1.Text=="Circle")
            {
                grx.DrawArc(new Pen(Brushes.Black),10,10,label1.Width-30,label1.Height-30,0,360);
            }
            else if (comboBox1.Text == "Line")
            {
                grx.DrawLine(new Pen(Brushes.Black), 10, 10, label1.Width - 30, 10);
            }
        }

        private void label1_Paint(object sender, PaintEventArgs e)
        {
            Color tem1 = Color.FromArgb(150, c1);
            Color tem2 = Color.FromArgb(150, c2);
            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(label1.ClientRectangle, c1, c2, 20);
            e.Graphics.FillRectangle(b,label1.ClientRectangle);
        }
    }
}



thanks in advance!!!

解决方案

Please see my comment to the question.

The problem is quite apparent. The code is incorrect, because you do rendering on click events. The drawing happens, but it does not persist. You should do all the rendering in your handler of the event Paint, or in the overridden OnPaint method. Use only the instance of Graphics obtained from event arguments.

You gradient is done correctly, shapes are not. Your handler of the mouse event handlers should only modify some data (store it in a control class, for example), and trigger re-rendering by calling Control.Invalidate.

Please see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

[EDIT]

I''m not saying that you never should do drawing in mouse event handlers. Sometimes you can, but this is an advanced technique. You should understand that the rendered drawing will not persist unless you reproduce the drawing in OnPaint. My past answer explaining how WM_PAINT works should explain it for your.

—SA


这篇关于如何在C#上绘制具有渐变背景颜色的标签上的形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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