crosswordsample.cpp

来自「WIndows mobile 5.0 pocket pc sdk sample 」· C++ 代码 · 共 677 行 · 第 1/2 页

CPP
677
字号
    DeleteObject(hBrush);
}

//////////////////////////////////////////////////////////////////////////////
// FUNCTION: PropSheetCallback.
//
// PURPOSE: Callback for the Tools / Options property sheet used by
//      OnCommandToolsOptions.  This is necessary so that dialog tabs get 
//      drawn in the flat PocketPC style.
//
int PropSheetCallback(HWND hwndDlg, UINT message, LPARAM lParam)
{
    if (message == PSCB_GETVERSION) 
    {
        return COMCTL32_VERSION;
    }
    return 0;
}

//////////////////////////////////////////////////////////////////////////////
// FUNCTION: OnCommandToolsOptions
//
// PURPOSE: Creates the dialog window for Tools / Options.
//
// ON ENTRY:
//      HWND hWnd: the main window app.
//
void OnCommandToolsOptions(HWND hWnd)
{
    PROPSHEETPAGE psp[2];
    psp[0].dwSize = sizeof(psp[0]);
    psp[0].dwFlags = PSP_DEFAULT;
    psp[0].hInstance = g_hInst;
    psp[0].pszTemplate = MAKEINTRESOURCE(IDD_TOOLS_OPTIONS_1);
    psp[0].pfnDlgProc = (DLGPROC)&ToolsOptionsDialog1;

    psp[1].dwSize = sizeof(psp[1]);
    psp[1].dwFlags = PSP_DEFAULT;
    psp[1].hInstance = g_hInst;
    psp[1].pszTemplate = MAKEINTRESOURCE(IDD_TOOLS_OPTIONS_2);
    psp[1].pfnDlgProc = (DLGPROC)&ToolsOptionsDialog2;

    //
    // See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win_ce/htm/pwc_propertysheets.asp
    // for more information about PSH_MAXIMIZE and handling PSCB_GETVERSION in the
    // PropertySheet callback. 
    //
    PROPSHEETHEADER psh;
    psh.dwSize = sizeof(psh);
    psh.dwFlags = PSH_DEFAULT | PSH_PROPSHEETPAGE | PSH_MAXIMIZE | PSH_NOAPPLYNOW | PSH_USECALLBACK;
    psh.hwndParent = hWnd;
    psh.hInstance = g_hInst;
    psh.pszCaption = _T("Tools / Options");
    psh.nPages = 2;
    psh.nStartPage = 0;
    psh.ppsp = &psp[0];
    psh.pfnCallback = PropSheetCallback;
    PropertySheet(&psh);
}

//////////////////////////////////////////////////////////////////////////////
// FUNCTION: OnClickEnterButton
//
// PURPOSE: Called when the enter button is clicked.  Copies the text in the
//      g_hwndEditBox to the crossword offscreen buffer (using DrawHint).  
//
// ON ENTRY:
//      HWND hWnd: the main window app.
//      g_selectedHint: the destination row or column the text will 
//          be copied to.
//
// ON EXIT:
//      The edit box is cleared.
//
void OnClickEnterButton(HWND hWnd)
{
    WCHAR szBuffer[80];
    SendDlgItemMessage(hWnd, IDC_MAIN_EDIT_BOX, WM_GETTEXT, sizeof(szBuffer)/sizeof(WCHAR), (LPARAM)szBuffer);
    SendDlgItemMessage(hWnd, IDC_MAIN_EDIT_BOX, WM_SETTEXT, 0, (LPARAM)_T(""));

    if (g_selectedHint == -1) return;

    for (int i = 0; i < g_hints[g_selectedHint].cLetters; i++)
    {
        if (szBuffer[i] == '\0')
        {
            break;
        }
        int x = g_hints[g_selectedHint].x + (g_hints[g_selectedHint].fDown ? 0 : i);
        int y = g_hints[g_selectedHint].y + (g_hints[g_selectedHint].fDown ? i : 0);
        g_crossword[y][x] = towupper(szBuffer[i]);
    }

    DrawHint(hWnd, g_selectedHint, RGB(128,0,0));
}

//////////////////////////////////////////////////////////////////////////////
// FUNCTION: OnLButtonDown
//
// PURPOSE: Called when the stylus is tapped on the screen.  Selects a row or
//      column based on the location of the tap.
//
// ON ENTRY:
//      HWND hWnd: the window which was tapped.
//      INT x: the x location of the screen tap in pixels.
//      INT y: the y location of the screen tap in pixels.
//
// ON EXIT:
//      g_selectedHint equals the location of the screen tap. 
//
void OnLButtonDown(HWND hWnd, INT x, INT y)
{
    x = (x - 8) / 16;
    y = (y - 26) / 16;

    if (x < 0 || y < 0 || x >= CROSSWORD_X - 1 || y >= CROSSWORD_Y - 1)
    {
        return;
    }

    //
    // Erase the old hint.
    //
    DrawHint(hWnd, g_selectedHint, RGB(255,255,255));
    
    //
    // Search for the hint that we clicked.
    //
    int oldSelectedHint = g_selectedHint;
    g_selectedHint = -1;
    for (int i = 0; i < sizeof(g_hints) / sizeof(CROSSWORD_HINT); i++)
    {
        if (i == oldSelectedHint) continue;
        if ((!g_hints[i].fDown && y == g_hints[i].y &&
            x >= g_hints[i].x && x < g_hints[i].x + g_hints[i].cLetters) ||
            (g_hints[i].fDown && x == g_hints[i].x &&
            y >= g_hints[i].y && y < g_hints[i].y + g_hints[i].cLetters))
        {
            g_selectedHint = i;
            break;
        }
    }

    //
    // Draw in this new hint.
    //
    DrawHint(hWnd, g_selectedHint, RGB(128, 0, 0));

    if (g_selectedHint != -1)
    {
        SendDlgItemMessage(hWnd, IDC_MAIN_EDIT_BOX, EM_LIMITTEXT, g_hints[g_selectedHint].cLetters, 0);
    }

    //
    // Invalidate the hint area so we redraw.
    //
    RECT rTallMode = { 0, 189, 240, 320 };
    RECT rWideMode = { 240, 26, 320, 240 };
    InvalidateRect(hWnd, InWideMode() ? &rWideMode : &rTallMode, FALSE);
}

//////////////////////////////////////////////////////////////////////////////
// FUNCTION: OnPaint
//
// PURPOSE: Paints the main application window.
//
// ON ENTRY:
//      HWND hWnd: the window to paint.
//
void OnPaint(HWND hWnd)
{
    PAINTSTRUCT ps;
    HDC hDC = BeginPaint(hWnd, &ps);

    //
    // Draw background (we made the bitmap 320x320 so to work in landscape mode also).
    //
    HBITMAP hBMP = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BACKGROUND_1));
    HDC hMemDC = CreateCompatibleDC(NULL);
    HBITMAP hOldBMP = (HBITMAP)SelectObject(hMemDC, hBMP);
    BitBlt(hDC, 0, 0, 320, 320, hMemDC, 0, 0, SRCCOPY);
    SelectObject(hMemDC, hOldBMP);
    DeleteObject(hBMP);
    DeleteDC(hMemDC);

    //
    // Draw crossword.
    //
    TransparentImage(hDC, 8, 26, 14 * 16 + 1, 10 * 16 + 1, 
        g_hMemDC, 0, 0, 14 * 16 + 1, 10 * 16 + 1, RGB(255, 255, 255));
            
    POINT line[] = { {0, 188}, {240, 188} };
    Polyline(hDC, line, 2);
    
    //
    // Erase the hint area using a pattern brush.
    //
    RECT rTallMode = { 25, 200, 230, 245 };
    RECT rWideMode = { 240, 43, 311, 185 };
    RECT& r = InWideMode() ? rWideMode : rTallMode;
    HBITMAP hPattern = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_PATTERN));
    HBRUSH hNewBrush = CreatePatternBrush(hPattern);
    HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hNewBrush);
    Rectangle(hDC, r.left, r.top, r.right, r.bottom);
    SelectObject(hDC, hOldBrush);
    DeleteObject(hNewBrush);
    DeleteObject(hPattern);

    if (g_selectedHint != -1)
    {
        //
        // Print the hint out at bottom.
        //
        r.left  += 4;
        r.right -= 4;
        SetBkMode(hDC, TRANSPARENT);
        HFONT hOldFont = (HFONT)SelectObject(hDC, g_hFont);
        DrawText(hDC, g_hints[g_selectedHint].szHint, -1, &r, DT_TOP | DT_LEFT | DT_WORDBREAK);
        SelectObject(hDC, hOldFont);

        //
        // Display an arrow next to the hint.
        //
        POINT rgArrow[] = { {0,3}, {6,3}, {6,0}, {12, 5}, {6, 10}, {6, 7}, {0, 7} };
        for (int i = 0; i < 7; i++)
        { 
            //
            // A vertical arrow is a horizontal arrow with X and Y flipped.
            //
            if (g_hints[g_selectedHint].fDown)
            {
                int t = rgArrow[i].x;
                rgArrow[i].x = rgArrow[i].y;
                rgArrow[i].y = t;
            }
            rgArrow[i].x += (InWideMode() ? 280 : 5);
            rgArrow[i].y += (InWideMode() ? 26 : 200); 
        }
        Polygon(hDC, rgArrow, 7);
    }
    
    EndPaint(hWnd, &ps);
}

//////////////////////////////////////////////////////////////////////////////
// FUNCTION: WndProc()
//
// PURPOSE:  Processes messages for the main window.
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
        case WM_CREATE:
            g_hwndCB = CreateRpCommandBar(hWnd);
            memset (&g_sai, 0, sizeof (g_sai));
            g_sai.cbSize = sizeof (g_sai);
            break;

        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
            case IDC_MAIN_ENTER_BUTTON:
                OnClickEnterButton(hWnd);
                break;
            
            case ID_TOOLS_OPTIONS:
                OnCommandToolsOptions(hWnd);
                break;
            
            case ID_TOOLS_ABOUT:
                DialogBox(g_hInst, MAKEINTRESOURCE(IDD_TOOLS_ABOUT), hWnd, &ToolsAboutDialog);
                break;
            }
            break;

        case WM_DRAWITEM:
            {
                //
                // Draws the Enter button.
                //
                LPDRAWITEMSTRUCT lpDis = (LPDRAWITEMSTRUCT)lParam;
                DrawEdge(lpDis->hDC, &lpDis->rcItem, 
                    (lpDis->itemState & ODS_SELECTED) ? EDGE_SUNKEN : EDGE_RAISED, 
                    BF_RECT);
                ImageList_Draw(g_hImageList,
                    (lpDis->itemState & ODS_SELECTED) ? 0 : 1,
                    lpDis->hDC, 2, 2, ILD_NORMAL);
            }
            break;

        case WM_PAINT:
            OnPaint(hWnd);
            break;
            
        case WM_LBUTTONDOWN:
            OnLButtonDown(hWnd, LOWORD(lParam), HIWORD(lParam));
            break;

        case WM_DESTROY:
            CommandBar_Destroy(g_hwndCB);
            PostQuitMessage(0);
            break;

        case WM_ACTIVATE:
            SHHandleWMActivate(hWnd, wParam, lParam, &g_sai, FALSE);
            break;

        case WM_SETTINGCHANGE:
            SHHandleWMSettingChange(hWnd, wParam, lParam, &g_sai);
            break;

        case WM_SIZE:
            {
                HWND hEditBox = GetDlgItem(hWnd, IDC_MAIN_EDIT_BOX);
                HWND hEnterButton = GetDlgItem(hWnd, IDC_MAIN_ENTER_BUTTON);
                INT nWidth = LOWORD(lParam);

                MoveWindow(hEditBox, 8, 4, nWidth - 70, 20, TRUE);
                MoveWindow(hEnterButton, nWidth - 57, 4, 50, 20, TRUE);
            }
            break;

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

//////////////////////////////////////////////////////////////////////////////
// FUNCTION: InWideMode()
//
BOOL InWideMode()
{
    int height = GetSystemMetrics(SM_CYSCREEN);
    return (height < 320) ? TRUE : FALSE;
}

⌨️ 快捷键说明

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