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

📄 doiview.cpp

📁 Windows CE程序设计随书源代码 在学习的过程中
💻 CPP
字号:
//======================================================================
// DOIView - Demonstrates window scroll bars
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include <windows.h>                 // For all that Windows stuff
#include "DOIView.h"                 // Program-specific stuff
#include <aygshell.h>                // Extended shell API

//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("DOIView");
HINSTANCE hInst;                     // Program instance handle


#define WM_MYMSG   (WM_USER + 100)
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
    WM_CREATE, DoCreateMain,
    WM_SIZE, DoSizeMain,
    WM_LBUTTONDOWN, DoLButtonDownMain,
    WM_COMMAND, DoCommandMain,
    WM_VSCROLL, DoVScrollMain,
    WM_PAINT, DoPaintMain,
    WM_DESTROY, DoDestroyMain,
};

// Command Message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
    IDM_HOME, DoMainCommandHome,
    IDM_END, DoMainCommandEnd,
    IDM_EXIT, DoMainCommandExit,
};

typedef struct {
    LPTSTR pszLine;
    int nLen;
} LINEARRAY, *PLINEARRAY;

#define MAXLINES 1000
LINEARRAY laText[MAXLINES];
int nNumLines = 0;
int nFontHeight = 1;
int nLinesPerPage = 1;

LPTSTR pszDeclaration;
HFONT hFont;

int nVPos, nVMax;
BOOL fFirst = TRUE;
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    LPWSTR lpCmdLine, int nCmdShow) {
    MSG msg;
    int rc = 0;
    HWND hwndMain;
    HACCEL hAccel;

    // Initialize this instance.
    hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
    if (hwndMain == 0) return 0x10;

    // Load accelerator table.
    hAccel = LoadAccelerators (hInst, MAKEINTRESOURCE (ID_ACCEL));

    // Application message loop
    while (GetMessage (&msg, NULL, 0, 0)) {
        // Translate accelerators 
        if (!TranslateAccelerator (hwndMain, hAccel, &msg)) {
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }
    // Instance cleanup
    return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
    WNDCLASS wc;
    HWND hWnd;
    PBYTE pRes, pBuff;
    int nStrLen = 0, i = 0;

    // Save program instance handle in global variable.
    hInst = hInstance;

#if defined(WIN32_PLATFORM_PSPC)
    // If Pocket PC, only allow one instance of the application
    hWnd = FindWindow (szAppName, NULL);
    if (hWnd) {
        SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));    
        return 0;
    }
#endif

    // Load text from multiple string resources into one large buffer
    pBuff = (PBYTE)LocalAlloc (LPTR, 8);
    while (pRes = (PBYTE)LoadString (hInst, IDS_DOITEXT + i++, NULL, 0))
    {
        // Get the length of the string resource
        int nLen = *(PWORD)(pRes-2) * sizeof (TCHAR);
        // Resize buffer
        pBuff = (PBYTE)LocalReAlloc (pBuff, nStrLen + 8 + nLen, 
                                     LMEM_MOVEABLE | LMEM_ZEROINIT);
        if (pBuff == NULL) return 0;
        // Copy resource into buffer
        memcpy (pBuff + nStrLen, pRes, nLen);
        nStrLen += nLen;
    }

    *(TCHAR *)(pBuff + nStrLen) = TEXT ('\0');
    pszDeclaration = (LPTSTR)pBuff;

    // Register application main window class.
    wc.style = 0;                             // Window style
    wc.lpfnWndProc = MainWndProc;             // Callback function
    wc.cbClsExtra = 0;                        // Extra class data
    wc.cbWndExtra = 0;                        // Extra window data
    wc.hInstance = hInstance;                 // Owner handle
    wc.hIcon = NULL,                          // Application icon
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
    wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wc.lpszMenuName =  NULL;                  // Menu name
    wc.lpszClassName = szAppName;             // Window class name

    if (RegisterClass (&wc) == 0) return 0;

    // Create main window.
    hWnd = CreateWindowEx (WS_EX_NODRAG, szAppName, 
                           TEXT("The Declaration of Independence"),
                           WS_VSCROLL | WS_VISIBLE | WS_CAPTION | 
                           WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 
                           CW_USEDEFAULT, CW_USEDEFAULT, NULL,
                           NULL, hInstance, NULL);
                                              
    if (!IsWindow (hWnd)) return 0;  // Fail code if not created.

    // Standard show and update calls
    ShowWindow (hWnd, nCmdShow);
    UpdateWindow (hWnd);
    return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
// 
int TermInstance (HINSTANCE hInstance, int nDefRC) {
    LocalFree (pszDeclaration);
    return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                              LPARAM lParam) {
    int i;
    //
    // Search message list to see if we need to handle this
    // message.  If in list, call procedure.
    //
    for (i = 0; i < dim(MainMessages); i++) {
        if (wMsg == MainMessages[i].Code)
            return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
    }
    return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateMain - Process WM_CREATE message for window.
//
LRESULT DoCreateMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                      LPARAM lParam) {
    TEXTMETRIC tm;
    HDC hdc = GetDC (hWnd);
    LOGFONT lf;
    HFONT hFontWnd;
    
    hFontWnd = (HFONT)GetStockObject (SYSTEM_FONT);
    GetObject (hFontWnd, sizeof (LOGFONT), &lf);

    lf.lfHeight = -12 * GetDeviceCaps(hdc, LOGPIXELSY)/ 72;
    lf.lfWeight = 0; 
    hFont = CreateFontIndirect (&lf);
    SendMessage (hWnd, WM_SETFONT, (WPARAM)hFont, 0);

    // Get the height of the default font.
    hFontWnd = (HFONT)SelectObject (hdc, hFont);
    GetTextMetrics (hdc, &tm);
    nFontHeight = tm.tmHeight + tm.tmExternalLeading;
    SelectObject (hdc, hFontWnd);

    ReleaseDC (hWnd, hdc);
    return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                       LPARAM lParam) {
    RECT rect;
    HDC hdc = GetDC (hWnd);
    GetClientRect (hWnd, &rect);
    int i = 0, nChars, nWidth;
    LPTSTR pszWndText = pszDeclaration;
    SCROLLINFO si;
    HFONT hFontWnd;
    BOOL fNewLine;

    hFontWnd = (HFONT)SelectObject (hdc, hFont);

    // Compute the line breaks
    nWidth = rect.right - rect.left - 10;
    while (i < MAXLINES){
        pszWndText = WrapString (hdc, pszWndText, &nChars, nWidth,
                                 &fNewLine);
        if (pszWndText == 0)
            break;
        laText[i].pszLine = pszWndText;
        laText[i].nLen = nChars;
        i++;
        if (fNewLine) {
            laText[i].nLen = 0;
            i++;
        }
        pszWndText += nChars;
    }
    nNumLines = i;
    nLinesPerPage = (rect.bottom - rect.top)/nFontHeight;

    // Compute lines per window and total lenght
    si.cbSize = sizeof (si);
    si.nMin = 0;
    si.nMax = nNumLines;
    si.nPage = nLinesPerPage;
    si.nPos = nVPos;
    si.fMask = SIF_ALL;
    SetScrollInfo (hWnd, SB_VERT, &si, TRUE);

    // Clean up
    SelectObject (hdc, hFontWnd);
    ReleaseDC (hWnd, hdc);
    InvalidateRect (hWnd, NULL, TRUE);
    return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window. 
// 
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    WORD idItem, wNotifyCode;
    HWND hwndCtl;
    int  i;

    // Parse the parameters.
    idItem = (WORD) LOWORD (wParam);
    wNotifyCode = (WORD) HIWORD(wParam);
    hwndCtl = (HWND) lParam;

    // Call routine to handle control message.
    for (i = 0; i < dim(MainCommandItems); i++) {
        if (idItem == MainCommandItems[i].Code)
            return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl, 
                                           wNotifyCode);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoLButtonDownMain - Process WM_LBUTTONDOWN message for window.
//
LRESULT DoLButtonDownMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                           LPARAM lParam) {
    HMENU hMenuMain, hMenu;
    POINT pt;
    int rc;

    // Display the menu at the point of the tap
    pt.x = LOWORD (lParam);
    pt.y = HIWORD (lParam);

    SHRGINFO sri;
    sri.cbSize = sizeof (sri);
    sri.dwFlags = 1;
    sri.hwndClient = hWnd;
    sri.ptDown = pt;

    // See if tap and hold
    rc = SHRecognizeGesture (&sri);
    if (rc == 0) return 0;

    // Display the menu at the point of the tap
	MapWindowPoints (hWnd, HWND_DESKTOP, &pt, 1);
    pt.x += 5;

    hMenuMain = LoadMenu (hInst, MAKEINTRESOURCE (ID_MENU));
    hMenu = GetSubMenu (hMenuMain, 0);
    TPMPARAMS tpm;
    tpm.cbSize = sizeof (tpm);
    GetClientRect (hWnd, &tpm.rcExclude);
    TrackPopupMenuEx (hMenu, TPM_LEFTALIGN | TPM_TOPALIGN, 
                      pt.x, pt.y, hWnd, &tpm);
    DestroyMenu (hMenuMain);
    DestroyMenu (hMenu);
    return 0;
}
//----------------------------------------------------------------------
// DoVScrollMain - Process WM_VSCROLL message for window.
//
LRESULT DoVScrollMain (HWND hWnd, UINT wMsg, WPARAM wParam,
                       LPARAM lParam) {
    RECT rect;
    SCROLLINFO si;
    int sOldPos = nVPos;

    GetClientRect (hWnd, &rect);

    switch (LOWORD (wParam)) {
    case SB_LINEUP:
        nVPos -= 1;
        break;

    case SB_LINEDOWN:
        nVPos += 1;
        break;

    case SB_PAGEUP:
        nVPos -= nLinesPerPage;
        break;

    case SB_PAGEDOWN:
        nVPos += nLinesPerPage;
        break;

    case SB_THUMBTRACK:
    case SB_THUMBPOSITION:
        nVPos = HIWORD (wParam);
        break;
    }
    // Check range.
    if (nVPos < 0)
        nVPos = 0;
    if (nVPos > nNumLines-1)
        nVPos = nNumLines-1;

    // If scroll position changed, update scrollbar and
    // force redraw of window.
    if (nVPos != sOldPos) {
        si.cbSize = sizeof (si);
        si.nPos = nVPos;
        si.fMask = SIF_POS;
        SetScrollInfo (hWnd, SB_VERT, &si, TRUE);

        InvalidateRect (hWnd, NULL, TRUE);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                     LPARAM lParam) {
    PAINTSTRUCT ps;
    HFONT hFontWnd;
    RECT rect;
    HDC hdc;
    int i, y = 5;

    hdc = BeginPaint (hWnd, &ps);

    GetClientRect (hWnd, &rect);

    hFontWnd = (HFONT)SelectObject (hdc, hFont);

    // Draw the text
    for (i = nVPos; i < nNumLines; i++) {
        if (y > rect.bottom - nFontHeight - 10)
            break;
        if (laText[i].nLen)
            ExtTextOut (hdc, 5, y, TRANSPARENT, NULL, laText[i].pszLine,
                        laText[i].nLen, NULL);
        y += nFontHeight;
    }
    SelectObject (hdc, hFontWnd);
    EndPaint (hWnd, &ps);
    return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
                       LPARAM lParam) {
    PostQuitMessage (0);
    return 0;
}
//======================================================================
// Command handler routines
//
//----------------------------------------------------------------------
// DoMainCommandHome - Process Program Home command.
//
LPARAM DoMainCommandHome (HWND hWnd, WORD idItem, HWND hwndCtl, 
                          WORD wNotifyCode) {
    SCROLLINFO si;
    if (nVPos != 0) {
        nVPos = 0;

        si.cbSize = sizeof (si);
        si.nPos = nVPos;
        si.fMask = SIF_POS;
        SetScrollInfo (hWnd, SB_VERT, &si, TRUE);

        InvalidateRect (hWnd, NULL, TRUE);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandEnd - Process End command.
//
LPARAM DoMainCommandEnd (HWND hWnd, WORD idItem, HWND hwndCtl, 
                         WORD wNotifyCode) {
    SCROLLINFO si;
    int nEndPos = nNumLines - nLinesPerPage + 1;

    if (nVPos != nEndPos) {
        nVPos = nEndPos;

        si.cbSize = sizeof (si);
        si.nPos = nVPos;
        si.fMask = SIF_POS;
        SetScrollInfo (hWnd, SB_VERT, &si, TRUE);

        InvalidateRect (hWnd, NULL, TRUE);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl, 
                          WORD wNotifyCode) {

    SendMessage (hWnd, WM_CLOSE, 0, 0);
    return 0;
}
//----------------------------------------------------------------------
// WrapString - Determine a length that will fit with a width
//
LPTSTR WrapString (HDC hdc, LPTSTR pszText, int *pnLen, int nWidth, 
                   BOOL *fEOL) {
    LPTSTR pszStr, pszStart;
    SIZE Size;

    *fEOL = FALSE;
    *pnLen = 0;

    // Skip to first non-space char
    for (; (*pszText!=TEXT('\0')) && (*pszText<=TEXT (' ')); pszText++);

    pszStart = pszText;

    if (*pszText == 0)
        return 0;

    while (1) {
        pszStr = pszText;
        // Find end of the next word
        for (; (*pszText!=TEXT('\0')) && *pszText>TEXT (' ');pszText++);

        // Get length of the string
        GetTextExtentPoint (hdc, pszStart, pszText - pszStart, &Size);

        if (Size.cx > nWidth)
            break;
        if ((*pszText == TEXT ('\0'))  || (*pszText == TEXT ('\r')) || 
            (*pszText == TEXT ('\n'))) {
            *fEOL = TRUE;
            pszStr = pszText;
            break;
        }
        // slip past space 
        pszText++;
    }
    *pnLen = pszStr - pszStart;
    return pszStart;
}

⌨️ 快捷键说明

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