Marvin Dickhaus
Marvin Dickhaus

Reputation: 805

WinForms: Area of a button where an image can be drawn on

Okay, I have a button, which displays itself in different sizes which is determained by some setting.

I now want to display an image on the button. How do I get the rectangle which I can draw on?

If I query button.DisplayRectangle or button.ClientRectangle, I get the exact size of the button returned. But as the button has a border and so on, which I can't draw on, this is just wrong.

As I understand from the documentation, DisplayRectangle should return only the rectangle I can draw on, excluding stuff like borders and margins?

Is there a way to get what I want?

Update:

The answer from Douglas Barbin brought me to look at he SystemInformation class which actually has the Border3DSize property.

But still, if I calculate

Dim size = New Size(ctrl.Width - SystemInformation.Border3DSize.Width * 2, _
    ctrl.Height - SystemInformation.Border3DSize.Height * 2)

on my display there are still some (two to be exact) pixels missing to actually fit the image completely.

Upvotes: 1

Views: 141

Answers (1)

Douglas Barbin
Douglas Barbin

Reputation: 3615

This will only work if the button's FlatAppearance is set to flat.

Dim mySize as Size = New Size With 
{.Height = button.Height - (button.FlatAppearance.BorderSize * 2), 
 .Width = button.Width - (button.FlatAppearance.BorderSize * 2) }

For all other (non-flat) border styles, the border depends on the OS and any installed themes, so I'm not sure that there is an easy way to get this from within Visual Studio. It might be possible through Windows API calls, but the code would be pretty ugly.

Upvotes: 1

Related Questions