Blurry Sterk
Blurry Sterk

Reputation: 1607

Get the size of non-client area of a themed Edit Control

How do you get the width of the border (non-client) of a themed Edit control in Windows XP and later versions?

On Windows 7 GetThemeMetric with TMT_BORDERSIZE as property identifier returns 0 for an existing edit control and 1 if no handle is provided. But on closer inspection it seems it should be 2.

When WS_VSCROLL or WS_HSCROLL windows styles are included for the control then the scrollbars are drawn inside that border and they are indeed 2 pixels away from the outside edge of the control, so I assume there is a way of getting the correct information.

The reason for the query is for me to be able to set the client area size when WM_NCCALCSIZE occurs when creating a custom control.

enter image description here

Upvotes: 4

Views: 1471

Answers (1)

zett42
zett42

Reputation: 27776

How do you get the width of the border (non-client) of a themed Edit control in Windows XP and later versions?

The following works for all controls, whether they are themed or not. It doesn't even need the theme API.

  1. Call GetClientRect() to get the size of the client area.
  2. Call ClientToScreen() to transform client rect to screen coordinates.
  3. Call GetWindowRect() to get the rectangle of the control including NC area, in screen coordinates.
  4. Calculate difference between client rect and window rect coordinates to get size of border (e. g. leftBorderWidth = clientRect.left - windowRect.left).

Edit:

Interestingly, the Wine source theme_edit.c doesn't use GetThemeMetric() at all. Instead they call GetSystemMetrics() with SM_CXEDGE and SM_CYEDGE.

On my systems (Windows 7 and Windows 10), this returns the correct value of 2.

Upvotes: 6

Related Questions