Archived

This topic is now archived and is closed to further replies.

Win32 question - MDI

This topic is 5886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m writing a program using MDI (Multiple Document Interface) and I''m getting some rather odd behavior. It works fine, until I do the following: Create 2 (or more) MDI child windows Maximize 1 of the MDI child windows Close the maximized MDI child window Then it proceeds to close ALL MDI child windows. Any ideas why this might be happening?

Share this post


Link to post
Share on other sites
Kind of short on time, but see if you are doing all of the following.

Use DefFrameProc instead of DefWindowProc in your frame window.
In your child window, use DefMDIChildProc.

Just return 0 in your WM_DESTROY message in your child window proc.

Also, make sure you are allocating memory for each of your child windows.

Finally, remember to use MDICREATESTRUCT and pass messages such as WM_MDICREATE rather than WM_CREATE.

Share this post


Link to post
Share on other sites
I have no idea what the problem is. Maybe someone can find out what the problem is.

  
#include <windows.h>
#include <string>
#include "resource.h"

using namespace std;

#define ID_MDI_FIRSTCHILD 50000

HWND mainWindow;
HWND mdiClient;
HINSTANCE appInstance;
char children = ''1'';

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_CREATE:
{
CLIENTCREATESTRUCT ccs;

ccs.hWindowMenu = GetSubMenu(GetMenu(hwnd), 1);
ccs.idFirstChild = ID_MDI_FIRSTCHILD;

mdiClient = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient", NULL, WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwnd, NULL, appInstance, (LPVOID)&ccs);

ShowWindow(mdiClient, SW_SHOW);
} break;

case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;

hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
} break;

case WM_DESTROY:
{
PostQuitMessage(0);
} break;

case WM_COMMAND:
{
switch(LOWORD(wparam))
{
case FILE_EXIT:
{
PostMessage(hwnd, WM_CLOSE, 0, 0);
} break;

case FILE_NEW:
{
MDICREATESTRUCT mcs;
HWND hchild;
string title;

title = "Untitled";
title += children;

mcs.szTitle = title.c_str();
mcs.szClass = "mdiChild";
mcs.hOwner = appInstance;
mcs.x = CW_USEDEFAULT;
mcs.y = CW_USEDEFAULT;
mcs.cx = CW_USEDEFAULT;
mcs.cy = CW_USEDEFAULT;
mcs.style = MDIS_ALLCHILDSTYLES;

hchild = (HWND)SendMessage(mdiClient, WM_MDICREATE, 0, (LONG)&mcs);

if(!hchild)
{
MessageBox(hwnd, "Could not create MDI Child", "Error", MB_ICONEXCLAMATION | MB_OK);
}
} break;

case WINDOW_CASCADE:
{
PostMessage(mdiClient, WM_MDICASCADE, 0, 0);
} break;

case WINDOW_TILEH:
{
PostMessage(mdiClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
} break;

case WINDOW_TILEV:
{
PostMessage(mdiClient, WM_MDITILE, MDITILE_VERTICAL, 0);
} break;

default:
{
if(LOWORD(wparam) >= ID_MDI_FIRSTCHILD)
{
DefFrameProc(hwnd, mdiClient, msg, wparam, lparam);
}
else
{
HWND hChild;
hChild = (HWND)SendMessage(mdiClient, WM_MDIGETACTIVE,0,0);
if(hChild)
{
SendMessage(hChild, WM_COMMAND, wparam, lparam);
}
}
}
}
} break;

default:break;
}

return DefFrameProc(hwnd, mdiClient, msg, wparam, lparam);
}

LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_CREATE:
{
children++;
} break;

case WM_SIZE:
{

} break;

case WM_MDIACTIVATE:
{

} break;

case WM_SETFOCUS:
{

} break;

case WM_COMMAND:
{

} break;

case WM_CLOSE:
{
int result;

result = MessageBox(hwnd, "Are you sure you want to close this window?", "Confirmation", MB_YESNO);

if(result == IDNO)
return 0;
else
children--;

} break;

default: break;
}

return DefMDIChildProc(hwnd, msg, wparam, lparam);
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
WNDCLASSEX winclass;
MSG msg;

winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)(COLOR_3DSHADOW+1);
winclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
winclass.lpszClassName = "winclass1";
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&winclass))
{
MessageBox(NULL, "Could not register main window class", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

winclass.lpfnWndProc = MDIChildWndProc;
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "mdiChild";
winclass.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);

if(!RegisterClassEx(&winclass))
{
MessageBox(NULL, "Could not register MDI child window class", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

mainWindow = CreateWindowEx(NULL, "winclass1", "Winserv", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN | WS_MAXIMIZE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hinstance, NULL);

ShowWindow(mainWindow, SW_MAXIMIZE);

if (!mainWindow)
{
MessageBox(NULL, "Could not create main window", "Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

appInstance = hinstance;

while(GetMessage(&msg, NULL, 0, 0))
{
if(!TranslateMDISysAccel(mdiClient, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return(msg.wParam);
}

Share this post


Link to post
Share on other sites