Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!evax!utacfd!merch!spudge!thorp From: thorp@spudge.UUCP (Don Thorp) Newsgroups: comp.windows.ms Subject: Child Windows Keywords: windows 3.0 Message-ID: <20995@spudge.UUCP> Date: 7 Sep 90 20:58:11 GMT Reply-To: thorp@spudge.UUCP (Don Thorp) Distribution: usa Organization: Friends of Guru Bob Lines: 254 I'm having problems with the following code. I was attempting to create a listbox as a child window to my main application window. I originally tried to create the listbox in the WM_CREATE section of the MainWindowProc. This always returns a NULL handle for the window. The section of code that is enabled by defining THIS_DOES_NOT_WORK was the original attempt. I then moved the same code into the InitApplication procedure and it worked just fine. I have been stumped for quite awhile on how this could be. Any suggestions would be greatly appreciated. Don Thorp ========================================================================== #include "windows.h" /* required for all Windows applications */ #define NULL 0 #include "wmail.h" /* specific to this program */ HANDLE hInst; /* current instance */ HWND hMainWindow; /* Main window handle. */ HWND hMailList; char szChildClass[] = "listbox"; int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow) HANDLE hInstance; /* current instance */ HANDLE hPrevInstance; /* previous instance */ LPSTR lpCmdLine; /* command line */ int nCmdShow; /* show-window type (open/icon) */ { MSG msg; /* message */ if (!hPrevInstance) { if (!InitApplication(hInstance)) { return (FALSE); /* Exits if unable to initialize */ } } if (!InitInstance(hInstance, nCmdShow)) { return (FALSE); } /* Acquire and dispatch messages until a WM_QUIT message is received. */ while (GetMessage(&msg, NULL, NULL, NULL)) { TranslateMessage(&msg); /* Translates virtual key codes */ DispatchMessage(&msg); /* Dispatches message to window */ } return (msg.wParam); /* Returns the value from PostQuitMessage */ } BOOL InitApplication(hInstance) HANDLE hInstance; /* current instance */ { WNDCLASS wc; /* Fill in window class structure with parameters that describe the */ /* main window. */ wc.style = NULL; /* Class style(s). */ wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */ /* windows of this class. */ wc.cbClsExtra = 0; /* No per-class extra data. */ wc.cbWndExtra = 0; /* No per-window extra data. */ wc.hInstance = hInstance; /* Application that owns the class. */ wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockObject(WHITE_BRUSH); wc.lpszMenuName = "WMailMenu"; /* Name of menu resource in .RC file. */ wc.lpszClassName = "WMailWClass"; /* Name used in call to CreateWindow. */ /* Register the window class and return success/failure code. */ if(!RegisterClass(&wc)) { MessageBeep(880); return(FALSE); } return(TRUE); } BOOL InitInstance(hInstance, nCmdShow) HANDLE hInstance; /* Current instance identifier. */ int nCmdShow; /* Param for first ShowWindow() call. */ { HWND hWnd; /* Main window handle. */ /* Save the instance handle in static variable, which will be used in */ /* many subsequence calls from this application to Windows. */ hInst = hInstance; /* Create a main window for this application instance. */ hWnd = CreateWindow( "WMailWClass", /* See RegisterClass() call. */ "WMail", /* Text for window title bar. */ WS_OVERLAPPEDWINDOW , CW_USEDEFAULT, /* Default horizontal position. */ CW_USEDEFAULT, /* Default vertical position. */ CW_USEDEFAULT, /* Default width. */ CW_USEDEFAULT, /* Default height. */ NULL, /* Overlapped windows have no parent. */ NULL, /* Use the window class menu. */ hInstance, /* This instance owns this window. */ NULL /* Pointer not needed. */ ); /* If window could not be created, return "failure" */ if (!hWnd) { return (FALSE); } /* Make the window visible; update its client area; and return "success" */ hMainWindow = hWnd; ShowWindow(hWnd, nCmdShow); /* Show the window */ UpdateWindow(hWnd); /* Sends WM_PAINT message */ #if defined(THIS_WORKS) hMailList = CreateWindow(szChildClass, NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD, 10,10, 200,200, hWnd, IDC_LISTBOX, hInst, NULL); if(!hMailList) { return(FALSE); } { LONG rc; rc = SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); displayWordError("LB_ADDSTRING",(WORD) rc); SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); } #endif return (TRUE); /* Returns the value from PostQuitMessage */ } LONG FAR PASCAL MainWndProc(hWnd, message, wParam, lParam) HWND hWnd; /* window handle */ unsigned message; /* type of message */ WORD wParam; /* additional information */ LONG lParam; /* additional information */ { FARPROC lpProcAbout; /* pointer to the "About" function */ LONG rc; switch (message) { #if defined(THIS_DOES_NOT_WORK) case WM_CREATE: hMailList = CreateWindow(szChildClass, NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD, 10,10, 200,200, hWnd, IDC_LISTBOX, hInst, NULL); if(!hMailList) { MessageBeep(440); break; } else { LONG rc; rc = SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); displayWordError("LB_ADDSTRING",(WORD) rc); SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); SendMessage(hMailList,LB_ADDSTRING, NULL, (LONG) (LPSTR) "Hello"); } break; #endif case WM_COMMAND: /* message: command from application menu */ if (wParam == IDM_ABOUT) { lpProcAbout = MakeProcInstance(About, hInst); DialogBox(hInst, "ABOUT", hWnd, lpProcAbout); FreeProcInstance(lpProcAbout); } else { /* Lets Windows process it */ return (DefWindowProc(hWnd, message, wParam, lParam)); } break; case WM_DESTROY: /* message: window being destroyed */ PostQuitMessage(0); break; default: /* Passes it on if unproccessed */ return (DefWindowProc(hWnd, message, wParam, lParam)); } return (NULL); } ========================================================================== Don Thorp UUCP: ...!letni!rwsys!spudge!thorp USENET: thorp@rwsys.lonestar.org BIX: dthorp