CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003
    Posts
    2

    Question Transparent Label... Bug????

    I've posted a code example below.

    A form with a LinearGradientBrush so that backgrounds a nice shade...

    Then simply put a Lable on the form with with its background color set to transparent.

    But it seems to cause an exception any one no why? or can any one think of a work around?

    Cheers

    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // label1
    //
    this.label1.BackColor = System.Drawing.Color.Transparent;
    this.label1.Location = new System.Drawing.Point(92, 56);
    this.label1.Name = "label1";
    this.label1.TabIndex = 0;
    this.label1.Text = "label1";
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.label1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    protected override void OnPaintBackground(PaintEventArgs e) {
    try {
    using(Graphics g = e.Graphics) {
    g.Clear(Color.White);
    Rectangle r = new Rectangle(5,5,this.Bounds.Width-15,this.Bounds.Height-15);
    System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(r,
    Color.FromArgb(128,65,151,255),
    Color.White,
    System.Drawing.Drawing2D.LinearGradientMode.Vertical);

    Rectangle r2 = new Rectangle(1,1,this.Bounds.Width-1,this.Bounds.Height-1);
    ControlPaint.DrawBorder3D(g,r2,Border3DStyle.Raised,Border3DSide.All);


    g.FillRectangle(lgb,r);
    }


    }catch(Exception ee) {

    }
    }
    }

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    remove using statement

    Graphics g = e.Graphics;

    g.Clear(Color.White);
    Rectangle r = new Rectangle(5,5,this.Bounds.Width-15,this.Bounds.Height-15);
    System.Drawing.Drawing2D.LinearGradientBrush lgb = new System.Drawing.Drawing2D.LinearGradientBrush(r,
    Color.FromArgb(128,65,151,255),
    Color.White,
    System.Drawing.Drawing2D.LinearGradientMode.Vertical);

    Rectangle r2 = new Rectangle(1,1,this.Bounds.Width-1,this.Bounds.Height-1);
    ControlPaint.DrawBorder3D(g,r2,Border3DStyle.Raised,Border3DSide.All);


    g.FillRectangle(lgb,r);



    }
    catch(Exception ee)
    {
    }

    do like that.

    G is gone after a try catch and it needs to be there atleast inside a paint method.

    Paresh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured