foobar
foobar

Reputation: 2943

MFC: Set controls position at runtime

I have a mfc dialog window with some button control and an activeX web browser control. For now I am dragging-dropping the controls over my dialog window so their position is somewhat hard coded and not relative to dialog. I want to set their position at runtime , when the dialog window opens.

I tried doing SetWindowPos(GetDlgItem(IDC_EXPLORER), 0,0,a,b,1); but its not doing anything. What else can I try?

Upvotes: 0

Views: 11920

Answers (3)

Afriza N. Arief
Afriza N. Arief

Reputation: 7876

If you have a lot of controls, using DeferWindowPos combined with BeginDeferWindowPos and EndDeferWindowPos may improve performance and reduce flickering.

Sample from Raymond Chen:

HDWP hdwp = BeginDeferWindowPos(2);
int cxHalf = cx/2;
if (hdwp) hdwp = DeferWindowPos(hdwp, g_hwndChildren[0],
             NULL, 0, 0, cxHalf, cy,
             SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
if (hdwp) hdwp = DeferWindowPos(hdwp, g_hwndChildren[1],
             NULL, cxHalf, 0, cx-cxHalf, cy,
             SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
if (hdwp) EndDeferWindowPos(hdwp);

Upvotes: 1

Werner Henze
Werner Henze

Reputation: 16726

Your question is not clear about if you want to change only the x-/y-position of the window or also the z-position. I assume you only want to x-/y-move it.

You need to apply SetWindowPos on the window whose position you want to change. You don't pass the window to move to SetWindowPos. And please don't pass 1 as a flag, please pass the SWP_ defines.

 GetDlgItem(IDC_EXPLORER)->SetWindowPos(&wndTop, 0, 0, a, b, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);

or even simpler:

GetDlgItem(IDC_EXPLORER)->MoveWindow(0, 0, a, b);

Upvotes: 1

snowdude
snowdude

Reputation: 3874

SetWindowPos is what you've just used the wrong parameters. The final flag you've specified SWP_NOSIZE but I don't think that's what you want. Try this:

GetDlgItem(IDC_EXPLORER)->SetWindowPos( NULL, 0, 0, a, b, SWP_NOZORDER | SWP_NOMOVE | SWP_SHOWWINDOW );

If you're looking to dynamically resize your controls based on the parent. Here's an absolute gem of a solution:

http://www.codeguru.com/cpp/misc/misc/article.php/c323/Method-to-achieve-repositioningresizing-of-child-controls.htm

Upvotes: 2

Related Questions