A Child Window Attached to a Frame

Toolbox
 

Introduction

This is an example of creating and attaching a child window, like a toolbox, to the main frame of an application.

Resource Header

#define IDD_TOOLBOX_DLG                 101
#define IDR_MAIN_MENU                   102
#define IDM_FILE_EXIT                   40001
#define IDM_VIEW_TOOLBOX                40002

Resource Script

#include "resource.h"

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_TOOLBOX_DLG DIALOG DISCARDABLE  0, 0, 86, 249
STYLE DS_MODALFRAME | WS_CHILD
FONT 8, "MS Sans Serif"
BEGIN
END

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDR_MAIN_MENU MENU DISCARDABLE 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit",                       IDM_FILE_EXIT
    END
    POPUP "&View"
    BEGIN
        MENUITEM "&Toolbox",                    IDM_VIEW_TOOLBOX, CHECKED
    END
END


/////////////////////////////////////////////////////////////////////////////
//
// String Table
//

STRINGTABLE DISCARDABLE 
BEGIN
    IDM_FILE_EXIT           "Closes the application"
    IDM_VIEW_TOOLBOX        "Toggles the presence and disappearance of the toolbox\nShow/Hide Toolbox"
END

#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////

Source Code

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

HINSTANCE hInst;
LPTSTR strAppName = "WndFrame";
LPTSTR WndName    = "Attaching a child window to an application's frame";
LPTSTR strToolbox = "WndFloater";

HWND   hWndMainFrame, hWndToolbox;

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg,
							 WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK ToolboxProc(HWND hWnd, UINT Msg,
							 WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine, int nCmdShow)
{
    MSG   msg;
    RECT rect;
	WNDCLASSEX  WndClsEx;

	WndClsEx.cbSize        = sizeof(WNDCLASSEX);
	WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
	WndClsEx.lpfnWndProc   = MainWndProc;
	WndClsEx.cbClsExtra    = 0;
	WndClsEx.cbWndExtra    = 0;
	WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClsEx.hbrBackground = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
	WndClsEx.lpszMenuName  =  MAKEINTRESOURCE(IDR_MAIN_MENU);
	WndClsEx.lpszClassName = strAppName;
	WndClsEx.hInstance     = hInstance;
	WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if (!RegisterClassEx(&WndClsEx))
		return (FALSE);

    hInst = hInstance;

    hWndMainFrame = CreateWindow(strAppName,
			           WndName,
                                                           WS_OVERLAPPEDWINDOW,
                                                           CW_USEDEFAULT,
			           CW_USEDEFAULT,
			           CW_USEDEFAULT,
			           CW_USEDEFAULT,
			           NULL,
			           NULL,
			           hInstance,
			           NULL);

    if( !hWndMainFrame )
	return (FALSE);


    // Create a child window based on the available dialog box
    hWndToolbox = CreateDialog(hInst,
		                   MAKEINTRESOURCE(IDD_TOOLBOX_DLG),
			   hWndMainFrame,
			   (DLGPROC)ToolboxProc);

    ShowWindow (hWndToolbox, SW_SHOW);
    ShowWindow(hWndMainFrame, nCmdShow);

    while (GetMessage(&msg,NULL, 0,0))
   {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK ToolboxProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
	case WM_INITDIALOG:
		return TRUE;
	}

	return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT Msg,
			      WPARAM wParam, LPARAM lParam)
{
	HMENU hMenu;
	RECT rctMainWnd, rctToolbox;
	UINT ToolboxMenuState;

    	switch(Msg)
    	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDM_VIEW_TOOLBOX:
			hMenu = GetMenu(hWndMainFrame);
			ToolboxMenuState = GetMenuState(hMenu, IDM_VIEW_TOOLBOX, MF_BYCOMMAND);
			if( LOBYTE(ToolboxMenuState) & MF_CHECKED )
			{
				CheckMenuItem(hMenu, IDM_VIEW_TOOLBOX, MF_BYCOMMAND | MF_UNCHECKED);
				ShowWindow(hWndToolbox, SW_HIDE);
			}
			else
			{
				CheckMenuItem(hMenu, IDM_VIEW_TOOLBOX, MF_BYCOMMAND | MF_CHECKED);
				ShowWindow(hWndToolbox, SW_SHOW);
			}
			break;

		case IDM_FILE_EXIT:
			PostQuitMessage(WM_QUIT);
			return 0;
		};
		break;

	case WM_SIZE:
		GetClientRect(hWndMainFrame, &rctMainWnd);
		GetWindowRect(hWndToolbox, &rctToolbox);

		SetWindowPos(hWndToolbox,
			         HWND_TOP,
			         rctMainWnd.left,
			         rctMainWnd.top,
			         rctToolbox.right - rctToolbox.left,
			         rctMainWnd.bottom,
			         SWP_NOACTIVATE | SWP_NOOWNERZORDER);

		break;

    	case WM_DESTROY:
        		PostQuitMessage(WM_QUIT);
        		break;

    	default:
        		return DefWindowProc(hWnd, Msg, wParam, lParam);
    	}

    	return 0;
}
//---------------------------------------------------------------------------
 

Home Copyright © 2004-2014 FunctionX, Inc.