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

📄 btndlg.c

📁 上传的代码是WindowsCEProgramming第一版的源代码
💻 C
字号:
//======================================================================
// BtnDlg - Button dialog box window code
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//======================================================================
#include <windows.h>                 // For all that Windows stuff
#include <prsht.h>                   // Property sheet includes
#include "DlgDemo.h"                 // Program-specific stuff

extern HINSTANCE hInst;

LRESULT DrawButton (HWND hWnd, LPDRAWITEMSTRUCT pdi);
//----------------------------------------------------------------------
// Global data
//
// Identification strings for various WM_COMMAND notifications
NOTELABELS nlBtn[] = {{TEXT ("BN_CLICKED "),      0},
                      {TEXT ("BN_PAINT   "),      1},
                      {TEXT ("BN_HILITE  "),      2},
                      {TEXT ("BN_UNHILITE"),      3},
                      {TEXT ("BN_DISABLE "),      4},
                      {TEXT ("BN_DOUBLECLICKED"), 5},
                      {TEXT ("BN_SETFOCUS "),     6},
                      {TEXT ("BN_KILLFOCUS"),     7}
};
extern NOTELABELS nlPropPage[];
extern int nPropPageSize;

// Handle for icon used in owner-draw icon
HICON hIcon = 0;
//======================================================================
// BtnDlgProc - Button page dialog box procedure
//
BOOL CALLBACK BtnDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam,
                          LPARAM lParam) {
    TCHAR szOut[128];
    HWND hwndMain;
    INT i;

    switch (wMsg) {

        case WM_INITDIALOG:
            // The generic parameter contains the
            // top-level window handle.
            hwndMain = (HWND)((LPPROPSHEETPAGE)lParam)->lParam;
            // Save the window handle in the window structure.
            SetWindowLong (hWnd, DWL_USER, (LONG)hwndMain);

            // Load icon for owner-draw window.
            hIcon = LoadIcon (hInst, MAKEINTRESOURCE (IDI_BTNICON));

            // We need to set the initial state of the radio buttons.
            CheckRadioButton (hWnd, IDC_RADIO1, IDC_RADIO2, IDC_RADIO1);
            return TRUE;
        //
        // Reflect WM_COMMAND messages to main window.
        //
        case WM_COMMAND:
            // Since the check box is not an auto check box, the button
            // has to be set manually.
            if ((LOWORD (wParam) == IDC_CHKBOX) &&
                (HIWORD (wParam) == BN_CLICKED)) {
                // Get the current state, complement, and set.
                i = SendDlgItemMessage (hWnd, IDC_CHKBOX, BM_GETCHECK,
                                        0, 0);
                if (i)
                    SendDlgItemMessage (hWnd, IDC_CHKBOX, BM_SETCHECK,
                                        0, 0);
                else
                    SendDlgItemMessage (hWnd, IDC_CHKBOX, BM_SETCHECK,
                                        1, 0);
            }


            // Get the handle of the main window from the user word.
            hwndMain = (HWND) GetWindowLong (hWnd, DWL_USER);

            // Look up button notification.
            lstrcpy (szOut, TEXT ("WM_COMMAND: "));
            for (i = 0; i < dim(nlBtn); i++) {
                if (HIWORD (wParam) == nlBtn[i].wNotification) {
                    lstrcat (szOut, nlBtn[i].pszLabel);
                    break;
                }
            }
            if (i == dim(nlBtn))
                wsprintf (szOut, TEXT ("WM_COMMAND notification: %x"),
                          HIWORD (wParam));

            SendMessage (hwndMain, MYMSG_ADDLINE,
                         MAKEWPARAM (LOWORD (wParam),ID_BTNPAGE),
                         (LPARAM)szOut);
            return TRUE;

        //
        // Reflect notify message.
        //
        case WM_NOTIFY:
            // Get the handle of the main window from the user word.
            hwndMain = (HWND) GetWindowLong (hWnd, DWL_USER);

            // Look up notify message.
            for (i = 0; i < nPropPageSize; i++) {
                if (((NMHDR *)lParam)->code ==
                                       nlPropPage[i].wNotification) {
                    lstrcpy (szOut, nlPropPage[i].pszLabel);
                    break;
                }
            }
            if (i == nPropPageSize)
                wsprintf (szOut, TEXT ("Notify code:%d"),
                          ((NMHDR *)lParam)->code);

            SendMessage (hwndMain, MYMSG_ADDLINE,
                         MAKEWPARAM (-1,ID_BTNPAGE), (LPARAM)szOut);

            return FALSE;  // Return false to force default processing.

        case WM_DRAWITEM:
            DrawButton (hWnd, (LPDRAWITEMSTRUCT)lParam);
            return TRUE;
    }
    return FALSE;
}

//---------------------------------------------------------------------
// DrawButton - Draws an owner-draw button.
//
LRESULT DrawButton (HWND hWnd, LPDRAWITEMSTRUCT pdi) {

    HPEN hPenShadow, hPenLight, hPenDkShadow, hOldPen;
    POINT ptOut[3], ptIn[3];
    HBRUSH hBr, hOldBr;
    TCHAR szOut[128];
    HWND hwndMain;
    LOGPEN lpen;

    // Get the handle of the main window from the user word.
    hwndMain = (HWND) GetWindowLong (hWnd, DWL_USER);

    // Reflect the messages to the report window.
    wsprintf (szOut, TEXT ("WM_DRAWITEM  Action:%x  State:%x"),
              pdi->itemAction, pdi->itemState);

    SendMessage (hwndMain, MYMSG_ADDLINE,
                 MAKEWPARAM (pdi->CtlID, ID_BTNPAGE),
                 (LPARAM)szOut);

    // Create pens for drawing.
    lpen.lopnStyle = PS_SOLID;
    lpen.lopnWidth.x = 3;
    lpen.lopnWidth.y = 3;
    lpen.lopnColor = GetSysColor (COLOR_3DSHADOW);
    hPenShadow = CreatePenIndirect (&lpen);

    lpen.lopnWidth.x = 1;
    lpen.lopnWidth.y = 1;
    lpen.lopnColor = GetSysColor (COLOR_3DLIGHT);
    hPenLight = CreatePenIndirect (&lpen);

    lpen.lopnColor = GetSysColor (COLOR_3DDKSHADOW);
    hPenDkShadow = CreatePenIndirect (&lpen);

    // Create a brush for the face of the button.
    hBr = CreateSolidBrush (GetSysColor (COLOR_3DFACE));

    // Draw a rectangle with a thick outside border to start the
    // frame drawing.
    hOldPen = SelectObject (pdi->hDC, hPenShadow);
    hOldBr = SelectObject (pdi->hDC, hBr);
    Rectangle (pdi->hDC, pdi->rcItem.left, pdi->rcItem.top,
               pdi->rcItem.right, pdi->rcItem.bottom);

    // Draw the upper left inside line.
    ptIn[0].x = pdi->rcItem.left + 1;
    ptIn[0].y = pdi->rcItem.bottom - 3;
    ptIn[1].x = pdi->rcItem.left + 1;
    ptIn[1].y = pdi->rcItem.top + 1;
    ptIn[2].x = pdi->rcItem.right - 3;
    ptIn[2].y = pdi->rcItem.top+1;

    // Select a pen to draw shadow or light side of button.
    if (pdi->itemState & ODS_SELECTED) {
        SelectObject (pdi->hDC, hPenDkShadow);
    } else {
        SelectObject (pdi->hDC, hPenLight);
    }
    Polyline (pdi->hDC, ptIn, 3);

    // If selected, also draw a bright line inside the lower
    // right corner.
    if (pdi->itemState & ODS_SELECTED) {
        SelectObject (pdi->hDC, hPenLight);
        ptIn[1].x = pdi->rcItem.right- 3;
        ptIn[1].y = pdi->rcItem.bottom - 3;
        Polyline (pdi->hDC, ptIn, 3);
    }
    // Now draw the black outside line on either the upper left or lower
    // right corner.
    ptOut[0].x = pdi->rcItem.left;
    ptOut[0].y = pdi->rcItem.bottom-1;
    ptOut[2].x = pdi->rcItem.right-1;
    ptOut[2].y = pdi->rcItem.top;

    SelectObject (pdi->hDC, hPenDkShadow);
    if (pdi->itemState & ODS_SELECTED) {
        ptOut[1].x = pdi->rcItem.left;
        ptOut[1].y = pdi->rcItem.top;
    } else {
        ptOut[1].x = pdi->rcItem.right-1;
        ptOut[1].y = pdi->rcItem.bottom-1;
    }
    Polyline (pdi->hDC, ptOut, 3);

    // Draw the icon.
    if (hIcon) {
        ptIn[0].x = (pdi->rcItem.right - pdi->rcItem.left)/2 -
                     GetSystemMetrics (SM_CXICON)/2 - 2;
        ptIn[0].y = (pdi->rcItem.bottom - pdi->rcItem.top)/2 -
                     GetSystemMetrics (SM_CYICON)/2 - 2;
        // If pressed, shift image down one pel to simulate the press.
        if (pdi->itemState & ODS_SELECTED) {
            ptOut[1].x += 2;
            ptOut[1].y += 2;
        }
        DrawIcon (pdi->hDC, ptIn[0].x, ptIn[0].y, hIcon);
    }

    // If button has the focus, draw the dotted rect inside the button.
    if (pdi->itemState & ODS_FOCUS) {
        pdi->rcItem.left += 3;
        pdi->rcItem.top += 3;
        pdi->rcItem.right -= 4;
        pdi->rcItem.bottom -= 4;
        DrawFocusRect (pdi->hDC, &pdi->rcItem);
    }

    // Clean up. First select the original brush and pen into the DC.
    SelectObject (pdi->hDC, hOldBr);
    SelectObject (pdi->hDC, hOldPen);

    // Now delete the brushes and pens created.
    DeleteObject (hBr);
    DeleteObject (hPenShadow);
    DeleteObject (hPenDkShadow);
    DeleteObject (hPenLight);
    return 0;
}

⌨️ 快捷键说明

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