Solved

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

Posted on 2005-04-21
409 Views
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
Question by:jclobocar

LVL 3

Expert Comment

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

LVL 1

Author Comment

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

LVL 11

Expert Comment

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

LVL 26

Accepted Solution

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

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

#### 759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!