Win32多窗口程序的创建

来源:互联网 发布:视频剪辑软件推荐 编辑:程序博客网 时间:2024/04/29 02:12

本例演示如何在Win32应用程序中创建多个窗口

// Win32LevelRail1.5.cpp : Defines the entry point for the application.//#include "stdafx.h"#include "Win32LevelRail1.5.h"#define MAX_LOADSTRING 100// Global Variables:HINSTANCE hInst;// current instanceTCHAR szTitle[MAX_LOADSTRING]={L"主窗口"};// The title bar textTCHAR szWindowClass[MAX_LOADSTRING];// the main window class nameTCHAR szTitle2[MAX_LOADSTRING] = {L"第二窗口"};TCHAR szWindowClass2[MAX_LOADSTRING] = {L"Submarine"};    // 第二个窗口类的类名HWND hWnd, hWnd2; // Forward declarations of functions included in this code module:ATOMMyRegisterClass(HINSTANCE hInstance);BOOLInitInstance(HINSTANCE, int);LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);LRESULT CALLBACK    WndProc2(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACKAbout(HWND, UINT, WPARAM, LPARAM);int APIENTRY _tWinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here.MSG msg;HACCEL hAccelTable;// Initialize global stringsLoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_WIN32LEVELRAIL15, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);// Perform application initialization:if (!InitInstance (hInstance, nCmdShow)){return FALSE;}hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32LEVELRAIL15));// Main message loop:while (GetMessage(&msg, NULL, 0, 0)){if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}return (int) msg.wParam;}////  FUNCTION: MyRegisterClass()////  PURPOSE: Registers the window class.////  COMMENTS:////    This function and its usage are only necessary if you want this code//    to be compatible with Win32 systems prior to the 'RegisterClassEx'//    function that was added to Windows 95. It is important to call this function//    so that the application will get 'well formed' small icons associated//    with it.//ATOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEX wcex,wcex2;//第一个窗口类wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc= WndProc;wcex.cbClsExtra= 0;wcex.cbWndExtra= 0;wcex.hInstance= hInstance;wcex.hIcon= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32LEVELRAIL15));wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= MAKEINTRESOURCE(IDC_WIN32LEVELRAIL15);wcex.lpszClassName= szWindowClass;wcex.hIconSm= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));// 第二个窗口类的定义    wcex2.cbSize = sizeof(WNDCLASSEX);     wcex2.style            = CS_HREDRAW | CS_VREDRAW;    wcex2.lpfnWndProc    = (WNDPROC)WndProc2;    wcex2.cbClsExtra    = 0;    wcex2.cbWndExtra    = 0;    wcex2.hInstance        = hInstance;    wcex2.hIcon           = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32LEVELRAIL15);    wcex2.hCursor        = LoadCursor(NULL, IDC_ARROW);    wcex2.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);    wcex2.lpszMenuName    = MAKEINTRESOURCE(IDC_WIN32LEVELRAIL15);    wcex2.lpszClassName    = szWindowClass2;    wcex2.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);return (RegisterClassEx(&wcex)&&RegisterClassEx(&wcex2));}////   FUNCTION: InitInstance(HINSTANCE, int)////   PURPOSE: Saves instance handle and creates main window////   COMMENTS:////        In this function, we save the instance handle in a global variable and//        create and display the main program window.//BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){   HWND hWnd;   hInst = hInstance; // Store instance handle in our global variable   //创建第一个窗口   hWnd = CreateWindow(szWindowClass, /*szTitle*/L"主窗口", WS_OVERLAPPEDWINDOW,      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);   //创建第二个窗口   hWnd2 = CreateWindow(szWindowClass2, /*szTitle2*/L"第二窗口", WS_OVERLAPPEDWINDOW,      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);     if (!hWnd || !hWnd2)   {      return FALSE;   }   ShowWindow(hWnd, nCmdShow);   UpdateWindow(hWnd);   ShowWindow(hWnd2, nCmdShow);   UpdateWindow(hWnd2);   return TRUE;}////  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)////  PURPOSE:  Processes messages for the main window.////  WM_COMMAND- process the application menu//  WM_PAINT- Paint the main window//  WM_DESTROY- post a quit message and return////LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;switch (message){case WM_COMMAND:wmId    = LOWORD(wParam);wmEvent = HIWORD(wParam);// Parse the menu selections:switch (wmId){case IDM_HELP_ABOUT:DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);break;case IDM_FILE_EXIT:if(MessageBox(hWnd, L"确认离开程序", L"警告", MB_OKCANCEL)==IDOK){DestroyWindow(hWnd);}break;default:return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;}LRESULT CALLBACK WndProc2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;switch (message){case WM_COMMAND:wmId    = LOWORD(wParam);wmEvent = HIWORD(wParam);// Parse the menu selections:switch (wmId){case IDM_HELP_ABOUT:DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);break;case IDM_FILE_EXIT:if(MessageBox(hWnd, L"确认离开窗口", L"警告", MB_OKCANCEL)==IDOK){DestroyWindow(hWnd);}break;default:return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...EndPaint(hWnd, &ps);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;}// Message handler for about box.INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){UNREFERENCED_PARAMETER(lParam);switch (message){case WM_INITDIALOG:return (INT_PTR)TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL){EndDialog(hDlg, LOWORD(wParam));return (INT_PTR)TRUE;}break;}return (INT_PTR)FALSE;}

效果如下:


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 中立性细胞偏低怎么办? 孩子爱告状老师怎么办 高中学不好数学怎么办 想做老师应该怎么办 教师职称换学校怎么办 大四差选修学分怎么办 尔雅通识课学分没修满怎么办 大学全英文授课怎么办 小孩脑部有囊肿怎么办 防震期间门应该怎么办 在家待着没意思怎么办 人户分离上学怎么办 小孩上学没人接送怎么办 宝宝上学没人接送怎么办 上海浦东新区酒类许可证怎么办 金税盘里报税处理打不开怎么办 惠民卡到期了怎么办 遇到不拴狗链的主人怎么办 平安福没钱续保怎么办 提前很久到机场怎么办 机场来早了怎么办 机场去早了怎么办 只有高中学历该怎么办 没钱没学历该怎么办 中招分数压线怎么办 两岁宝宝蛀牙怎么办 大学必修课差一分怎么办 好医生差两分怎么办 万丽酒店怎么办会员 银行放款慢 业主怎么办 错过了毕业申请怎么办 初中生字写不好怎么办 对口升学没考上怎么办 天生喝不了酒怎么办 王者荣耀乱举报怎么办 易班考试不及格怎么办 易学习忘记密码怎么办 电脑qq不能登录怎么办 被钱心智迷失怎么办 自考第一次没过怎么办 宝宝不会用吸管怎么办