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

📄 bitblt.cpp

📁 WINDOWS2000开发人员指南源代码第五章的
💻 CPP
字号:
// bitblt.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);
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 /*pCmdLine*/
   ,int nCmdShow
)
{
    MSG msg;
    HACCEL hAccelTable;

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

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

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

    // 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_BITBLT);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = (LPCSTR)IDC_BITBLT;
    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)
{
    PAINTSTRUCT ps;

    HDC hDC = BeginPaint(hWnd, &ps);
    
    // create a compatible memory dc
    HDC hDCMem = CreateCompatibleDC(hDC);

    // load the bitmap
    HBITMAP hBitmap = LoadBitmap(
        hInst, MAKEINTRESOURCE(IDB_BITMAP));
    
    // set the bitmap 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);
    
    RECT rectClient;
    GetClientRect(hWnd, &rectClient);

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