How to draw on a button ?

derez

Expert
Licensed User
Longtime User
I managed to create a button with gradient, for the desktop, with c# and .net , based on Agraham's ImageLibEx . In .net Framework 2 the button has an Image property and I use it, so it works, but in compact framework this property does not exist so I have to draw on a bitmap and copy to the button.

Now I try to draw on the button using Graphics and drawimage method, but it does not work.

Can someone look at it and give me an advise ?

I attach the cs file, the dll and the basic program.

The drawing that works is
B4X:
private void drawbutton( int w,int h)
{
GFB.Image = new System.Drawing.Bitmap(w,h);
Rectangle nrc = new System.Drawing.Rectangle(0,0,w,h);
LinearGradientBrush gbrush = new LinearGradientBrush(nrc, StartColor, EndColor, (Single)(FillDirection == 0 ? 0 : 90));
Graphics g = Graphics.FromImage(GFB.Image);
g.FillRectangle(gbrush, nrc);
g.Dispose();
}

and the code that doesn't work:
B4X:
private void drawbutton( int w,int h)
{
Bitmap bm = new System.Drawing.Bitmap(w,h);
Rectangle nrc = new System.Drawing.Rectangle(0,0,w,h);
LinearGradientBrush gbrush = new LinearGradientBrush(nrc, StartColor, EndColor, (Single)(FillDirection == 0 ? 0 : 90));
Graphics g = Graphics.FromImage(bm);
Graphics gr = GFB.CreateGraphics();
g.FillRectangle(gbrush, nrc);
gr.DrawImage(bm,nrc);
g.Dispose();
gr.Dispose();
}

and another question - do I have to define the Paint event in the constructor, and overide it in the class with the drawbutton method? how do I do it ?

Thanks.
 

Attachments

  • GFB.zip
    5.4 KB · Views: 179
Last edited:

derez

Expert
Licensed User
Longtime User
Thank you, I wasn't aware of all the cs files there.
I'll study it.
 
Top