Win32-窗口注册和创建-MDI注册和创建

/*窗口注册*/

// 系统全局窗口类 - BUTTON
#include <windows.h>

HINSTANCE g_hInst = NULL;

/*
窗口类分为下面3种:
系统全局窗口类;
应用程序全局窗口类;
局部窗口类;

1.WinMain入口函数
2.窗口过程处理函数
3.注册窗口
4.创建窗口
5.显示窗口
6.消息循环
*/

// 窗口过程处理函数
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT uMsg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// 创建一个系统全局窗口,这个窗口不需要注册
// 系统默认就注册好了,直接使用
// 例如:下面是创建一个系统全局的按钮,这个按钮
// 她的类名就是BUTTON不能更改为其他的
HWND CreateButton()
{
    HWND hWnd = CreateWindow("BUTTON", 
                             "Button",
                             WS_OVERLAPPEDWINDOW,
                             0,
                             0,
                             500,
                             600,
                             NULL,
                             NULL,
                             g_hInst,
                             NULL);
    return hWnd;
}

// 显示窗口
void DisplayWnd(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

// 消息循环
void Message()
{
    MSG msg = {0};

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

// WinMain入口函数
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR pszCmdLine,
                     int nShowCmd)
{
    g_hInst = hInstance;
    HWND hWndBtn = CreateButton();
    DisplayWnd(hWndBtn);
    Message();
    return 0;
}

---------------------------------------------------------------------------
// 系统全局窗口类 - EDIT
#include <windows.h>

HINSTANCE g_hInst = NULL;

// 窗口过程函数
LRESULT CALLBACK WinProc(HWND hWnd,
                         UINT uMsg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// 创建系统窗口全局类
HWND CreateButton()
{
    HWND hwnd = CreateWindow("EDIT",
        "Button", WS_OVERLAPPEDWINDOW,
        0, 0, 600, 600, NULL, NULL, g_hInst, NULL );
    return hwnd;
}

// 显示窗口
void DisplayWnd(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

// 消息窗口
void Message()
{
    MSG msg = {0};
    
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

// WinMain入口函数
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    g_hInst = hInstance;
    HWND hWndBtn = CreateButton();
    DisplayWnd(hWndBtn);
    Message();
    return 0;
}

-----------------------------------------------------------------------
// 创建应用程序全局窗口类
#include <windows.h>

HINSTANCE g_hInst = NULL;

// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT uMsg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// 显示窗口
void DisplayWnd(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

// 消息窗口
void Message()
{
    MSG msg = {0};
    
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

// 注册窗口
BOOL RegisterWnd(LPSTR pszClassName)
{
    WNDCLASSEX wce = {0};

    wce.cbSize = sizeof(wce);// 增加了这个结构体的宽度字段
    wce.style  = CS_VREDRAW|CS_HREDRAW;
    wce.lpfnWndProc = WndProc;
    wce.cbClsExtra = 100;
    wce.cbWndExtra = 200;
    wce.hCursor = NULL;
    wce.hIcon = NULL;
    wce.hbrBackground = HBRUSH(COLOR_BTNFACE+1);
    wce.lpszClassName = pszClassName;
    wce.hIconSm = NULL;
    wce.hInstance = g_hInst;

    ATOM nAtom = RegisterClassEx(&wce);
    if (nAtom == 0)
    {
        return FALSE;
    }
    return TRUE;
}

HWND CreateWnd(LPSTR pszClassName)
{
    HWND hWnd = CreateWindow(pszClassName,
        "MyWindowsCntf", WS_OVERLAPPEDWINDOW,
        0, 0, 800, 600, NULL, NULL, g_hInst, NULL);
    return hWnd;
}

// WinMain入口函数
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    g_hInst = hInstance;

    RegisterWnd("MyWindowsCC");
    HWND hMyWindows = CreateWnd("MyWindowsCC");
    DisplayWnd(hMyWindows);

    Message();
    return 0;
}

-----------------------------------------------------------------

// 窗口附加信息

#include <windows.h>
#include <stdio.h>

HINSTANCE g_hInst = NULL;

// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT uMsg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// 显示窗口
void DisplayWnd(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

// 消息窗口
void Message()
{
    MSG msg = {0};
    
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

// 注册窗口
BOOL RegisterWnd(LPSTR pszClassName)
{
    WNDCLASSEX wce = {0};

    wce.cbSize = sizeof(wce);// 增加了这个结构体的宽度字段
    wce.style  = CS_VREDRAW|CS_HREDRAW;
    wce.lpfnWndProc = WndProc;
    wce.cbClsExtra = 100;
    wce.cbWndExtra = 200;
    wce.hCursor = NULL;
    wce.hIcon = NULL;
    wce.hbrBackground = HBRUSH(COLOR_BTNFACE+1);
    wce.lpszClassName = pszClassName;
    wce.hIconSm = NULL;
    wce.hInstance = g_hInst;

    ATOM nAtom = RegisterClassEx(&wce);
    if (nAtom == 0)
    {
        return FALSE;
    }
    return TRUE;
}

HWND CreateWnd(LPSTR pszClassName)
{
    HWND hWnd = CreateWindow(pszClassName,
        "MyWindowsCntf", WS_OVERLAPPEDWINDOW,
        0, 0, 800, 600, NULL, NULL, g_hInst, NULL);
    return hWnd;
}

// 设置窗口附加信息
void SetExtra(HWND hWnd)
{
    // 设置CLASS附加数据
    SetClassLong(hWnd, 2, 400);
    // 设置Window附加数据
    SetWindowLong(hWnd, 2, 800);
}

// 获取窗口附加信息
void GetExtra(HWND hWnd)
{
    // 获取CLASS附加数据
    DWORD nClass =GetClassLong(hWnd, 2);
    // 获取Window附加数据
    DWORD nWnd = GetWindowLong(hWnd, 2);
    // 让其显示
    CHAR szText[280] = {0};
    sprintf(szText, "CLASS:%d WINDOW:%d",nClass, nWnd);
    MessageBox(NULL, szText, "Extra", MB_OK);
}

// WinMain入口函数
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    g_hInst = hInstance;

    RegisterWnd("MyWindowsCC");
    HWND hMyWindows = CreateWnd("MyWindowsCC");
    HWND hMyWndYC = CreateWnd("MyWindowsCC");
    DisplayWnd(hMyWindows);
    GetExtra(hMyWndYC);
    SetExtra(hMyWindows);
    GetExtra(hMyWindows);

    Message();
    return 0;
}

------------------------------------------------------------------------

窗口创建

// WinCreate.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <stdio.h>

// 全局变量应用程序实例句柄
HINSTANCE g_hInst = NULL;

// 窗口过程调用函数
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT uMsg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// 创建窗口类并注册此类
BOOL MyRegisterWnd(LPSTR pszClassName)
{
    WNDCLASSEX wce = {0};

    wce.cbSize          = sizeof(wce);
    wce.style           = CS_HREDRAW|CS_VREDRAW;
    wce.cbClsExtra      = 0;
    wce.cbWndExtra      = 0;
    wce.lpfnWndProc     = WndProc;
    wce.hIcon           = NULL;
    wce.hCursor         = NULL;
    wce.hbrBackground   = HBRUSH(COLOR_WINDOW);
    wce.hInstance       = g_hInst;
    wce.lpszClassName   = pszClassName;
    wce.lpszMenuName    = NULL;
    wce.hIconSm         = NULL;

    ATOM nAtom = RegisterClassEx(&wce);
    if (nAtom == 0)
    {
        return FALSE;
    }
    return TRUE;
}

// 创建窗口
HWND CreateWnd(LPSTR pszClassName)
{
    HWND hWnd = CreateWindowEx(0, 
                               pszClassName,
                               "MyWindowsCNTFCC",
                               WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               NULL,
                               NULL,
                               g_hInst,
                               NULL
                               );
    return hWnd;
}

// 窗口显示
void DisplayWnd(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

// 消息循环
void Message()
{
    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        DispatchMessage(&msg);
    }
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
    g_hInst = hInstance;

    MyRegisterWnd("CncfWindowsCC");
    HWND hWnd = CreateWnd("CncfWindowsCC");
    DisplayWnd(hWnd);
    Message();

	return 0;
}

--------------------------------------------------------------------------------

// 传递多个参数创建窗口

#include "stdafx.h"
#include <stdio.h>

// 全局变量应用程序实例句柄
HINSTANCE g_hInst = NULL;

// 窗口过程调用函数
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT uMsg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK ChildProc( HWND hWnd,
                           UINT nMsg,
                           WPARAM wParam,
                           LPARAM lParam )
{
    return DefWindowProc( hWnd, nMsg, 
        wParam, lParam );
}

// 创建窗口类并注册此类
BOOL MyRegisterWnd(LPSTR pszClassName, WNDPROC proc, int nBrush)
{
    WNDCLASSEX wce = {0};

    wce.cbSize          = sizeof(wce);
    wce.style           = CS_HREDRAW|CS_VREDRAW;
    wce.cbClsExtra      = 0;
    wce.cbWndExtra      = 0;
    wce.lpfnWndProc     = proc;
    wce.hIcon           = NULL;
    wce.hCursor         = NULL;
    wce.hbrBackground   = HBRUSH(nBrush);
    wce.hInstance       = g_hInst;
    wce.lpszClassName   = pszClassName;
    wce.lpszMenuName    = NULL;
    wce.hIconSm         = NULL;

    ATOM nAtom = RegisterClassEx(&wce);
    if (nAtom == 0)
    {
        return FALSE;
    }
    return TRUE;
}

// 创建窗口
HWND CreateWnd(LPSTR pszClassName, DWORD nStyle, HWND hParent)
{
    HWND hWnd = CreateWindowEx(0, 
                               pszClassName,
                               "MyWindowsCNTFCC",
                               nStyle,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               hParent,
                               NULL,
                               g_hInst,
                               NULL
                               );
    return hWnd;
}

// 窗口显示
void DisplayWnd(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

// 消息循环
void Message()
{
    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        DispatchMessage(&msg);
    }
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
    g_hInst = hInstance;

    MyRegisterWnd("CncfWindowsCC", WndProc, COLOR_BTNFACE+1);
    HWND hWnd = CreateWnd("CncfWindowsCC", WS_OVERLAPPEDWINDOW, NULL);
    DisplayWnd(hWnd);
    Message();

	return 0;
}

--------------------------------------------------------------------------------
// 子窗口创建

// WinCreate.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <stdio.h>

// 全局变量应用程序实例句柄
HINSTANCE g_hInst = NULL;

// 父窗口过程调用函数
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT uMsg,
                         WPARAM wParam,
                         LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// 子窗口过程处理函数
LRESULT CALLBACK ChildProc( HWND hWnd,
                           UINT nMsg,
                           WPARAM wParam,
                           LPARAM lParam )
{
    return DefWindowProc( hWnd, nMsg, 
        wParam, lParam );
}

// 创建窗口类并注册此类
BOOL MyRegisterWnd(LPSTR pszClassName, WNDPROC proc, int nBrush)
{
    WNDCLASSEX wce = {0};

    wce.cbSize          = sizeof(wce);
    wce.style           = CS_HREDRAW|CS_VREDRAW;
    wce.cbClsExtra      = 0;
    wce.cbWndExtra      = 0;
    wce.lpfnWndProc     = proc;
    wce.hIcon           = NULL;
    wce.hCursor         = NULL;
    wce.hbrBackground   = HBRUSH(nBrush);
    wce.hInstance       = g_hInst;
    wce.lpszClassName   = pszClassName;
    wce.lpszMenuName    = NULL;
    wce.hIconSm         = NULL;

    ATOM nAtom = RegisterClassEx(&wce);
    if (nAtom == 0)
    {
        return FALSE;
    }
    return TRUE;
}

// 创建窗口
HWND CreateWnd(LPSTR pszClassName, DWORD nStyle, HWND hParent)
{
    HWND hWnd = CreateWindowEx(0, 
                               pszClassName,
                               "MyWindowsCNTFCC",
                               nStyle,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               CW_USEDEFAULT,
                               hParent,
                               NULL,
                               g_hInst,
                               NULL
                               );
    return hWnd;
}

// 窗口显示
void DisplayWnd(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

// 消息处理函数
void Message()
{
    MSG msg = {0};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        DispatchMessage(&msg);
    }
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    g_hInst = hInstance;

    // 注册父窗口类
    MyRegisterWnd("CncfWindowsCC", WndProc, COLOR_BTNFACE+1);

    // 注册子窗口类
    MyRegisterWnd("CntfChildWndCC", ChildProc, COLOR_WINDOW);

    // 创建父窗口
    HWND hWnd = CreateWnd("CncfWindowsCC", WS_OVERLAPPEDWINDOW, NULL);

    // 创建子窗口
    HWND hChild1 = CreateWnd("CntfChildWndCC", 
        WS_CHILD|WS_VISIBLE|WS_BORDER|
        WS_THICKFRAME|WS_CAPTION|WS_SYSMENU|
		WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
        hWnd);

    HWND hChild2 = CreateWnd("CntfChildWndCC", 
        WS_POPUP|WS_VISIBLE|
        WS_THICKFRAME|WS_CAPTION|WS_SYSMENU, 
        hWnd );

    HWND hChild3 = CreateWnd("CntfChildWndCC", 
        WS_CHILD|WS_VISIBLE|WS_BORDER, 
		hWnd );

    // 移动窗口位置
    MoveWindow( hChild1, 100, 100, 
        200, 200, TRUE );
    MoveWindow( hChild2, 100, 200, 
        200, 200, TRUE );
    MoveWindow( hChild3, 100, 300, 
		200, 200, TRUE );

    // 显示父窗口
    DisplayWnd(hWnd);
    Message();

	return 0;
}

--------------------------------------------------------------------------------
// 使用MDI创建主窗口和子窗口

// WinMDI.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

HINSTANCE g_hInst = NULL;
HWND g_hMDIClient = NULL;

//主窗口的窗口处理函数
LRESULT CALLBACK MainProc( HWND hWnd,
						   UINT nMsg,
						   WPARAM wParam,
						   LPARAM lParam )
{
	switch( nMsg )
	{
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	}
	return DefFrameProc( hWnd, g_hMDIClient,
		nMsg, wParam, lParam );
}
//子窗口的窗口处理函数
LRESULT CALLBACK ChildProc( HWND hWnd,
						    UINT nMsg,
							WPARAM wParam,
							LPARAM lParam )
{
	return DefMDIChildProc( hWnd, nMsg,
		wParam, lParam );
}
//窗口注册函数
BOOL RegisterWnd( LPSTR   pszClassName,
				  WNDPROC Proc,
				  int     nBrush )
{
	WNDCLASSEX wce = { 0 };
	wce.cbSize     = sizeof( wce );
	wce.style      = CS_HREDRAW|CS_VREDRAW;
	wce.cbClsExtra = 0;
	wce.cbWndExtra = 0;
	wce.lpfnWndProc= Proc;
	wce.hInstance  = g_hInst;
	wce.hCursor    = NULL;
	wce.hIcon      = NULL;
	wce.hbrBackground = HBRUSH(nBrush);
	wce.lpszClassName = pszClassName;
	wce.lpszMenuName  = NULL;
	wce.hIconSm       = NULL;

	ATOM nAtom = RegisterClassEx( &wce );
	if( nAtom == 0 )
	{
		return FALSE;
	}
	return TRUE;
}
//显示窗口
void DisplayWnd( HWND hWnd )
{
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );
}
//消息循环
void Message( )
{
	MSG msg = { 0 };
	while( GetMessage( &msg, NULL, 0, 0 ) )
	{
		DispatchMessage( &msg );
	}
}
//创建主窗口
HWND CreateMainWnd( LPSTR pszClassName )
{
	HWND hWnd = CreateWindowEx( 0,
		pszClassName, "MainWnd", 
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, NULL, NULL, g_hInst,
		NULL );
	return hWnd;
}
//创建MDICLIENT窗口
HWND CreateMDIClient( HWND hParent )
{
	CLIENTCREATESTRUCT cs = { 0 };
	cs.idFirstChild = 1000;
	HWND hWnd = CreateWindowEx( 0,
		"MDICLIENT", "MainWnd", 
		WS_CHILD|WS_VISIBLE, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, hParent, NULL, g_hInst,
		&cs );
	return hWnd;
}
//创建MDI子窗口
HWND CreateChildWnd( LPSTR pszClassName, 
					 HWND hParent )
{
	HWND hWnd = CreateWindowEx( WS_EX_MDICHILD,
		pszClassName, "ChildWnd", 
		WS_CHILD|WS_VISIBLE, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, hParent, NULL, g_hInst,
		NULL );
	return hWnd;
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{	//注册主窗口
	RegisterWnd( "MainWnd", MainProc, 
		COLOR_BTNFACE+1 );
	//注册子窗口
	RegisterWnd( "ChildWnd", ChildProc,
		COLOR_WINDOW );
	//创建MDI主窗口
	HWND hMain = CreateMainWnd( "MainWnd" );
	//创建MDICLIENT窗口
	g_hMDIClient = CreateMDIClient( hMain );
	MoveWindow( g_hMDIClient, 0, 0, 500, 
		500, TRUE );
	//创建MDI子窗口
	CreateChildWnd( "ChildWnd", 
		g_hMDIClient );
	CreateChildWnd( "ChildWnd", 
		g_hMDIClient );
	CreateChildWnd( "ChildWnd", 
		g_hMDIClient );

	//显示和消息处理
	DisplayWnd( hMain );
	Message( );
	return 0;
}

--------------------------------------------------------------------------------

// WinMDI.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

HINSTANCE g_hInst = NULL;
HWND g_hMDIClient = NULL;

//主窗口的窗口处理函数
LRESULT CALLBACK MainProc( HWND hWnd,
						   UINT nMsg,
						   WPARAM wParam,
						   LPARAM lParam )
{
	switch( nMsg )
	{
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	}
	return DefFrameProc( hWnd, g_hMDIClient,
		nMsg, wParam, lParam );
}
//子窗口的窗口处理函数
LRESULT CALLBACK ChildProc( HWND hWnd,
						    UINT nMsg,
							WPARAM wParam,
							LPARAM lParam )
{
	return DefMDIChildProc( hWnd, nMsg,
		wParam, lParam );
}
//窗口注册函数
BOOL RegisterWnd( LPSTR   pszClassName,
				  WNDPROC Proc,
				  int     nBrush )
{
	WNDCLASSEX wce = { 0 };
	wce.cbSize     = sizeof( wce );
	wce.style      = CS_HREDRAW|CS_VREDRAW;
	wce.cbClsExtra = 0;
	wce.cbWndExtra = 0;
	wce.lpfnWndProc= Proc;
	wce.hInstance  = g_hInst;
	wce.hCursor    = NULL;
	wce.hIcon      = NULL;
	wce.hbrBackground = HBRUSH(nBrush);
	wce.lpszClassName = pszClassName;
	wce.lpszMenuName  = NULL;
	wce.hIconSm       = NULL;

	ATOM nAtom = RegisterClassEx( &wce );
	if( nAtom == 0 )
	{
		return FALSE;
	}
	return TRUE;
}
//显示窗口
void DisplayWnd( HWND hWnd )
{
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );
}
//消息循环
void Message( )
{
	MSG msg = { 0 };
	while( GetMessage( &msg, NULL, 0, 0 ) )
	{
		DispatchMessage( &msg );
	}
}
//创建主窗口
HWND CreateMainWnd( LPSTR pszClassName )
{
	HWND hWnd = CreateWindowEx( 0,
		pszClassName, "MainWnd", 
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, NULL, NULL, g_hInst,
		NULL );
	return hWnd;
}
//创建MDICLIENT窗口
HWND CreateMDIClient( HWND hParent )
{
	CLIENTCREATESTRUCT cs = { 0 };
	cs.idFirstChild = 1000;
	HWND hWnd = CreateWindowEx( 0,
		"MDICLIENT", "MainWnd", 
		WS_CHILD|WS_VISIBLE, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, hParent, NULL, g_hInst,
		&cs );
	return hWnd;
}
//创建MDI子窗口
HWND CreateChildWnd( LPSTR pszClassName, 
					 HWND hParent )
{
	HWND hWnd = CreateWindowEx( WS_EX_MDICHILD,
		pszClassName, "ChildWnd", 
		WS_CHILD|WS_VISIBLE, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, hParent, NULL, g_hInst,
		NULL );
	return hWnd;
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{	//注册主窗口
	RegisterWnd( "MainWnd", MainProc, 
		COLOR_BTNFACE+1 );
	//注册子窗口
	RegisterWnd( "ChildWnd", ChildProc,
		COLOR_WINDOW );
	//创建MDI主窗口
	HWND hMain = CreateMainWnd( "MainWnd" );
	//创建MDICLIENT窗口
	g_hMDIClient = CreateMDIClient( hMain );
	MoveWindow( g_hMDIClient, 0, 0, 500, 
		500, TRUE );
	//创建MDI子窗口
	CreateChildWnd( "ChildWnd", 
		g_hMDIClient );
	CreateChildWnd( "ChildWnd", 
		g_hMDIClient );
	CreateChildWnd( "ChildWnd", 
		g_hMDIClient );

	//显示和消息处理
	DisplayWnd( hMain );
	Message( );
	return 0;
}

效果展示

窗口注册和创建&添加附加信息

image

窗口和子窗口创建

image

使用MDI创建主窗口和子窗口

image

posted @ 2021-11-13 18:54  皇帽讲绿帽带法技巧  阅读(312)  评论(0编辑  收藏  举报