AlwaysLearningNewStuff
AlwaysLearningNewStuff

Reputation: 3031

Get the size of checkbox

I am using a custom draw to change the text and background color of a themed checkbox.

In order to position text properly I need the dimensions of the check box. The below picture illustrates what I mean:

enter image description here

Browsing through StackOverflow archive I found this post but it got me confused. I just do not know which answer is the solution to my problem. Internet did not help either.

Upvotes: 2

Views: 1064

Answers (2)

Denis Anisimov
Denis Anisimov

Reputation: 3317

function GetCheckBoxSize(AWnd: HWND): TSize;
var
  Theme: HTHEME;
  Bitmap: HBITMAP;
  BitmapSize: Winapi.Windows.TBitmap;
  State: DWORD;
  StateID: DWORD;
begin
  Result.cx := 0;
  Result.cy := 0;

  if IsWindowsXPOrLater and IsThemeActive and IsAppThemed then
    begin
      Theme := GetWindowTheme(AWnd);
      if Theme <> 0 then
        begin
          State := SendMessage(AWnd, BM_GETSTATE, 0, 0);
          if State and BST_CHECKED <> 0 then StateID := CBS_CHECKEDNORMAL
                                        else StateID := CBS_UNCHECKEDNORMAL;
          if GetThemePartSize(Theme, 0, BP_CHECKBOX, StateID, nil, TS_TRUE, Result) = S_OK then
            Exit;
        end;
    end;

  Bitmap := LoadBitmap(0, PChar(OBM_CHECKBOXES));
  if Bitmap <> 0 then
    try
      if GetObject(Bitmap, SizeOf(BitmapSize), @BitmapSize) = SizeOf(BitmapSize) then
        begin
          Result.cx := BitmapSize.bmWidth div 4;
          Result.cy := BitmapSize.bmHeight div 3;
        end;
    finally
      DeleteObject(Bitmap);
    end;
end;

Upvotes: 4

David Heffernan
David Heffernan

Reputation: 613442

The function that you need, as I understand it is GetThemePartSize. You need to supply BP_CHECKBOX for the part, and whichever of the states is appropriate.

Upvotes: 3

Related Questions