从零学起----Windows程序设计笔记(二) 窗口

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

#pragma comment(lib,"winmm.lib")
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND hwnd;
    MSG msg;

    WNDCLASS wndclass;
    wndclass.style        = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc  = WndProc;
    wndclass.cbClsExtra   = 0;
    wndclass.cbWndExtra   = 0;
    wndclass.hInstance    = hInstance;
    wndclass.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName= szAppName;

    if (!RegisterClass (&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
        return 0;
    }
    
    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);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam ;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    switch (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!"), -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);
}

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

1、Windows函数

LoadIcon 加载图标供程序使用。
LoadCursor 加载鼠标光标供程序使用。
GetStockObject 取得一个图形对象(在这个例子中,是取得绘制窗口背景的画刷对象)。
RegisterClass 为程序窗口注册窗口类别。
MessageBox 显示消息框。
CreateWindow 根据窗口类别建立一个窗口。
ShowWindow 在屏幕上显示窗口。
UpdateWindow 指示窗口自我更新。
GetMessage 从消息队列中取得消息。
TranslateMessage 转译某些键盘消息。
DispatchMessage 将消息发送给窗口消息处理程序。
PlaySound 播放一个声音文件。
BeginPaint 开始绘制窗口。
GetClientRect 取得窗口显示区域的大小。
DrawText 显示字符串。
EndPaint 结束绘制窗口。
PostQuitMessage 在消息队列中插入一个「退出程序」消息。
DefWindowProc 执行内定的消息处理。

2、数值常数前缀

前缀 类别
CS 窗口类别
CW 建立窗口
DT 绘制文字
IDI 图示ID
IDC 游标ID
MB 消息框
SND 声音
WM 窗口消息
WS 窗口样式


3、Windows表头文件中定义的四种数据结构

结构 含义
MSG 消息结构
WNDCLASS 窗口类别结构
PAINTSTRUCT 绘图结构
RECT 矩形结构


4、句柄简介

HINSTANCE 执行实体(程序自身)句柄
HWND 窗口句柄
HDC 设备内容句柄

5、匈牙利表示法

c char或WCHAR或TCHAR
by BYTE (无正负号字符)
n short
i int
x, y int分别用作x坐标和y坐标
cx, cy int分别用作x长度和y长度;C代表「计数器」
b或f BOOL (int);f代表「旗标」
w WORD (无正负号短整数)
l LONG (长整数)
dw DWORD (无正负号长整数)
fn function(函数)
s string(字符串)
sz 以字节值0结尾的字符串
h 句柄
p 指标

6、注册窗口类别

    WNDCLASS wndclass;
    wndclass.style        = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc  = WndProc;
    wndclass.cbClsExtra   = 0;
    wndclass.cbWndExtra   = 0;
    wndclass.hInstance    = hInstance;
    wndclass.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName= szAppName;

    if (!RegisterClass (&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
        return 0;
    }

第二个字段(lpfnWndProc) 是依据这个类别来建立的所有窗口所使用的窗口消息处理程序的地址,是WndProc函数。

最后一个字段是窗口类别的文字名称。程序写作者可以随意定义其名称。在只建立一个窗口的程序中,窗口类别名称通常设定为程序名称。

#define     CS_VREDRAW           0x0001
#define     CS_HREDRAW           0x0002        
#define     CS_KEYCVTWINDOW      0x0004        
#define     CS_DBLCLKS           0x0008        
#define     CS_OWNDC             0x0020       
#define     CS_CLASSDC           0x0040       
#define     CS_PARENTDC          0x0080       
#define     CS_NOKEYCVT          0x0100       
#define     CS_NOCLOSE           0x0200      
#define     CS_SAVEBITS          0x0800       
#define     CS_BYTEALIGNCLIENT   0x1000       
#define     CS_BYTEALIGNWINDOW   0x2000       
#define     CS_GLOBALCLASS       0x4000       
#define     CS_IME               0x00010000


wndclass.style = CS_HREDRAW | CS_VREDRAW ;
每当窗口的水平方向大小(CS_HREDRAW)或者垂直方向大小(CS_VREDRAW)改变之后,窗口要完全重画。
wndclass.lpfnWndProc = WndProc ;

将这个窗口类别的窗口消息处理程序设定为WndProc。
wndclass.hInstance = hInstance ;

程序的执行实体句柄。

wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;

设置一个图标。

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

加载一个预先定义的鼠标光标。
wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;

窗口显示区域的背景完全为白色。

wndclass.lpszMenuName = NULL ;

指定窗口类别菜单。
wndclass.lpszClassName = szAppName ;
必须给出一个类别名称。对于小程序,类别名称可以与程序名相同,即存放在szAppName变量中的「HelloWin」字符串。

在初始化该结构的10个字段后,呼叫RegisterClass来注册这个窗口类别。

7、建立窗口

窗口类别定义了窗口的一般特征,因此可以使用同一窗口类别建立许多不同的窗口。

    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

8、显示窗口
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

9、消息循环

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






原文地址:https://www.cnblogs.com/cyendra/p/3681540.html