Newbie Question - I can't create a mdi child window

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I used the VS2010 and created a win 32 program, that auto generated some code, and I modified it to support mdi child windows.
This line keeps failing, and I think the hMDIClient is not formed correctly
hChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
error code 1400 is returned by GetLastError
I highlighed the code that I think is failing. Perhaps a quick glance by an expert could reveal my mistake. Thanks


<div style="color:black; background-color:white
<pre><span style="color:green // NASE2012_SUWizard.cpp : Defines the entry point for the application.
<span style="color:green //

#include <span style="color:#a31515 "stdafx.h"
#include <Winuser.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <commctrl.h>
#include <span style="color:#a31515 "resource.h"
#include <span style="color:#a31515 "NASE2012_SUWizard.h"

#define MAX_LOADSTRING 100

<span style="color:green // Global Variables:
HINSTANCE hInst; <span style="color:green // Current instance
HINSTANCE hInst_GAC; <span style="color:green // Secondary instance
HINSTANCE hInst_MDI; <span style="color:green // MDI instance

TCHAR szTitle[MAX_LOADSTRING]; <span style="color:green // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; <span style="color:green // The main window class name
TCHAR szChildWindowClass[MAX_LOADSTRING]; <span style="color:green //The MDI Child Window Class Name
HWND g_hMDIClient = NULL;

<span style="color:green // Forward declarations of functions included in this code module:
ATOM RegisterWindowClass(HINSTANCE hInstance);
BOOL RegisterMDIChildWindowClass(HINSTANCE);

BOOL InitInstance(HINSTANCE, <span style="color:blue int);
LRESULT CALLBACK MPFrameWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MDIChildWndProc(HWND, UINT, WPARAM, LPARAM);

HWND CreateNewMDIChild(HWND);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

<span style="color:green // WinMain Function
<span style="color:blue int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
<span style="color:blue int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

<span style="color:green // TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

<span style="color:green // Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_NASE2012_SUWIZARD, szWindowClass, MAX_LOADSTRING);
LoadString(hInstance, ID_MDI_FIRSTCHILD, szChildWindowClass, MAX_LOADSTRING);

<span style="color:green //Intialize the MDI Parent Window Class
RegisterWindowClass(hInstance);

<span style="color:green // Perform application initialization:
<span style="color:blue if (!InitInstance (hInstance, nCmdShow))
{
<span style="color:blue return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_NASE2012_SUWIZARD));

<span style="color:green //Intialize MDI Child Window Class
<span style="color:blue if(!RegisterMDIChildWindowClass(hInstance))
<span style="color:blue return 0;

<span style="color:green // Main message loop:
<span style="color:blue while (GetMessage(&msg, NULL, 0, 0))
{
<span style="color:blue if (!TranslateMDISysAccel(g_hMDIClient, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

<span style="color:blue return (<span style="color:blue int) msg.wParam;
}

ATOM RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = <span style="color:blue sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC) MPFrameWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_NASE2012_SUWIZARD));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_NASE2012_SUWIZARD);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

<span style="color:blue return RegisterClassEx(&wcex);
}

BOOL RegisterMDIChildWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = <span style="color:blue sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC) MDIChildWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
wcex.lpszMenuName = (LPCTSTR) NULL;
wcex.lpszClassName = szChildWindowClass;
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

<span style="color:blue if(!RegisterClassEx(&wcex) == 0)
{
MessageBox(0,
L<span style="color:#a31515 "The MDI Child Window Class could not be registered properly. This is a severe error, and the program will terminate.",
L<span style="color:#a31515 "Register MDI Child Window Class",
MB_ICONEXCLAMATION | MB_OK);
<span style="color:blue return FALSE;
}
<span style="color:blue else
<span style="color:blue return TRUE;
}

BOOL InitInstance(HINSTANCE hInstance, <span style="color:blue int nCmdShow)
{
HWND hWnd;
hInst_GAC = hInstance; <span style="color:green // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

<span style="color:blue if (!hWnd)
{
<span style="color:blue return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

<span style="color:blue return TRUE;
}

LRESULT CALLBACK MPFrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
<span style="color:blue int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

<span style="color:blue switch (message)
{

<span style="color:blue case WM_CREATE:
{
CLIENTCREATESTRUCT ccs;

ccs.hWindowMenu = GetSubMenu(GetMenu(hWnd), 2);
ccs.idFirstChild = ID_MDI_FIRSTCHILD;

CREATESTRUCT* pCreateStruct;
MDICREATESTRUCT* pMDICreateStruct;

pCreateStruct = (CREATESTRUCT*)lParam;
pMDICreateStruct = (MDICREATESTRUCT*)pCreateStruct->lpCreateParams;

//I think my gMDIClient is bad
g_hMDIClient = CreateWindowEx(
WS_EX_CLIENTEDGE,
szChildWindowClass,
NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hWnd,
(HMENU)IDC_MAIN_MDI,
GetModuleHandle(NULL),
(LPVOID)&ccs
);

<span style="color:green /*
pMDICreateStruct now points to the same MDICREATESTRUCT that you
sent along with the WM_MDICREATE message and you can use it
to access the lParam.
*/
}

<span style="color:blue case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
<span style="color:blue switch (wmId) {
<span style="color:blue case IDM_WEBSERVER_INSTALL:
CreateNewMDIChild(g_hMDIClient);
<span style="color:blue break;

<span style="color:blue case IDM_WEBSERVER_DEFAULT_WEBSITE:
<span style="color:blue break;

<span style="color:blue case IDM_WEBSERVER_CREATE_WEBSITE:
<span style="color:blue break;

<span style="color:blue case IDM_WEBSERVER_UNINSTALL:
<span style="color:blue break;

<span style="color:blue case IDM_SQLSERVER_SCAN:
<span style="color:blue break;

<span style="color:blue case IDM_SQLSERVER_CREATE_CREDENTIALS:
<span style="color:blue break;

<span style="color:blue case IDM_SQLSERVER_CREATE_DATABASE:
<span style="color:blue break;

<span style="color:blue case IDM_SQLSERVER_POPULATE_DATABASE:
<span style="color:blue break;

<span style="color:blue case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
<span style="color:blue break;

<span style="color:blue case IDM_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0);
<span style="color:blue break;

<span style="color:blue default:

<span style="color:blue if (LOWORD(wParam) >= ID_MDI_FIRSTCHILD)
{
DefFrameProc(hWnd, g_hMDIClient, message, wParam, lParam);
}
<span style="color:blue else
{
HWND hChild = (HWND)SendMessage(g_hMDIClient, WM_MDIGETACTIVE,0,0);
<span style="color:blue if(hChild)
{
SendMessage(hChild, WM_COMMAND, wParam, lParam);
}
}
}
<span style="color:blue break;

<span style="color:blue case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
<span style="color:green // TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
<span style="color:blue break;

<span style="color:blue case WM_DESTROY:
PostQuitMessage(0);
<span style="color:blue break;

<span style="color:blue default:
<span style="color:green //return DefWindowProc(hWnd, message, wParam, lParam);
<span style="color:blue return DefFrameProc(hWnd, g_hMDIClient, message, wParam, lParam);
}
<span style="color:blue return 0;
}

LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
<span style="color:blue switch(msg)
{
<span style="color:blue case WM_CREATE:
{
HFONT hfDefault;
HWND hChild;

hChild = CreateWindowEx(WS_EX_CLIENTEDGE, L<span style="color:#a31515 "MDIClient", L<span style="color:#a31515 "",
WS_CHILD | WS_VISIBLE,
0, 0, 100, 100, hwnd, NULL, GetModuleHandle(NULL), NULL);

<span style="color:blue if(hChild == NULL)
MessageBox(hwnd, L<span style="color:#a31515 "Scan for SQL Servers has failed", L<span style="color:#a31515 "The program could not create a MDI Child Window", MB_OK | MB_ICONERROR);

hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hChild, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
}
<span style="color:blue break;
<span style="color:blue case WM_MDIACTIVATE:
{

}
<span style="color:blue break;
<span style="color:blue case WM_COMMAND:
<span style="color:blue switch(LOWORD(wParam))
{

}
<span style="color:blue break;
<span style="color:blue case WM_SIZE:
{

}
<span style="color:blue return DefMDIChildProc(hwnd, msg, wParam, lParam);

<span style="color:blue default:
<span style="color:blue return DefMDIChildProc(hwnd, msg, wParam, lParam);

}
<span style="color:blue return 0;
}

<span style="color:green // Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
<span style="color:blue switch (message)
{
<span style="color:blue case WM_INITDIALOG:
<span style="color:blue return (INT_PTR)TRUE;

<span style="color:blue case WM_COMMAND:
<span style="color:blue if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
<span style="color:blue return (INT_PTR)TRUE;
}
<span style="color:blue break;
}
<span style="color:blue return (INT_PTR)FALSE;
}

HWND CreateNewMDIChild(HWND hMDIClient)
{
MDICREATESTRUCT mcs;
HWND hChild;

mcs.szTitle = L<span style="color:#a31515 "[Scan for SQL Servers]";
mcs.szClass = szChildWindowClass;
mcs.hOwner = GetModuleHandle(NULL);
mcs.x = mcs.cx = CW_USEDEFAULT;
mcs.y = mcs.cy = CW_USEDEFAULT;
mcs.style = WS_MAXIMIZE;

hChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);

<span style="color:blue if(!hChild)
{
MessageBox(hMDIClient,
L<span style="color:#a31515 "The call to create a new MDI Child window has failed.",
L<span style="color:#a31515 "Create New MDI Child Window.",
MB_ICONEXCLAMATION | MB_OK);
}
<span style="color:blue return hChild;
}
[/code]

<br/>
<br/>


<br/>
<br/>
<br/>

View the full article
 
Back
Top