0

I couldnt find anything like that at all (basicly every problem from so else is always a syntax problem) and well..., the situation is a bit more complicated. to avoid using 500 lines of code im going to describe it for the most part:

Ive got a Form wich is acting as a parent Form (MdiParent) and another Form wich is a Child but basicly a fully functional Form at its own. Im using several OnPaint methods in the childform, witch work perfectly fine, and 3 custom buttons on the parent Form witch also have their own OnPaint methods. These 3 buttons (actualy panels) and every other control on the parent Form are contained in a PictureBox witch fills the parent Form completely and is used to make the background of the parent transparent / clickthrough via TransparencyKey (havnt found any other ways of doing that).

the Problem is that every OnPaint Method on the parent wont work at all (they're beeing executed but dont paint anything).

here is some code but that isnt the problem i'd say:

        this.myButtonObject1.BackColor = System.Drawing.Color.Red;
        this.myButtonObject1.Location = new System.Drawing.Point(840, 0);
        this.myButtonObject1.Name = "myButtonObject1";
        this.myButtonObject1.Size = new System.Drawing.Size(50, 50);
        this.myButtonObject1.TabIndex = 0;
        this.myButtonObject1.Click += new System.EventHandler(this.myButton1_Click);
        this.myButtonObject1.Paint += new System.Windows.Forms.PaintEventHandler(this.myButtonObject1_Paint);


    private void myButtonObject1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        LinearGradientBrush lgb = new LinearGradientBrush(new PointF(0, 0), new PointF(myButtonObject1.Width, myButtonObject1.Height), Color.Green, Color.Lime);
        Pen p = new Pen(lgb, 5);
        g.DrawRectangle(p, myButtonObject1.Bounds);
        lgb.Dispose();
        p.Dispose();
    }

if anyone can tell me; what am i doing wrong?

PS: i m using .net 4.5, VS 2015, and havnt changed any of the default settings besides TopMost FormBorderStyle ShowInTaskbar StartPosition and ofc the color and trancparencyKey, but i dont think it has anything todo with that.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

0

Set myButtonObject1.FlatStyle to FlatStyle.Standard.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Update

The small error in your code is to use the Panel's Bounds property, which at runtime will refer to the Panel's Location within its Parent! But the drawing code must be relative to the object, not its parent!

So do not use Bounds but ClientRectangle and make sure to set the right PenAlignment:

using (LinearGradientBrush lgb = 
   new LinearGradientBrush(ClientRectangle, Color.Green, Color.Lime, 0f) ) //or some angle!
using (Pen p = new Pen(lgb, 5))
{
    p.Alignment = PenAlignment.Inset;
    g.DrawRectangle(p, ClientRectangle);
}

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
  • im just going to tell you that you CAN create a pen from lgb the problem was , as you pointed out corectly, the `.bounds` rectangle ty very much – Sacul Rennorb Dec 06 '15 at 19:07
  • Gee, you're right. Good to know!! I'll update the answer.. See the additional hints! – TaW Dec 06 '15 at 19:46