当前位置:首页 > Windows程序 > 正文

《Windows程序设计》读书笔三 窗口与消息

2021-03-18 Windows程序

第三章 窗口于消息

前面的例子都使用MessageBox来创建窗口 ,单他所创建的窗口灵活性有限。

3.1 窗口的创建

只要调用CreateWindow函数即可

3.1.1 系统结构概述

一个应用程序窗口可能包含,标题栏,菜单栏,工具栏,滚动条。另外还有一种类型的窗口是对话框,这种窗口可以不带标题栏

还可能包含,按钮,单选按钮,复选框,列表框,滚动条,文本框等。每一个这些对象都被称为 子窗口,或者 控件窗口


当用户改变窗口尺寸时,Windows便向应用程序发送一条携带新窗口尺寸相关的信息,接着应用程序对自身内容进行调整以反映出窗口尺寸的变化。


“Windows向应用程序发送了一条消息”,windows调用了该程序内部的一个函数---这个函数是由你所写,,而且是整个程序的核心。此函数的参数描述了由windows

所发送的由你的程序所接受的特定消息。这个函数被称为“窗口过程”。 windows会调用应用程序的这个函数,这种思路正式windows体系结构的基础。


应用程序所创建的每一个窗口都有一个与之对应的窗口过程。可以是应用程序中的某一个函数也可以是DLL库中的函数。窗口过程依据这些消息做相应的处理,然后将控制权返回给windows


更准确的说,窗口总是依赖窗口类来创建。窗口类标示了用于处理传递给窗口的消息的窗口过程。多个窗口可以共享一个窗口类,使用相同的窗口过程。


当windows程序创建以后,windows首先为该程序创建一个消息队列。该消息队列中存放着应用程序可能创建的所有窗口的消息。windows应用程序中通常都包含一小段消息循环的代码,该代码用于从消息队列中检索信息,并将其分发给窗口过程。


窗口, 窗口类,窗口过程,消息队列,消息循环以及窗口消息整合到实际应用中的一个例子

3.1.2 

需要在工程中加载 Winmm.lib  使用PlaySound API函数, 在VS2015中禁用

#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //window procedure. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT("HelloWin"); HWND hwnd; MSG msg; WNDCLASS wndClass; //The window Class wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc <span style="white-space:pre"> </span>= WndProc;// assign the window procedure to windows class. wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground <span style="white-space:pre"> </span>= (HBRUSH) GetStockObject(WHITE_BRUSH); wndClass.lpszMenuName <span style="white-space:pre"> </span>= NULL; wndClass.lpszClassName <span style="white-space:pre"> </span>= szAppName; //Register the Window Class to the Windows System. if (!RegisterClass(&wndClass)) { MessageBox(NULL, TEXT("This program require Windows NT!"), szAppName, MB_ICONERROR); return 0; } //This function will generate an WM_CREATE message. hwnd = CreateWindow(szAppName, //Window class name TEXT("The Hello Program"), //Window caption WS_OVERLAPPEDWINDOW, //Window Style CW_USEDEFAULT, //initial x position CW_USEDEFAULT, //initial y position CW_USEDEFAULT, //initial x size CW_USEDEFAULT, //initial y size NULL, //parent window handle NULL, //window menu handle hInstance, //program instance handle NULL); //creation parameters ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd); //This function will generate a WM_PAINT message. /* The message loop for this program. if received the WM_QUIT message, the function will return 0.*/ while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } //define the Window Procedure WndProc LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) //get the message { case WM_CREATE: PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC); return 0; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, TEXT("Hello, Windows 7 x64 Ultimate Sercice Pack 1!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }

3.1.3 通盘考虑

实际上任何windows程序的结构都与Hellowin.cpp类似。不需要记住这个框架的所有细节

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/63033.html