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

📄 regions.cpp

📁 WINDOWS2000开发人员指南源代码第五章的
💻 CPP
字号:
// regions.cpp : Defines the entry point for the application.
//

#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

HRGN g_hRegion1 = NULL;
HRGN g_hRegion2 = NULL;
HRGN g_hRegion3 = NULL;

HBRUSH g_hBrushRed = NULL;
HBRUSH g_hBrushGreen = NULL;
HBRUSH g_hBrushBlue = NULL;

// Foward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
void                ExitInstance();
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
void                OnPaint(HWND hWnd);
BOOL                UpdateRegions(HWND hWnd);
void                OnLButtonDown(HWND hWnd, POINT& pt);
void                OnGetMinMaxInfo(LPMINMAXINFO lpmmi);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

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

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

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

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

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

    ExitInstance();

    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_REGIONS);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = (LPCSTR)IDC_REGIONS;
    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)
    {
        // create the brushes 
        g_hBrushRed = CreateSolidBrush(RGB(255,0,0));
        g_hBrushGreen = CreateSolidBrush(RGB(0,255,0));
        g_hBrushBlue = CreateSolidBrush(RGB(0,0,255));

        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
        bReturn = TRUE;
    } 

   return bReturn;
}

//
// ExitInstance: cleans up the resources created
//
void ExitInstance()
{
    // delete the brushes 
    DeleteObject(g_hBrushRed);
    DeleteObject(g_hBrushGreen);
    DeleteObject(g_hBrushBlue);

    // delete the regions
    DeleteObject(g_hRegion1);
    DeleteObject(g_hRegion2);
    DeleteObject(g_hRegion3);
}

//
//  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_SIZE:
            UpdateRegions(hWnd);
        break;

        case WM_LBUTTONDOWN:
        {
            POINT pt;
            pt.x = GET_X_LPARAM(lParam);
            pt.y = GET_Y_LPARAM(lParam);
            OnLButtonDown(hWnd, pt);
        }
        break;

        case WM_GETMINMAXINFO:
            OnGetMinMaxInfo((LPMINMAXINFO)lParam);
        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)
{
    PAINTSTRUCT ps;

    HDC hDC = BeginPaint(hWnd, &ps);

    // fill the regions with the brushes
    FillRgn(hDC, g_hRegion1, g_hBrushRed);
    FillRgn(hDC, g_hRegion2, g_hBrushGreen);
    FillRgn(hDC, g_hRegion3, g_hBrushBlue);

    EndPaint(hWnd, &ps);
}

//
// UpdatesRegions: called to create regions based on the 
//  size of the client area
//
BOOL UpdateRegions(HWND hWnd)
{
    BOOL bReturn(TRUE);

    // delete the regions if they already exist
    if (g_hRegion1 != NULL)
    {
        DeleteObject(g_hRegion1);
        g_hRegion1 = NULL; 
    }

    if (g_hRegion2 != NULL)
    {
        DeleteObject(g_hRegion2);
        g_hRegion2 = NULL; 
    }

    if (g_hRegion3 != NULL)
    {
        DeleteObject(g_hRegion3);
        g_hRegion3 = NULL; 
    }

    // calculate the size and position of the rectangle
    RECT rectClient;
    GetClientRect(hWnd, &rectClient);
    InflateRect(&rectClient, -25, -25);

    int xCenter = 
        ((rectClient.right - rectClient.left) / 2)
            + rectClient.left; 
    int yCenter = 
        ((rectClient.bottom - rectClient.top) / 2) 
            + rectClient.top;

    // create the regions
    POINT arPoints[6];
    int arPolyPoints[2];

    // create a triangular region from the top, left to
    // the center point and then to the left, bottom
    arPoints[0].x = rectClient.left;
    arPoints[0].y = rectClient.top;
    arPoints[1].x = xCenter;
    arPoints[1].y = yCenter;
    arPoints[2].x = rectClient.left;
    arPoints[2].y = rectClient.bottom;
    g_hRegion1 = CreatePolygonRgn(arPoints, 3, ALTERNATE);
    if (g_hRegion1 != NULL)
    {
        // invalidate the region
        InvalidateRgn(hWnd, g_hRegion1, FALSE);
    }

    // create a triangular region from the right, left to
    // the center point and then to the right, bottom
    arPoints[0].x = rectClient.right;
    arPoints[0].y = rectClient.top;
    arPoints[1].x = xCenter;
    arPoints[1].y = yCenter;
    arPoints[2].x = rectClient.right;
    arPoints[2].y = rectClient.bottom;
    g_hRegion2 = CreatePolygonRgn(
        arPoints, 3, ALTERNATE);
    if (g_hRegion2 != NULL)
    {
        // invalidate the region
        InvalidateRgn(hWnd, g_hRegion2, FALSE);
    }

    // create a region, consisting of two triangular regions
    // from the top and bottom of the rectangle
    arPoints[0].x = rectClient.left;
    arPoints[0].y = rectClient.top;
    arPoints[1].x = xCenter;
    arPoints[1].y = yCenter;
    arPoints[2].x = rectClient.right;
    arPoints[2].y = rectClient.top;
    arPoints[3].x = rectClient.left;
    arPoints[3].y = rectClient.bottom;
    arPoints[4].x = xCenter;
    arPoints[4].y = yCenter;
    arPoints[5].x = rectClient.right;
    arPoints[5].y = rectClient.bottom;

    arPolyPoints[0] = 3;
    arPolyPoints[1] = 3;
    g_hRegion3 = CreatePolyPolygonRgn(
        arPoints, arPolyPoints, 2, ALTERNATE);
    if (g_hRegion3 != NULL)
    {
        // invalidate the region
        InvalidateRgn(hWnd, g_hRegion3, FALSE);
    } 

    return bReturn;
}

//
// OnLButtonDown: inverts a region if the mouse is
//  clicked within the region
//
void OnLButtonDown(HWND hWnd, POINT& pt)
{
    HRGN hRegion(NULL);

    // find which region if any the mouse was
    //  clicked in
    if (PtInRegion(g_hRegion1, pt.x, pt.y))
    {
        hRegion = g_hRegion1;
    }
    else if (PtInRegion(g_hRegion2, pt.x, pt.y))
    {
        hRegion = g_hRegion2;
    }
    else if (PtInRegion(g_hRegion3, pt.x, pt.y))
    {
        hRegion = g_hRegion3;
    }

    // invert the region
    if (hRegion != NULL)
    {
        HDC hDC = GetDC(hWnd);
        InvertRgn(hDC, hRegion);
        ReleaseDC(hWnd, hDC);
    }
}

//
//  OnGetMinMaxInfo: only allow the window to be
//    sized to 200 x 200 pixels
//
void OnGetMinMaxInfo(LPMINMAXINFO lpmmi)
{
    lpmmi->ptMinTrackSize.x = 200;
    lpmmi->ptMinTrackSize.y = 200;
}

//
// 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 + -