Discussion:
Setwindowpos()-problem.Moving control is not happening.
(too old to reply)
austin
2005-11-28 05:39:23 UTC
Permalink
Hi all,


I am facing a problem in using SetWindowPos() . I am handling a
localization project. For this i need to resize and reposition the
dialog controls. I am calling GetWindowRect () to get the control
dimensions and after changing them i am trying to call SetWindowPos()
to place the control to the changed position.


The following is the code i am using ..

RECT gControlDimension;
HWND hCtrl;

//Handle to the Dialog control
hCtrl = GetDlgItem (hWnd, ID_CURRENT);

//Get the control dimensions
status1 = GetWindowRect (hCtrl, &gControlDimension);
ctrlLeft = gControlDimension.left ;
ctrlRight = gControlDimension.right;
ctrlTop = gControlDimension.top;
ctrlBottom = gControlDimension.bottom;

ctrlWidth = ctrlRight - ctrlLeft;
ctrlHeight = ctrlBottom - ctrlTop;

/*

//Here i am using the logic to change
ctrlLeft,ctrlRight,ctrlWidth,ctrlHeight.
//I am not adding that here as it is coming to around 20 lines.

*/
if (onlyresize)
{

SetWindowPos ( hCtrl,NULL, ctrlLeft, ctrlTop, ctrlWidth, ctrlHeight,
SWP_NOMOVE );

}
else//both resize and move
{
SetWindowPos ( hCtrl,NULL, ctrlLeft, ctrlTop, ctrlWidth,
ctrlHeight, SWP_NOZORDER);
}

This last check is failing for me. if onlyresize means the window is
getting resized properly and showing me the output window which is a
resized one properly.
But if it needs to move ie in the else part the window is not
showing.Why so?? I tried with all the flags, other than SWP_NOMOVE none
of the other flags are showing me the window at all.I added showwindow
after Setwindowpos in else part but still the result remains same.
If I have to use MoveWindow() how can i use it better??

pls let me know where it is going wrong.

Thanks in advance
suresh.
Scott McPhillips [MVP]
2005-11-28 08:48:44 UTC
Permalink
Post by austin
Hi all,
I am facing a problem in using SetWindowPos() . I am handling a
localization project. For this i need to resize and reposition the
dialog controls. I am calling GetWindowRect () to get the control
dimensions and after changing them i am trying to call SetWindowPos()
to place the control to the changed position.
The following is the code i am using ..
RECT gControlDimension;
HWND hCtrl;
//Handle to the Dialog control
hCtrl = GetDlgItem (hWnd, ID_CURRENT);
//Get the control dimensions
status1 = GetWindowRect (hCtrl, &gControlDimension);
ctrlLeft = gControlDimension.left ;
ctrlRight = gControlDimension.right;
ctrlTop = gControlDimension.top;
ctrlBottom = gControlDimension.bottom;
ctrlWidth = ctrlRight - ctrlLeft;
ctrlHeight = ctrlBottom - ctrlTop;
/*
//Here i am using the logic to change
ctrlLeft,ctrlRight,ctrlWidth,ctrlHeight.
//I am not adding that here as it is coming to around 20 lines.
*/
if (onlyresize)
{
SetWindowPos ( hCtrl,NULL, ctrlLeft, ctrlTop, ctrlWidth, ctrlHeight,
SWP_NOMOVE );
}
else//both resize and move
{
SetWindowPos ( hCtrl,NULL, ctrlLeft, ctrlTop, ctrlWidth,
ctrlHeight, SWP_NOZORDER);
}
This last check is failing for me. if onlyresize means the window is
getting resized properly and showing me the output window which is a
resized one properly.
But if it needs to move ie in the else part the window is not
showing.Why so?? I tried with all the flags, other than SWP_NOMOVE none
of the other flags are showing me the window at all.I added showwindow
after Setwindowpos in else part but still the result remains same.
If I have to use MoveWindow() how can i use it better??
pls let me know where it is going wrong.
Thanks in advance
suresh.
Your ctrlLeft and ctrlTop variables probably move the control outside of
the dialog dimensions. Check their values. (These variables are not
used in the SWP_NOMOVE case.)

It looks like you have not adjusted the control rectangle coordinates
with ScreenToClient().
--
Scott McPhillips [VC++ MVP]
Joseph M. Newcomer
2005-11-29 16:22:46 UTC
Permalink
See below...
Post by austin
Hi all,
I am facing a problem in using SetWindowPos() . I am handling a
localization project. For this i need to resize and reposition the
dialog controls. I am calling GetWindowRect () to get the control
dimensions and after changing them i am trying to call SetWindowPos()
to place the control to the changed position.
The following is the code i am using ..
RECT gControlDimension;
HWND hCtrl;
//Handle to the Dialog control
hCtrl = GetDlgItem (hWnd, ID_CURRENT);
//Get the control dimensions
status1 = GetWindowRect (hCtrl, &gControlDimension);
ctrlLeft = gControlDimension.left ;
ctrlRight = gControlDimension.right;
ctrlTop = gControlDimension.top;
ctrlBottom = gControlDimension.bottom;
ctrlWidth = ctrlRight - ctrlLeft;
ctrlHeight = ctrlBottom - ctrlTop;
/*
//Here i am using the logic to change
ctrlLeft,ctrlRight,ctrlWidth,ctrlHeight.
//I am not adding that here as it is coming to around 20 lines.
*/
if (onlyresize)
{
SetWindowPos ( hCtrl,NULL, ctrlLeft, ctrlTop, ctrlWidth, ctrlHeight,
SWP_NOMOVE );
}
else//both resize and move
{
SetWindowPos ( hCtrl,NULL, ctrlLeft, ctrlTop, ctrlWidth,
ctrlHeight, SWP_NOZORDER);
}
****
SetWindowPos using a control needs the coordinates in Client Coordinates of the parent.
GetWindowRect returns the Screen Coordinates of the control, and you must first do
ScreenToClient to convert them.

Also, try to avoid GetDlgItem. See my essay on Avoding GetDlgItem on my MVP Tips site.

Also, use Hungarian Notation correctly or not at all. The "g" prefix suggests a global
variable. Since it conveys no meaningful information in this context, since you appear to
be using a local variable, its presence is confusing. In HN, the correct prefix would
have been "rc" or "rect". The prefix "ctrl" suggests a control type, but in fact these
are int values. It is bad enough that HN exists at all without having people introduce
usages that are inconsistent with it (however internally consistent it might be in your
app). Better to use no prefixes at all than to use prefixes which are confusing and
misleading.
****
Post by austin
This last check is failing for me. if onlyresize means the window is
getting resized properly and showing me the output window which is a
resized one properly.
But if it needs to move ie in the else part the window is not
showing.Why so??
****
Because of the failure to transform to client coordinates
*****
Post by austin
I tried with all the flags, other than SWP_NOMOVE none
of the other flags are showing me the window at all.I added showwindow
after Setwindowpos in else part but still the result remains same.
If I have to use MoveWindow() how can i use it better??
pls let me know where it is going wrong.
Thanks in advance
suresh.
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
austin
2005-12-02 05:10:34 UTC
Permalink
hi all,

Thanks for all your suggestions. yes, once i converted the window
co-cordinates to that of the client area coordinates, it is doing well.
By the way my project is not using MFC support, so I am going with
GetDlgItem (). Thanks again..

Friends I have another query.
How can we change the title of an AfxMessageBox, as it always takes the
caption alone as input .I cant find the place where to change it's
title.

thanks & regards,
Suresh
Aman Anand
2022-12-27 06:59:34 UTC
Permalink
Post by austin
hi all,
Thanks for all your suggestions. yes, once i converted the window
co-cordinates to that of the client area coordinates, it is doing well.
By the way my project is not using MFC support, so I am going with
GetDlgItem (). Thanks again..
Friends I have another query.
How can we change the title of an AfxMessageBox, as it always takes the
caption alone as input .I cant find the place where to change it's
title.
thanks & regards,
Suresh
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
SetWindowTextW(_T("CheckMark 1099 E-File ")); //To write the header name of the dialog
}
Aman Anand
2022-12-27 07:00:33 UTC
Permalink
Post by austin
hi all,
Thanks for all your suggestions. yes, once i converted the window
co-cordinates to that of the client area coordinates, it is doing well.
By the way my project is not using MFC support, so I am going with
GetDlgItem (). Thanks again..
Friends I have another query.
How can we change the title of an AfxMessageBox, as it always takes the
caption alone as input .I cant find the place where to change it's
title.
thanks & regards,
Suresh
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
SetWindowTextW(_T("Name of The header ")); //To write the header name of the dialog
}

Loading...