Click here to Skip to main content
12,071,437 members (66,100 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ VisualC++
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.

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 5-Dec-12 23:20pm
Edited 5-Dec-12 23:40pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

GetWindowRect() gives you the absolute coordinates. Using ScreenToClient() is not necessary here (the absolute coordinates are what you probably want):
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());
}
  Permalink  
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.
Jochen Arndt 6-Dec-12 6:12am
   
Don't call UpdateData() but change the content of the control by code. To do this, get a pointer to the control passing the ID to GetDlgItem() or add a control member variable for the control to your dialog (I assume you have added a CString variable for an edit control; change this to the CEdit type).

With a CEdit member variable, call SetWindwText() passing the new text.

With GetDlgItem(), call GetDlgItem(IDC_EDIT_ID)->SetWindwoText(lpszNewText);

See also a similar question I have just answered: http://www.codeproject.com/Questions/504498/Beginnersplusquestion-3aplushowplustoplusupdateplu
J.Surjith Kumar 6-Dec-12 6:40am
   
I clearly understood, everything was fine but im getting "Debug assertion failed" when im executing it.
J.Surjith Kumar 6-Dec-12 6:49am
   
Im using static control(Label). I changed the member variable from CString to CStatic.
J.Surjith Kumar 6-Dec-12 7:02am
   
The application runs well when i press the ignore button at that "Debug assertion failed" dialog box.
Jochen Arndt 6-Dec-12 8:00am
   
Then you should try to solve the issue. Press retry to see the line which asserts (probably a MFC source file) and what value is wrong. Then check the call stack. There will be some other MFC functions before one of your functions is shown. In most cases the error resides in this function. Note that the assertion must be not related to the OnMove() / OnSize() code, but be sourced by something else.
J.Surjith Kumar 6-Dec-12 8:18am
   
This is my OnMove() function code:

void CTickCounterDlg::OnMove(int x, int y)
{
CDialog::OnMove(x, y);
// TODO: Add your message handler code here
CRect dialogRect;
GetWindowRect(&dialogRect);
wchar_t *a=new wchar_t[50];
wsprintf(a,L"After move: x = %d, y = %d, width = %d, height = %d",
dialogRect.left, dialogRect.top, dialogRect.Width(), dialogRect.Height());
m_PointSize.SetWindowTextW(a);
}

I pressed the retry button.

Its pointing to the winocc.cpp

void CWnd::SetWindowText(LPCTSTR lpszString)
{
ENSURE(this);
ENSURE(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));

if (m_pCtrlSite == NULL)
::SetWindowText(m_hWnd, lpszString);
else
m_pCtrlSite->SetWindowText(lpszString);
}

How to find out the solution for that problem.
Jochen Arndt 6-Dec-12 8:53am
   
I assume that the second ENSURE() fails because thw window is not created.

What type is m_PointSize (it must be a CStatic here)?

Is it connected to the control?
There must be a message map entry in your cpp file like
DDX_Control(pDX, <ID of control>, m_PointSize);
Maybe you still have it mapped to the old CString object.

One more note:
You missed the 'delete [] a' at the end of OnMove(). Why did you allocate a buffer with a small fixed size? Just use 'wchar_t a[50];'. Or better use a CString object with MFC so that the size does not care:
CStringW a;
a.Format(L"x = %d...", dialogRect.left, ...);
m_PointSize.SetWindowText(a.GetString());
J.Surjith Kumar 6-Dec-12 9:49am
   
Thank you Jochen Arndt for responding from the beginning. Im doing all corrections what ur telling in my program but am still getting that "Debug assertion failed".
Jochen Arndt 6-Dec-12 10:04am
   
Did you check the call stack (it's in one of the tabbed windows like the output window)?

To know which window is involved, see the local window when the assertion occurs after pressing Retry. The content of the lpszString variable may give you a hint. When clicking the '+' on this, you can see the derived class (e.g. CStatic, CEdit) and the content of all member vars. Using these information you can identify the control and what went wrong (e.g. m_hWnd is NULL). Then you must check your source code why it went wrong.
J.Surjith Kumar 6-Dec-12 23:54pm
   
In that it showing m_hWnd unused. error:expression cannot be evaluated.
J.Surjith Kumar 7-Dec-12 0:00am
   
When i click the '+' its showing (65 L'A').
Jochen Arndt 7-Dec-12 2:55am
   
Sorry, my comment was misleading: Click '+' on the 'this' tree item to see the member vars. But it seems that th 'A' is the first letter of the m_PointSize string.
If m_hWnd is unused / NULL, the window has not been created. That is, you have the m_PointSize object but there is no window associated. So I still believe that this DoDataExchange() entry is missing: DDX_Control(pDX, , m_PointSize);
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100