Windows零基础入门:2.23 窗口类结构体之窗口类风格CS_NOCLOSE

3978 人浏览 | 时间: 2015-09-19 22:49:29 | 作者: codexia 会员文章,禁止转载

    这节课我们来讲讲下一个窗口类风格CS_NOCLOSE。我是按照窗口类分组介绍的。
    在MSDN中的解释就是禁用了Windows窗口上的关闭系统菜单的关闭菜单项。对于禁用关闭系统菜单的关闭菜单项,你可能就只是以为就是屏蔽了右上角的那个叉叉按钮。其实,我们还需要了解一些其他的知识哦。
    首先我们来看看正常的窗口和禁用关闭菜单项的窗口的区别,见下两张图。

   

            图1:正常窗口

   

            图2:禁用了关闭系统菜单的关闭菜单项的窗口

#include <Windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, PSTR szCmdLine, int iCmdShow)
{
    HWND hwnd;
    MSG  msg;
    WNDCLASS wndclass;
    wndclass.style =  CS_VREDRAW | CS_HREDRAW | CS_NOCLOSE;
    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 = L"C1";

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, L"注册窗口类失败,此程序需要运行在Windows NT平台下。", L"C1", MB_ICONERROR);
        return 0;
    }

    hwnd = CreateWindow(L"C1", L"主窗口", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    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;
    HWND hwnd2;
    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        DrawText(hdc, L"主窗口", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
        SetTextColor(hdc, RGB(0, 0, 255));// - 设置
        EndPaint(hwnd, &ps);
        return 0;
    case WM_RBUTTONDOWN:
        PostQuitMessage(0);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}
当前文章为会员文章,请前往[用户中心]开通会员后继续阅读。

Win32课程菜单