Label transparency when button is pressed

  • Thread starter Thread starter Lonifasiko
  • Start date Start date
L

Lonifasiko

Hi,

I've got a button (Button2 from OpenNETCF 2.0) that must show the name
of the month centered and in a second line, the number of the year,
also centered. I've tried "\r\n", "\n" and "\r" but as the month name
length is variable, the number of the year is not centered. A pity!

Therefore, I've added a label for each button in my form, and I use
this label for showing the number of the year. I'm haviong problems
with the transparency of the label. I know it's quite a common problem,
therefore I did a little research and tried to put it into practice. No
success.

The problem comes when button is pressed. Button's backcolor is changed
to ActiveBackColor, and label's backcolor is not refreshed. I've
developed a CustomLabel control for supporting transparency but I see
that OnPaint and OnPaintBackground of my CustomLabel control are not
executed when button is being pressed.

Any hints on this? I've tried many examples found in the newsgroup, but
no success at all! Please, I would really appreciate any help.

Thanks very much in advance.
 
Hi,
I guess you are using OnMouseDown & OnMouseUp to manage the button. I did
the same and my code lokks like this:

// When the mouse is pressed, set the "pressed" flag = true and invalidate
// to cause a repaint. The Compact Framework sets the mouse capture
// automatically.
protected override void OnMouseDown(MouseEventArgs e)
{
this.pressed = true;
this.Invalidate();
base.OnMouseDown(e);
}

The Invalidate is important to cause the repaint.

Have Fun!
 
Hi,

Thanks for your reply, but I must tell you that I'm only using Click
event for the button. I do not use neither OnMouseDown nor OnMouseUp, I
just have changed "ActiveBackColor" property of the Button2 control
from OpenNETCF. Then, when button is pressed, the back color of the
button automatically changes.

I don't understand what you exactly mean. You mean I should override
OnMouseDown for all the form? You call "this" the button?

Have you experienced same problem as me?

Thanks very much.
 
Sorry,

I had not fully understood what you are doing.

Is the initial problem to get a two line lable onto a button and have each
line individually centered?

Well then you could probably do this by making your own button class and
overriding the OnPaint method of the base class.
Here you paint the text onto the button as you need. I created a
PictureButton Control from the msdn example
that does that.

My control uses two bitmaps, one for each button state (pressed and not
pressed).
For the label I use two different text colours, one for each state.
The label is then drawn horizontally and vertically centered onto the button.

So here is my OnPaint that draws the button and the label:
I uses the transparent stuff to support non-rectangluar buttons.

// Override the OnPaint method so we can draw the button image and the label
protected override void OnPaint(PaintEventArgs e)
{
// only if images are available
if (this.pressedImage != null && this.notPressedImage != null)
{
// Set the transparency color key as defined
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(transparentColor, transparentColor);

// Draw the image using the image attributes
if (this.pressed)
e.Graphics.DrawImage(this.pressedImage,
this.ClientRectangle,
0, 0,
this.pressedImage.Size.Width,
this.pressedImage.Size.Height,
GraphicsUnit.Pixel,
attr);
else
e.Graphics.DrawImage(this.notPressedImage,
this.ClientRectangle,
0, 0,
this.notPressedImage.Size.Width,
this.notPressedImage.Size.Height,
GraphicsUnit.Pixel,
attr);
}

// Draw the label, if there is any
if (buttonLabel.Length > 0)
{
// get the label size
SizeF size = e.Graphics.MeasureString(buttonLabel, this.Font);

// Center the text inside the client area of the PictureButton
// the color depends on the button state
if (this.pressed)
e.Graphics.DrawString(buttonLabel,
this.Font,
new SolidBrush(this.downLabelColor),
(this.ClientSize.Width - size.Width) / 2,
(this.ClientSize.Height - size.Height) / 2);
else
e.Graphics.DrawString(buttonLabel,
this.Font,
new SolidBrush(this.upLabelColor),
(this.ClientSize.Width - size.Width) / 2,
(this.ClientSize.Height - size.Height) / 2);
}
base.OnPaint(e);
}

You would have to change this to handle a double line label:
two DrawString calls each horizonatally centered, but handling the double
height.
You might be able to use the standard button as the base class, then you
wouldn't need the image stuff.



If you still want to use a custom label then I believe you will need to
invalidate the label to get it repainted.
Are you changing the background color each time the button is pressed? If
yes then there would be a good place to
invalidate the label.

Have fun.
 
I'll take a look into your code, but still don't know if it will work
for me.

I'm using Button2 control from OpenNETCF and this button has got a
Paint event. This event is fired whenever I press, click....the button.

I yesterday tried painting the label backColor with the same color the
button actually has, but I had no success. I think the back color of
the button does not match with ActiveBackColor property when Paint
event is fired. Maybe I need some more time to try harder.


To sum up, I need the back color of my label located "inside" the
button, to be painted everytime with the same back color of the button.
If button changes to ActiveBackColor when it is pressed, clicked, or
whatever, the back color of the label should be also filled with this
back color. Simple as that.

I hope to try harder next days. Thanks anyway very much.
 
Back
Top