# Resize child window

## Recommended Posts

Decrius    100
Hi, I made a window, in the window I created 4 child windows:
HWND Create_Window_Child1(HWND hParent, char *Classname, int win_width, int win_height)
{
return CreateWindowEx(
WS_EX_CLIENTEDGE, // give it a standard border
Classname,
NULL,
WS_VISIBLE | WS_CHILD,
1, 28, (((win_width-10)/2)-2), (((win_height-100)/2)-1),
hParent,
NULL, GetModuleHandle(0), NULL
);
}
HWND Create_Window_Child2(HWND hParent, char *Classname, int win_width, int win_height)
{
return CreateWindowEx(
WS_EX_CLIENTEDGE, // give it a standard border
Classname,
NULL,
WS_VISIBLE | WS_CHILD,
1, 28+(((win_height-100)/2)-1)+4, (((win_width-10)/2)-2), (((win_height-100)/2)-2),
hParent,
NULL, GetModuleHandle(0), NULL
);
}
HWND Create_Window_Child3(HWND hParent, char *Classname, int win_width, int win_height)
{
return CreateWindowEx(
WS_EX_CLIENTEDGE, // give it a standard border
Classname,
NULL,
WS_VISIBLE | WS_CHILD,
1+(((win_width-10)/2)-2)+4, 28, (((win_width-10)/2)-2), (((win_height-100)/2)-2),
hParent,
NULL, GetModuleHandle(0), NULL
);
}
HWND Create_Window_Child4(HWND hParent, char *Classname, int win_width, int win_height)
{
return CreateWindowEx(
WS_EX_CLIENTEDGE, // give it a standard border
Classname,
NULL,
WS_VISIBLE | WS_CHILD,
1+(((win_width-10)/2)-2)+4, 28+(((win_height-100)/2)-1)+4, (((win_width-10)/2)-2), (((win_height-100)/2)-2),
hParent,
NULL, GetModuleHandle(0), NULL
);
}


win_width and win_height are the sizes of the window (512x512). When I resize the window, I want that the 4 child windows will resize too, and not stay the same, but contract or expand. So I need to do something with the
case WM_SIZE:
{
return 0;
}


function. Should I use SendMessage( or MoveWindow( ? And how to use them, noone of them worked yet. I tried them all... Any suggestion is very appreciated :) Decrius

##### Share on other sites
Evil Steve    2017
SetWindowPos() is probably what you want.
Example:
SetWindowPos(hChild, hParent, x, y, width, height, 0);

##### Share on other sites
Decrius    100

So it should be:

SetWindowPos(hChild1, hWnd, 1, 28, (((LOWORD(lParam)-10)/2)-2), (((HIWORD(lParam)-100)/2)-1), 0);

But thats not working, the child isn't sizing at all. What's wrong?

Decrius

##### Share on other sites
Evil Steve    2017
Does it work if you call that outside your WM_SIZE handler? It works fine for me... Does SetWindowPos() return any error?

##### Share on other sites
Decrius    100
Well, when the program starts, the first Child (which I try to resize) has a different size on start up, but when I resize the window, nothing happens. How can I check if the function gives an error?

Thank you anyways, :)

Decrius

PS: or do I have to redraw the window?

##### Share on other sites
Evil Steve    2017
Quote:
 Return ValuesNonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.
The only reason I can think that the function would fail is if your window handles are invalid, however.

EDIT: You shouldn't have to redraw the window, Windows will do that for you.

##### Share on other sites
Decrius    100
I got some progress, the second parameter shouldn't be 'hWnd', but something like 'HWND_BOTTOM', 'HWND_NOTTOPMOST', 'HWND_TOP' or 'HWND_TOPMOST'. For nottopmost and topmost I see no difference. But for the bottom and top things, I see nothing until I resize the window, I see them flickering, flashing (whatever :p). The flashing border of the child grows if the window expands. But when I stop resizing the flashing is gone. Its like the child window is under the surface, but I don't see how to fix it...

Thanks so far anyways :)

Decrius

##### Share on other sites
Erik Rufelt    5901
First try to resize the window to a fixed size, perhaps you have a negative width or something.. I don't know what happens if you do.

Here's how you can check for errors:
You might need to #include <stdio.h> for sprintf
BOOL bRet;bRet = SetWindowPos(	hChildWnd,	NULL,	0,	0,	500,	500,	SWP_NOZORDER);if(bRet == 0) {	DWORD dwErr = GetLastError();	char str[255];		sprintf(str, "Error %d", dwErr);	MessageBox(NULL, str, "Message", MB_OK);}

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by DecriusI got some progress, the second parameter shouldn't be 'hWnd', but something like 'HWND_BOTTOM', 'HWND_NOTTOPMOST', 'HWND_TOP' or 'HWND_TOPMOST'. For nottopmost and topmost I see no difference. But for the bottom and top things, I see nothing until I resize the window, I see them flickering, flashing (whatever :p). The flashing border of the child grows if the window expands. But when I stop resizing the flashing is gone. Its like the child window is under the surface, but I don't see how to fix it...

No necessarily [wink]
Quote:
 hWndInsertAfter[in] Handle to the window to precede the positioned window in the z-order. This parameter must be a window handle or one of the following values.
(Emphasis mine). Try what Erik Rufelt suggested, and see if that helps.

##### Share on other sites
Decrius    100
Thank you :)

I put it in the program, when I start up the program, I get one message box with:

Error: 1400

But when I resize the window I get nothing.

How to find out what error 1400 means?

Decrius

##### Share on other sites
Evil Steve    2017
In the debugger, in a watch window, put 1400,hr. You get "Invalid window handle". What is the value of the two window handles you pass in?

##### Share on other sites
Decrius    100
You mean the hwnd's?

HWND hChild;HWND Create_Window_Child1(HWND hParent, char *Classname, int win_width, int win_height);HWND Create_Window_Main(char *Classname, char *Title, int x, int y, int width, int height, BYTE type, DWORD flags){........blahdyblah........hChild1 = Create_Window_Child1(hWnd, Classname, win_width, win_height);}HWND Create_Window_Child1(HWND hParent, char *Classname, int win_width, int win_height){    return CreateWindowEx(                 WS_EX_CLIENTEDGE, // give it a standard border                 Classname,                 NULL,                 WS_VISIBLE | WS_CHILD,                 1, 28, (((win_width-10)/2)-2), (((win_height-100)/2)-1),                 hParent,                 NULL, GetModuleHandle(0), NULL               );}LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){    static PAINTSTRUCT ps;    switch(uMsg)    {........case WM_SIZE:        {            PostMessage(hWnd, WM_PAINT, 0, 0);                    bool bRet;		    bRet = SetWindowPos(hChild1, hWnd, 1, 28, (((LOWORD(lParam)-10)/2)-2), (((HIWORD(lParam)-100)/2)-1), SWP_NOZORDER);		    if(bRet == 0) {            DWORD dwErr = GetLastError();            char str[255];            sprintf(str, "Error %d", dwErr);            MessageBox(NULL, str, "Message", MB_OK);}........            return 0;        }}

Thats the code for the first Child window, thats what you required, didn't you?

Decrius

##### Share on other sites
Evil Steve    2017
Use the debugger and put a breakpoint at the SetWindowPos line, and take a look what the values of hChild1 and hWnd are. Alternatively, use sprintf() to print their values as pointers ("%p" or "0x%08X" will do).

##### Share on other sites
Decrius    100
Sorry, but I don't know what you mean.

I just press F9 and it compiles and runs...I don't know what you mean with debugger and put a breakpoint somewhere. And how can I sprintf() the values?

Sorry, but I'm not yet familiar with IDE's and compiler/debugger etc. neither my english is well enough :(.

However, thanks so far for the help :)

Decrius

##### Share on other sites
Decrius    100
Okay, I found out what a breakpoint is, and placed it on the line. And how can I read the value's of hWnd etc.?

Decrius

##### Share on other sites
Erik Rufelt    5901
It means that the HWND variable you pass to SetWindowPos is not a handle to a valid window, that is you are not telling SetWindowPos to resize your window.
You need to make sure that the hChild1 is actually the window you wish to resize.
If they are then make sure that you use global variables to store their handles, and that you have no colliding names. Make sure only one single variable in your program has the name hChild1.

##### Share on other sites
Decrius    100
I'm quite sure of that. And I declared the HWND's in a header (not in a function). So it should be global, shouldn't it? Well, I placed the code a few reply's ago, I don't know what's wrong. Maybe you guys can find out?

Decrius

##### Share on other sites
Evil Steve    2017
Quote:
 Original post by DecriusOkay, I found out what a breakpoint is, and placed it on the line. And how can I read the value's of hWnd etc.?
That depends on your compiler. Using Visual C, you can enter the variable names in a watch window.

For showing them with sprint, something like this will do:
if(bRet == 0){   char str[255];   sprintf(str, "hChild1=0x%08X, hWnd=0x%08X", hChild1, hWnd);   MessageBox(NULL, str, "Message", MB_OK);}
Just to replace the code you ahev at the moment. If either is NULL (0x00000000), or 0xcccccccc then there's a problem.

##### Share on other sites
Decrius    100
hChild1 has 0x00000->

So I'll need to find the error tomorrow.

Thanks a lot for the help :D

Sleeping time :P ;)

Decrius

##### Share on other sites
Decrius    100
Well, I got it all to work, the resizing part. But I'm not satisfied yet :P. What I did to solve the problem that the Child windows had an error was:

HWND Create_Window_Child1(HWND hParent, char *Classname){    return CreateWindowEx(                 WS_EX_CLIENTEDGE, // give it a standard border                 "EDIT",                 NULL,                 WS_VISIBLE | WS_CHILD,                 0, 0, 0, 0,                 hParent,                 NULL, GetModuleHandle(0), NULL               );}

As you might notice, I used for the Classname "EDIT" instead of the Classname of the main window. "EDIT" is a predefined classname, which will allow you to write text in the child windows. However, this is not what I require, I want to run OpenGL in the windows, so writing is it isn't allowed. What Classname should I use? Make another class...? Use the Classname of the main window (which didn't worked)? Use another Classname...?

The x, y, Δx and Δy are '0', because at the WM_SIZE part I already give them x and y etc. values.

Thanks anyways,

Decrius

EDIT: The value I give to this function (Classname) isn't used in the function, but might be handy for if I DO have to give it the Classname, and yes...I'm too lazy to remove it, and if I do need it place it :P.

Decrius    100
***BUMP***

##### Share on other sites
Evil Steve    2017
If any Windows function returns an error, you should use GetLastError() to find out what the problem was.
In this case, I'd put a breakpoint or logging statement in your window message handler for the Classname class of window, and see if the window peocedure is even called. If it is, then the problem is most likely in there (Such as returning non-zero in response to WM_CREATE).

In either case, if CreateWindowEx fails, use GetLastError afterwards to see why it failed.

##### Share on other sites
Decrius    100
Okay, thanks, I'll see if everything works.

Its really doing weird, when I give hChild1 (all 4 hChild window have the same size, I just have 4 Child windows, top-left, top-right, bottom-left, bottom-right) the same classname as the main window has, it doesn't appears. But when I resize the screen its flashing, what I see flashing are 3 smaller Child windows where the 1st (top-left) doesn't appears, the same as hChild 1 to 4 but than smaller! And in that missing first Child window, appear 3 others, with again, 1 missing. As you guess: the top-left one is missing.

Sorry, I can't post a picture, because when I press print screen, the flashing stuff doesn't appears.

Very weird :S

Decrius

##### Share on other sites
Decrius    100
But what is usual way to create custom control? Should the custom control have an own registered class? Or should the control use the class of the main window?

Decrius

##### Share on other sites
Evil Steve    2017
The custom control should be it's own class - since you need to call RegisterClass() for the sub-window, and specify a WindowProc there. Otherwise, Windows has no way of knowing where to send window messages for that window.

You did register Classname with RegisterClass[Ex](), didn't you?