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

📄 fonts.cpp

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

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

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

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

    // 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_FONTS);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = (LPCSTR)IDC_FONTS;
    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);
  
    // set the background mode to transparent
    SetBkMode(hDC, TRANSPARENT);

    // create a font
    LOGFONT lf;
    memset(&lf, 0x00, sizeof(lf));

    // lf.lfHeight is negative since we want to use character 
    // height, not the cell height
    lf.lfHeight = -36;
    strncpy(lf.lfFaceName, _T("Arial"), 32);
    lf.lfWeight = FW_NORMAL;
    HFONT hFont = CreateFontIndirect(&lf);

    HFONT hFontOld = (HFONT)SelectObject(hDC, hFont);
     
    TCHAR szText[] = _T("the quick brown fox");
    RECT rectDraw;

    // get the extent of the text and draw a rectangle
    // to place the text in
    SIZE size;
    GetTextExtentPoint32(hDC, szText, strlen(szText), &size);

    rectDraw.left = 10;
    rectDraw.top = 10;
    rectDraw.right = rectDraw.left + size.cx;
    rectDraw.bottom = rectDraw.top + size.cy;

    Rectangle(hDC, rectDraw.left, rectDraw.top,
        rectDraw.right, rectDraw.bottom);

    // set the text color to blue
    SetTextColor(hDC, RGB(0,0,255));

    // display the text in the rectangle
    DrawText(hDC, szText, -1, &rectDraw, DT_LEFT); 

    SelectObject(hDC, hFontOld);

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