Overriding Button Control

Hi there,

I'm trying to give the button control (System.Windows.Forms.Button) a different appearence. Therefore I overrode the buttons OnPaint method

protected override void OnPaint(PaintEventArgs pe)

{ do some paintings }

The rendering works but the original button partially looks through my painting.

How can I get rid of the original button surface

Can anyone help me

Thanks in advance...



Answer this question

Overriding Button Control

  • Mike Vernal

    Don't call the base method and override the OnPaintBackGound method.

    You can first clear you Graphics with the Clear method of the Graphics object, if needed.


  • HelloWorld .Net

    Hi PJ,

    I didn't call the base method and I overrode the other OnPaint method, but the original button can still be seen. The crucial hint was to call the Clear method. Now everthing works fine!

    Thank you very much.

  • Kenny Wolf

    A good article about creating a custom button can be foud here on codeproject.

    Sorry for the doublepost, but my browser won't let me edit my posts.


  • jayakumar

    Hi Salvador,

    cool solution!

    Thank you for your help.

  • Tony Virnoche

    Hi,

    You can do two things, the first option overriding the OnPaint, you can do your drawing and do not call the base OnDraw, so no further drawing is executed.

    If you want to go a little deeper, just override the

    protected override void WndProc(ref Message m)
    {
    const int WM_PAINT = 0x000F;

    switch (m.Msg)
    {
    case WM_PAINT:
    {
    RenderFunction();
    break;
    }
    default:
    {
    base.WndProc (ref m);
    break;
    }
    }
    }

    Remember to call the base one if you are not using the event, otherwise no message will go through and your app will not response.

    Hope this helps.

    Regards




  • Overriding Button Control