• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Finding the upper left coordinate of minimize button on the caption bar

How do I find the upper left coordinate of minimize button on the caption bar?
How can I determine the width of the bottom too?
0
jclobocar
Asked:
jclobocar
1 Solution
 
gandalf_the_whiteCommented:
you mean you have a form that is minimized and want to know where the X for closing it is?

could you explain why you need it. maybe there is a workaround for solving your problem.
0
 
jclobocarAuthor Commented:
No
I have a code to draw a "minimize to tray" button on caption bar, but depending on resolution or applied XP theme there is a overlap.
If I knew where is the upper, left corner of the leftmost caption button (minimize), my button would be drawn in the correct place.
0
 
pcsentinelCommented:
I think you may be able to use getsystemmetrics with the SM_CXSIZE, SM_CYSIZE settings which return the Width and height of a button in a window's caption or title bar, in pixels.

i.e. GetSystemMetrics(SM_CXSIZE) should return the width of the minimize button

you would then need to do the math to calculate which buttons are visible and multiply it by thre previous figure.

Min andmax buttons are next to each other so just add 2 widths, the close button is I think 2 pixels gap form the min and max buttons so add that and another width for that button. then subtract that from the Total Form Width.

regards

0
 
Russell LibbySoftware Engineer, Advisory Commented:
The following calculations should give you what you are after (sample code follows the function code). I take no credit for the original code, as it comes from here:

http://www.catch22.net/tuts/titlebar.asp

I only translated it from C-> Delphi

Regards,
Russell

function GetSysBtnPos(Handle: HWND): TPoint;
var  dwStyle:       Integer;
     dwBtn:         Integer;
     dwSysBtn:      Integer;
     dwExStyle:     Integer;
     rcClient:      TRect;

     function CalcTopEdge(Style: Integer): Integer;
     begin
            if ((Style and WS_THICKFRAME) = WS_THICKFRAME) then
           result:=GetSystemMetrics(SM_CYSIZEFRAME)
        else
                  result:=GetSystemMetrics(SM_CYFIXEDFRAME);
     end;

     function CalcRightEdge(Style: Integer): Integer;
     begin
            if ((Style and WS_THICKFRAME) = WS_THICKFRAME) then
           result:=GetSystemMetrics(SM_CXSIZEFRAME)
        else
                  result:=GetSystemMetrics(SM_CXFIXEDFRAME);
     end;

const
     EDGE_SIZE       =  2;

begin

  // Get window style and extended style
  dwStyle:=GetWindowLong(Handle, GWL_STYLE);
  dwExStyle:=GetWindowLong(Handle, GWL_EXSTYLE);

  // Get client rect for window
  GetClientRect(Handle, rcClient);

  // Set defaults
  dwBtn:=0;

  // Calc top position
  result.y:=CalcTopEdge(dwStyle)+EDGE_SIZE;

  // Check for tool window style
      if ((dwExStyle and WS_EX_TOOLWINDOW) = WS_EX_TOOLWINDOW) then
  begin
     dwSysBtn:=GetSystemMetrics(SM_CXSMSIZE) - EDGE_SIZE;
     if ((dwStyle and WS_SYSMENU) = WS_SYSMENU) then Inc(dwBtn, dwSysBtn + EDGE_SIZE);
  end
  else
  begin
     dwSysBtn:=GetSystemMetrics(SM_CXSIZE)-EDGE_SIZE;
            // Window has Close [X] button. This button has a 2-pixel border on either size
            if ((dwStyle and WS_SYSMENU) = WS_SYSMENU) then Inc(dwBtn, dwSysBtn + EDGE_SIZE);
            // If either of the minimize or maximize buttons are shown, then both will
     // appear (but may be disabled). This button pair has a 2 pixel border on the left
            if ((dwStyle and WS_MINIMIZEBOX) = WS_MINIMIZEBOX) or ((dwStyle and WS_MAXIMIZEBOX) = WS_MAXIMIZEBOX) then
        Inc(dwBtn, EDGE_SIZE + dwSysBtn * 2)
     else if ((dwStyle and WS_SYSMENU) = WS_SYSMENU) and ((dwExStyle and WS_EX_CONTEXTHELP) = WS_EX_CONTEXTHELP) then
               // A window can have a question-mark button, but only if it doesn't have the
        // min / max buttons
                  Inc(dwBtn, EDGE_SIZE + dwSysBtn);
  end;

      // Now calculate the size of the border
  result.x:=rcClient.Right-dwBtn+CalcRightEdge(dwStyle)+EDGE_SIZE;

end;

// Called from form1's button click
procedure TForm1.Button1Click(Sender: TObject);
var  ptSysBtn:      TPoint;
     hdcSelf:       HDC;
begin

  ptSysBtn:=GetSysBtnPos(Handle);
  hdcSelf:=GetWindowDC(Handle);
  SetPixel(hdcSelf, ptSysBtn.x, ptSysBtn.y, clBlack);


end;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now