Find the position of a control in C++ - c++

I need to find the x and y coordinates of a control, but I'm not sure where to begin. The only solutions that I've found on the internet use MFC, and I want to avoid using that. So, is there a way to find the coordinates of a control without using MFC?
Using FindWindow to find the control:
RECT cr;
HWND chWnd = FindWindow("SunAwtCanvas", NULL);
GetWindowRect(chWnd, &cr);
cw = cr.right;
ch = cr.bottom;
cx = cr.left;
cy = cr.top;
cout << chWnd << endl;
chWnd is 0 when it should have the hWnd of the control.

use GetWindowRect on the hwnd of the control (every control has its own hwnd): http://msdn.microsoft.com/en-gb/library/windows/desktop/ms633519(v=vs.85).aspx. this will give you the left, right, top and bottom allowing to know the bounds of the control, and also work out its height and width

Your code for getting HWND is bad.
Cite from MSDN: FindWindow
Retrieves a handle to the top-level window whose class name and window
name match the specified strings. This function does not search child
windows. This function does not perform a case-sensitive search.
Use FindWindowEx instead.

Related

How to find handle to a mfc control at mouse point but lie under other control?

I want find control handle lie under other when mouse over, but it only found control above (use WindowFromPoint, ChildWindowFromPoint , etc does not work).
Any suggestion?
You could use GetNextWindow Win32 API call to retrieve next window in Z-Order that lays below this one.
Algorithm could be like this:
Get initial window by HWND hwnd = ChildWindowFromPoint(hwndParent, point);
If hwnd is sought-for window then stop
hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
If hwnd contains point then go to 2
Otherwise go to 3.
Another way - just enumerate all controls on dialog or view, store their rectangles and handles in some container and do the point check manually.

Translate coords to re-position dialog box?

I am hooking CreateDialogIndirectParam. I want to do some manipulations over the dialog box, but but the width, height, and x and y positions are in dialog box units. Can someone explain how to convert them to screen coordinates?
Thanks in advance.
Try the MapDialogRect() function. I think it does what you think.
Remember that the mapping depends on the font used by the dialog, so the HWND must be that particular dialog.
Also from GetDialogBaseUnits():
pixelX = MulDiv(templateunitX, baseunitX, 4);
pixelY = MulDiv(templateunitY, baseunitY, 8);
Being baseunitX the value tmAveCharWidth and baseUnitY the value tmHeight returned by function GetTextMetrics(). You just need a HDC with the dialog font selected.

Troubles with ::MoveWindow - leaving blank space

I am quite desperate to resolve this very annoying issue :(
I am trying to display a child window on parent window. Some time the window need to be resized. But for some reason, when I using MoveWindow function it leaves blank space on the top of the parent window. I would like to present a picture here but I can not post a picture.
Here is the code example:
HWND hwnd // Comes from external function. Was defined as WS_CHILD previously
HWND hwndParent = ::GetParent(hwnd);
RECT parentRect = {0,0,0,0};
RECT childRect = {0,0,0,0};
::GetClientRect(hwndParent, &parentRect); // Suppose it returns {0,0,600,300}
BOOL ok = ::MoveWindow(hwnd, 0, 0, 600, 300, true);
::GetClientRect(hwnd, &childRect); // Will return {0,0,584,297}
WHY ?????
What am I doing wrong? Did I forgot some flags with window initialization?!
Rather than use GetClientRect, use GetWindowRect and MapWindowPoints(NULL,hwndParent,&parentRect,2) to adjust it to the parent window coordinates. GetWindowRect will include the non-client area that MoveWindow requires.
Edit: If you want a window that doesn't have a non-client area so the window rect and the client rect are the same size, you need to trim the window styles that you apply to the window. Avoid the WS_BORDER, WS_CAPTION, WS_DLGFRAME, WS_OVERLAPPED, WS_SIZEBOX, and WS_THICKFRAME styles.
MoveWindow updates window position, while GetClientRect gets a client-area part of the window, which does not have to be the same. If your window has non-client area, then everything is fine and works as expected.
If you are still under impression that child window does not fully cover parent's client area, then the spacing belongs to the child control/window, and you need to look for ways to remove it there (control flags, parameters etc).
MoveWindow operates on window coordinates -- including non-client area (borders, title bar, etc).
GetClientRect gets the area of the client portion of the window, ignoring borders, title bar, etc.
This is where the mismatch is. If you want to MoveWindow to a desired client size, you need to just AdjustWindowRect to try and predict what to pass into MoveWindow. Note that it's not always possible, and not always accurate. For example minimum / maximum sizes of windows, menus (which can wrap to multiple lines), etc.
The problem was WS_POPUP flag to the parent window.
Very strange. As far as I know it was not suppose to have such an effect.
Thanks for everyone!

Is there a way to verify that the current window of another program is totally visible?

Is there any function, or I'll have to iterate through all windows that are in front of mine and detect if they overlap my window?
Thanks
Did you try comparing the HWND returned by GetForegroundWindow() with the HWND of your application? (http://msdn.microsoft.com/en-us/library/ms633505%28VS.85%29.aspx)
To be more clear:
hwndForeground = GetForegroundWindow();
if (hwndForeground != myapp)
//Compare if rect of (hwndForeground) is overlapping rect of (myapp)

How to get width and height from CreateWindowEx() window? C++

I have made a window with CreateWindowEx() function, now how do i get the width and height from that window i created? This sounds very basic thing to do, but i just couldnt find any answer ;_;
This is needed because the window height is created automatically depending on how the Windows wants to create it.
Language C or C++
Use GetWindowRect. Subtract the right from the left to get the width and the bottom from the top to get the height.
RECT rect;
if(GetWindowRect(hwnd, &rect))
{
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
}
As a side note, if you'd like the client area instead of the entire window. You can use GetClientRect. For other information about the window you can use GetWindowInfo.
I believe you're looking for GetWindowInfo
Example:
HWND window = ::CreateWindowEx(...);
WINDOWINFO info;
if ( ::GetWindowInfo(window, &info) ) {
...
}
Have you tried GetWindowRect() or GetWindowInfo() which returns a WINDOWINFO structure?
Given there's no indication why you need the size, and that the size can change if the window style is set to include resizable attributes [and the user resizes the window using minimize/maximize/restore or drags a window edge], your safest choice is to include a message handler for WM_SIZE and use the wparam and lparam parameter values to determine window dimensions. This way, you'll always know the current size. WM_SIZE is called in the sequence of messages post window creation.