⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dc1.cpp

📁 WINDOWS2000开发人员指南源代码第五章的
💻 CPP
字号:
// dc1.cpp: shows how to display output on a device context
//

#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                        // current instance
TCHAR szTitle[MAX_LOADSTRING];          // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];    // The title bar text

// Foward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
void                OnPaint(HWND hWnd);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

//
// WinMain: Entry point for the application
//
int APIENTRY WinMain
(
    HINSTANCE hInstance
   ,HINSTANCE /*hPrevInstance*/
   ,LPSTR     /*lpCmdLine*/
   ,int       nCmdShow
)
{
    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_DC1, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow)) 
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DC1);

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}

//
// MyRegisterClass: registers the window class
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, (LPCTSTR)IDI_DC1);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = (LPCSTR)IDC_DC1;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

    return RegisterClassEx(&wcex);
}

//
// InitInstance: creates the window
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    BOOL bReturn(FALSE);
    HWND hWnd;

    // Store instance handle in our global variable
    hInst = hInstance; 

    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    if (hWnd)
    {
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
        bReturn = TRUE;
    } 

   return bReturn;
}

//
//  WndProc: main window window procedure
//
LRESULT CALLBACK WndProc
(
    HWND hWnd
   ,UINT message
   ,WPARAM wParam
   ,LPARAM lParam
)
{
    int wmId, wmEvent;

    switch (message) 
    {
        case WM_COMMAND:
            wmId    = LOWORD(wParam); 
            wmEvent = HIWORD(wParam); 
            // Parse the menu selections:
            switch (wmId)
            {
                case IDM_ABOUT:
                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX,
                        hWnd, (DLGPROC)About); 
                break;

                case IDM_EXIT:
                    DestroyWindow(hWnd);
                break;
                
                default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
            }
        break;

        case WM_PAINT:
            OnPaint(hWnd);
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
        break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
   
    return 0;
}

//
// OnPaint:  handles the painting for the window
//
void OnPaint(HWND hWnd)
{
    // Begin painting
    PAINTSTRUCT ps;
    HDC hDC = BeginPaint(hWnd, &ps);
  
    // create a blue pen and a red brush
    HPEN hPenRect = CreatePen(PS_SOLID, 3, RGB(0, 0, 255));
    HBRUSH hBrushRect = CreateSolidBrush(RGB(255, 0, 0));

    // select the pen and brush
    HPEN hPenOld = (HPEN)SelectObject(hDC, hPenRect);
    HBRUSH hBrushOld = (HBRUSH)SelectObject(hDC, hBrushRect);

    // get the client window's rectangle
    RECT rectClient;
    GetClientRect(hWnd, &rectClient);

    // find the center of the client window
    int xCenter = rectClient.right / 2;
    int yCenter = rectClient.bottom / 2;

    // draw a 100 x 100 pixel rectangle
    RECT rectPen;
    rectPen.left = xCenter - 50;
    rectPen.right = xCenter + 50;
    rectPen.top = yCenter - 50;
    rectPen.bottom = yCenter + 50;
    Rectangle(hDC, rectPen.left, rectPen.top,
        rectPen.right, rectPen.bottom);

    // restore the dc to its original state
    SelectObject(hDC, hBrushOld);
    SelectObject(hDC, hPenOld);

    // clean up created objects
    DeleteObject(hBrushRect);
    DeleteObject(hPenRect);

    EndPaint(hWnd, &ps);
}

//
// Mesage handler for about box.
//
LRESULT CALLBACK About
(
    HWND hDlg
   ,UINT message
   ,WPARAM wParam
   ,LPARAM /*lParam*/
)
{
    switch (message)
    {
        case WM_INITDIALOG:
                return TRUE;

        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
            {
                EndDialog(hDlg, LOWORD(wParam));
                return TRUE;
            }
        break;
    }

    return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -