Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to get the position,width and length of the dialog box when im moving that dialog box and that should be displayed in that dialog itself.I used OnMove and OnSize but it doesn't work in the dialog based applications. The given below code is what i know to get the coordinates while moving the window.

C#
void CTickCounterDlg::OnMove(int x, int y)
{
    CDialog::OnMove(x, y);
    // TODO: Add your message handler code here
wchar_t *s=new wchar_t[20];
wsprintf(s,L"%d|%d",x,y);
m_size.Format(L"%s",s);
//CRect dialogRect;
//GetWindowRect(&dialogRect);
//ScreenToClient(dialogRect);
//int a=dialogRect.left;
//int b=dialogRect.right;
//wsprintf(s,L"%d|%d",a,b);
//m_size.Format(L"%s",s);
}

void CTickCounterDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);
    // TODO: Add your message handler code here
wchar_t *sz=new wchar_t[20];
wsprintf(sz,L"%d|%d",cx,cy);
m_ScreenSize.Format(L"%s",sz);
}


Anyone tell me the solutions for the dialog based applications.

Thanks & regards
J.Surjith Kumar
Posted
Updated 5-Dec-12 22:40pm
v3

GetWindowRect() gives you the absolute coordinates. Using ScreenToClient() is not necessary here (the absolute coordinates are what you probably want):
C++
void CTickCounterDlg::OnMove(int x, int y)
{
    CRect dialogRect;
    GetWindowRect(&dialogRect);
    m_Size.Format(L"Before move: x = %d, y = %d, width = %d, height = %d", 
        dialogRect.left, dialogRect.top, dialogRect.Width(), dialogRect.Height());
    CDialog::OnMove(x, y);
    GetWindowRect(&dialogRect);
    m_Size.Format(L"After move: x = %d, y = %d, width = %d, height = %d", 
        dialogRect.left, dialogRect.top, dialogRect.Width(), dialogRect.Height());
}
 
Share this answer
 
Comments
J.Surjith Kumar 6-Dec-12 5:33am    
my dialog box is at the center but it showing x and y position as 0. And the value is not updated.
Jochen Arndt 6-Dec-12 5:53am    
I've just tested it and my code is showing the coordinates when moving the window.

Maybe you are confused by screen and client coordinates. The x and y parameters are the new top left position of the client area in screen coordinates. So they will differ from the position returned by GetWindowRect() (x offset is the border with and y offset is the border width plus height of the caption bar).

When calling GetClientRect(), the returned left and top values are always zero and the right and bottom values will be the width resp. height of the client area.

In general:


Use GetClientRect() when you want to do something in the client area of a window like drawing text.

Use GetWindowRect() when want to do something with the window / dialog like changing size and position.
J.Surjith Kumar 6-Dec-12 5:58am    
Ok i got it. The only problem is that the value is not updating in the controls what im using to display in the dialog box.
J.Surjith Kumar 6-Dec-12 5:59am    
If i use UpdateData(FALSE) at the end of the statement it shows debug assertion failed.
J.Surjith Kumar 6-Dec-12 6:01am    
And i should not use the UpdateData(FALSE) because it will refresh all other controls at the same time. For this case what should i do.
dear friend
you have to write the "getting" features in a timer event.
when you're clicking on the dialog,in "OnLCickDown" event you have to enable and call the "OnTimer" event.
in OnTimer you have to get Rect of your dialog with GetWindowRect function.
after that you can get what you want.
it's easy and works.be sure my friend.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900