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

📄 clippath.cpp

📁 WINDOWS2000开发人员指南源代码第五章的
💻 CPP
字号:
// clippath.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

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

void OnPaint(HWND hWnd);
BOOL DefinePath(HWND hWnd, HDC hDC);

//
// 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_CLIPPATH, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

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

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

    // 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_CLIPPATH);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = (LPCSTR)IDC_CLIPPATH;
    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;
        
        case WM_GETMINMAXINFO:
        {
            LPMINMAXINFO pMinMaxInfo = (LPMINMAXINFO)lParam;
            pMinMaxInfo->ptMaxSize.x = 400;
            pMinMaxInfo->ptMaxSize.y = 300;
            pMinMaxInfo->ptMaxTrackSize.x = 400;
            pMinMaxInfo->ptMaxTrackSize.y = 300;
        }
        break;

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

   return 0;
}

//
//  DefinePath: defines a path consisting of an ellipses 
//      within an ellipse
//
BOOL DefinePath(HWND hWnd, HDC hDC)
{
    BOOL bReturn(FALSE);

    // begin the path
    bReturn = BeginPath(hDC);
    if (bReturn)
    {
        // get the client rect
        RECT rectClient;
        GetClientRect(hWnd, &rectClient);
        
        // create a ellipse 25 x 25 inside the client area
        InflateRect(&rectClient, -25, -25);
        Ellipse(hDC, rectClient.left, rectClient.top,
            rectClient.right, rectClient.bottom);

        // deflate the rect 25 x25 and draw another ellipse
        InflateRect(&rectClient, -25, -25);
        Ellipse(hDC, rectClient.left, rectClient.top,
            rectClient.right, rectClient.bottom);

        // end the path
        EndPath(hDC);
    }

    return bReturn;
}

//
// OnPaint:  handles the painting for the window
//
void OnPaint(HWND hWnd)
{
    // make sure our clipping region is large enough to set
    // our clipping area since an application cannot grow
    // a clipping area using SelectClipPath or SelectClipRgn
    RECT rectClient;
    GetClientRect(hWnd, &rectClient);
    
    InflateRect(&rectClient, -25, -25);
    InvalidateRect(hWnd, &rectClient, FALSE);

    PAINTSTRUCT ps;
    HDC hDC = BeginPaint(hWnd, &ps);
    
    // select clip path
    DefinePath(hWnd, hDC);
    SelectClipPath(hDC, RGN_COPY);

    // load the bitmap
    HBITMAP hBitmap = LoadBitmap(
        hInst, MAKEINTRESOURCE(IDB_BITMAP));
    
    // create a compatible memory dc
    HDC hDCMem = CreateCompatibleDC(hDC);

    // set the bitamp into the memory dc
    HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDCMem, hBitmap);

    // get the dimension of the bitmap so we can center
    // it in the window
    BITMAP bitmap;
    GetObject(hBitmap, sizeof(bitmap), &bitmap);

    int nLeft = (rectClient.right / 2) - (bitmap.bmWidth / 2);
    int nTop =  (rectClient.bottom / 2) - (bitmap.bmHeight / 2);

    // display the bitmap
    BitBlt(hDC, nLeft, nTop, bitmap.bmWidth, bitmap.bmHeight, 
        hDCMem, 0, 0, SRCCOPY);

    SelectObject(hDCMem, hBitmapOld);

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