Someone can explain me how to convert dialog box units to screen coordinates values ?
I saw that there is MapDialogRect function, but its converting RECT, i want to convert the x,y and cx,cy values to screen coordinate values and i dont really understand how to achieve this.
Thanks in advance.
If you already have a window handle, then just use the MapDialogRect function. As others have noted, MapDialogRect takes a RECT, so if you don't have a RECT, you can create one.
RECT rc;
rc.left = x;
rc.top = y;
rc.right = x + cx;
rc.bottom = y + cy;
MapDialogRect(hdlg, &rc);
If your problem is that you don't have a dialog box handle in the first place, then the documentation for the MapDialogRect function tells you how to perform the calculations: Determine the the average character dimensions for the dialog box (which the documentation calls baseunitX and baseunitY) and then plug it into the formulas.
Note that this calculates the client rectangle of the dialog box. You still have to add non-client space. It's not clear what you're trying to do, so I don't know whether adding non-client space is appropriate or not.
Related
I need to perform mouse click on a particular element in a window. I have calculated the coordinates of that element in my system and written a code to perform mouse click on that particular coordinate.
The code works fine for my machine. However, if I run it on a different machine the click operation is performed in a different position. It's because the screen size is different. How do I perform the mouse click on the same element in any machine regardless of window size. Is there any other way to locate an element on the screen? like percentage instead of pixels? Will it work?
RECT rect;
GetWindowRect(hwnd, &rect);
int x = rect.left;
int y = rect.top;
SetCursorPos(x + 570, y + 450);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
In the above code I have set the cursor position with respect to my system. The element's XY coordinate is different for other machines with bigger screens. Any hint would be of much help.
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.
My application's main window is starting to have lots of stuff so I need a vertical scrollbar to fit everything inside client area. I coded a scrollbar control, WM_VSCROLL messages like SB_LINEDOWN are being processed and scrollbar moves nicely. The last part is to make the content of a main window to move along with the thumb of a scrollbar and it seems a bit hard task for me. This is my best try:
int dy = -( CurrPos - si.nPos );
RECT rctMainWindowArea = { 0, 0, 1000, main_window.bottom };
ScrollWindowEx( hwndMainWindow, 0, dy,( CONST RECT * ) &rctMainWindowArea,( CONST RECT * ) NULL,( HRGN ) NULL,( LPRECT ) NULL, SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE );
UpdateWindow( hwndMainWindow );
It works as long I'm scrolling down. When I scroll back up again everything gets messed up. I've been googling about this issue for a while and it seems that I have to redraw the lost client area of main window. However I have no idea how to do it. I've found on the web only examples where text is being scrolled inside edit control. I need to scroll whole main window which has couple of different basic controls, some bmp graphic, some other graphic elements like TextOut(), RoundRect() and so on.
I need some code examples how to solve my issue or at least some simple explanation (I'm amateur programmer). Thanks a lot !
Windows doesn't keep track of how much the window has scrolled, so when it asks you to repaint part of the window, you need to change what you paint based on how much scrolling you've done.
The easiest way to do this is to adjust the window origin to match the amount of scrolling you've done. Your WM_PAINT handler might look something like this. offsetX and offsetY are the distances you've scrolled in the X and Y directions respectively.
// Adjust coordinates to automatically scroll
POINT origin;
GetWindowOrgEx(hdc, &origin);
SetWindowOrgEx(hdc, origin.x + offsetX, origin.y + offsetY, 0);
// Move the paint rectangle into the new coordinate system
OffsetRect(&ps.rcPaint, offsetX, offsetY);
// Do the painting
// Change this to call your painting function
CWindow::DoPaint(hdc, ps);
// Restore coordinates
SetWindowOrgEx(hdc, origin.x, origin.y, 0);
I try to draw a rectangle on a mfc window using the instructions by: http://msdn.microsoft.com/en-US/library/8w4fzfxf%28v=VS.80%29.aspx . Much though I tried, the Rectangle appears on the border of the window covering the whole of it. What is the problem with the following code int the function OnDraw(CDC* pDC) ? What can be done to draw a Rectangle with particular coordinates in the window?
CPen penBlack;
penBlack.CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
CPen* pOldPen = pDC->SelectObject(&penBlack);
CPoint pt(10, 10);
CSize sz(100, 50);
CRect myRect(pt, sz);
GetClientRect(&myRect);
pDC->Rectangle(&myRect);
Drop the call to GetClientRect.
That function will write to the rectangle object passed to it, so by calling, you're overwriting your specific coordinates that you set up just before the call using pt and sz.
As #stakx suggested you should remove the GetClientRect, which gets the whole window client area, and overwrites your own rectangle.
As to the instruction, it first gets the whole client area, and shrinks the rectangle to get the rectangle to draw, so GetClientRect is needed there.
This site will help you to draw the rectangle in mfc Dialog-based-application.
http://cboard.cprogramming.com/windows-programming/37788-drawing-mfc.html
http://cboard.cprogramming.com/cplusplus-programming/102490-cplusplus-mfc-rectangle-class.html
Don't use GetClientRect().It will override your previous coordinates.
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.