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

📄 npwnd.c

📁 MS-Press book about programming under Windows CE with source-codes of examples
💻 C
字号:
//======================================================================
// NPWnd - An IM window
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//======================================================================
#include <windows.h>
#define COBJMACROS
#include <aygshell.h>                // Palm-size PC shell includes
#include <sip.h>                     // SIP includes
#include <keybd.h>                   // Keyboard flag includes

#include "NPWnd.h"                  // Includes for this window

INT DrawButton (HDC hdc, RECT *prect, LPTSTR pChar, BOOL fPressed);

TCHAR g_tcBtnChar[] = {
        TEXT ('1'), TEXT ('2'), TEXT ('3'), TEXT ('-'), TEXT ('*'),
        TEXT ('4'), TEXT ('5'), TEXT ('6'), TEXT ('+'), TEXT ('/'),
        TEXT ('7'), TEXT ('8'), TEXT ('9'), TEXT ('0'), TEXT ('='),
};
UINT g_BtnVChars[] = {
        '1', '2', '3', VK_HYPHEN, VK_MULTIPLY, 
        '4', '5', '6', VK_ADD, VK_SLASH,
        '7', '8', '9', '0', VK_EQUAL,
};

// Message dispatch table for SipWindowProc
const struct decodeUINT SipMessages[] = {
    WM_CREATE, DoCreateSip,
    WM_PAINT, DoPaintSip,
    MYMSG_METHCALL, DoSetSipInfo,
    WM_LBUTTONDOWN, DoMouseSip,
    WM_MOUSEMOVE, DoMouseSip,
    WM_LBUTTONUP, DoMouseSip,
    WM_LBUTTONDBLCLK, DoMouseSip,
    WM_DESTROY, DoDestroySip,
};
//======================================================================
// NPWndProc - Window procedure for SIP 
//
LRESULT CALLBACK NPWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
                             LPARAM lParam) {
    INT  i;
    // Call routine to handle control message.
    for (i = 0; i < dim(SipMessages); i++) {
        if (wMsg == SipMessages[i].Code)
            return (*SipMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
    }
    return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateSip - Process WM_CREATE message for window.
//
LRESULT CALLBACK DoCreateSip (HWND hWnd, UINT wMsg, WPARAM wParam, 
                             LPARAM lParam) {
    LPSIPWNDSTRUCT pWndData;

    // Allocate a data structure for the SIP keyboard window.
    pWndData = LocalAlloc (LPTR, sizeof (SIPWNDSTRUCT));
    if (!pWndData) {
        DestroyWindow (hWnd);
        return 0;
    }
    memset (pWndData, 0, sizeof (SIPWNDSTRUCT));
    GetWindowRect (GetParent (hWnd), &pWndData->rectDocked);
    SetWindowLong (hWnd, GWL_USERDATA, (INT)pWndData);
    return 0;
}
//----------------------------------------------------------------------
// DoSetSipInfo - Process set information user message for window.
//
LRESULT CALLBACK DoSetSipInfo (HWND hWnd, UINT wMsg, WPARAM wParam, 
                               LPARAM lParam) {
    LPSIPWNDSTRUCT pWndData;
    RECT rect;

    pWndData = (LPSIPWNDSTRUCT)GetWindowLong (hWnd, GWL_USERDATA);
    switch (wParam) {
    // Called when RegisterCallback method called
    case 0:
        pWndData->pIMCallback = (IIMCallback *)lParam;
        break;
    // Called when GetInfo method called
    case 1:
        pWndData->imi = *(IMINFO *)lParam;
        break;
    // Called when ReceiveSipInfo method called
    case 2:
        GetClientRect (GetParent(hWnd), &rect);
        MoveWindow (hWnd, 0, 0, rect.right - rect.left,
                    rect.bottom - rect.top, TRUE);
        break;
    }
    return 0;
}
//----------------------------------------------------------------------
// DoPaintSip - Process WM_PAINT message for window.
//
LRESULT CALLBACK DoPaintSip (HWND hWnd, UINT wMsg, WPARAM wParam, 
                             LPARAM lParam) {
    HDC hdc;
    HBRUSH hOld;
    PAINTSTRUCT ps;
    RECT rect, rectBtn;
    INT i, j, k, x, y, cx, cy, cxBtn, cyBtn;
    LPSIPWNDSTRUCT pWndData;

    pWndData = (LPSIPWNDSTRUCT)GetWindowLong (hWnd, GWL_USERDATA);

    hdc = BeginPaint (hWnd, &ps);
    GetClientRect (hWnd, &rect);

    cx = (rect.right - rect.left - 3 - GRIPWIDTH) / CXBTNS;
    cy = (rect.bottom - rect.top - 3) / CYBTNS;
    cxBtn = cx - 3;
    cyBtn = cy - 3;

    // Select a brush for the gripper.
    hOld = SelectObject (hdc, GetStockObject (GRAY_BRUSH));
    Rectangle (hdc, rect.left, rect.top, rect.left + GRIPWIDTH, 
               rect.bottom);
    SelectObject (hdc, hOld);

    k = 0;
    y = 3;
    for (i = 0; i < CYBTNS; i++) {
        x = 3 + GRIPWIDTH;
        for (j = 0; j < CXBTNS; j++) {
            SetRect (&rectBtn, x, y, x + cxBtn, y + cyBtn);
            DrawButton (hdc, &rectBtn, &g_tcBtnChar[k++], 
                        pWndData->dwBtnDnFlags & (1 << k));
            x += cx;
        }
        y += cy;
    }
    EndPaint (hWnd, &ps);
    return 0;
}
//----------------------------------------------------------------------
// HandleGripper - Handles mouse messages over gripper bar
//
LRESULT HandleGripper (HWND hWnd, LPSIPWNDSTRUCT pWndData, UINT wMsg, 
                       LPARAM lParam) {
    POINT pt;

    pt.x = (short)LOWORD (lParam);
    pt.y = (short)HIWORD (lParam);

    switch (wMsg) {
    case WM_LBUTTONDOWN:
        if (pt.x > GRIPWIDTH+3)
            return 0;
        SetCapture (hWnd);
        pWndData->fMoving = TRUE;
        pWndData->ptMovBasis = pt;
        ClientToScreen (hWnd, &pt);
        pWndData->ptMovStart = pt;
        break;

    case WM_MOUSEMOVE:
        if (!pWndData->fMoving)
            return 0;
        break;
    case WM_LBUTTONUP:
        if (!pWndData->fMoving)
            return 0;
        ReleaseCapture();
        pWndData->fMoving = FALSE;
        ClientToScreen (hWnd, &pt);
        if ((abs (pWndData->ptMovStart.x - pt.x) < 3) &&
            (abs (pWndData->ptMovStart.y - pt.y) < 3))
            break;
        pt.x -= pWndData->ptMovBasis.x;
        pt.y -= pWndData->ptMovBasis.y;

        pWndData->imi.rcSipRect.right = FLOATWIDTH;
        pWndData->imi.rcSipRect.bottom = FLOATHEIGHT;
        pWndData->imi.rcSipRect.left = pt.x;
        pWndData->imi.rcSipRect.top = pt.y;
        pWndData->imi.rcSipRect.right += pt.x;
        pWndData->imi.rcSipRect.bottom += pt.y;

        pWndData->imi.fdwFlags &= ~SIPF_DOCKED;
        pWndData->imi.fdwFlags |= SIPF_ON;

        IIMCallback_SetImInfo(pWndData->pIMCallback, &pWndData->imi);
        break;

    case WM_LBUTTONDBLCLK:
        if (pt.x > GRIPWIDTH+3)
            return 0;
        ReleaseCapture();
        pWndData->fMoving = FALSE;
        pWndData->imi.fdwFlags |= (SIPF_DOCKED | SIPF_ON);
        pWndData->imi.rcSipRect = pWndData->rectDocked;
        IIMCallback_SetImInfo(pWndData->pIMCallback, &pWndData->imi);
        break;
    }
    pWndData->dwBtnDnFlags = 0;   // If we moved, no buttons down.
    return 1;
}
//----------------------------------------------------------------------
// DoMouseSip - Process mouse button messages for window. 
//
LRESULT CALLBACK DoMouseSip (HWND hWnd, UINT wMsg, WPARAM wParam, 
                             LPARAM lParam) {
    RECT rect;
    INT i, x, y, cx, cy, nChar;
    DWORD BtnDnFlags, dwShiftFlags = 0;
    LPSIPWNDSTRUCT pWndData;
    pWndData = (LPSIPWNDSTRUCT)GetWindowLong (hWnd, GWL_USERDATA);

    // See if moving gripper or gripper tap.
    if (HandleGripper (hWnd, pWndData, wMsg, lParam)) 
        return 0;

    // Compute the button grid.
    GetClientRect (hWnd, &rect);
    cx = (rect.right - rect.left - 3 - GRIPWIDTH) / CXBTNS;
    cy = (rect.bottom - rect.top - 3) / CYBTNS;
    x = ((LOWORD (lParam)-3-GRIPWIDTH) / cx);
    y = ((HIWORD (lParam)-3) / cy);
    i = (y * CXBTNS) + x;    // i now contains btn index.

    // Do small amount of message-specific processing.
    switch (wMsg) {
    case WM_LBUTTONDOWN:
        SetCapture (hWnd);
        // Fall through to WM_MOUSEMOVE case.
    case WM_MOUSEMOVE:
        BtnDnFlags = 1 << i;
        break;
    case WM_LBUTTONDBLCLK:
    case WM_LBUTTONUP:
        if (pWndData->dwBtnDnFlags)
            ReleaseCapture();
        BtnDnFlags = 0;
        nChar = g_tcBtnChar[i];
        IIMCallback_SendCharEvents(pWndData->pIMCallback, 
                                   g_BtnVChars[i], KeyStateDownFlag,
                                   1, &dwShiftFlags, &nChar);
        break;
    } 
    // Decide how to repaint wnd. If only 1 btn changed, just 
    // invalidate that rect. Otherwise, invalidate entire wnd.
    if ((wMsg == WM_MOUSEMOVE) && (BtnDnFlags !=pWndData->dwBtnDnFlags))
        InvalidateRect (hWnd, NULL, FALSE);
    else {
        i = 3+GRIPWIDTH;   // Compensate for the gripper on left side.
        SetRect (&rect, x*cx+i, y*cy, (x+1)*cx+i, (y+1)*cy);
        InvalidateRect (hWnd, &rect, FALSE);
    }
    pWndData->dwBtnDnFlags = BtnDnFlags;
    return 0;
}
//----------------------------------------------------------------------
// DoDestroySip - Process WM_DESTROY message for window.
//
LRESULT CALLBACK DoDestroySip (HWND hWnd, UINT wMsg, WPARAM wParam, 
                               LPARAM lParam) {
    LPSIPWNDSTRUCT pWndData;

    pWndData = (LPSIPWNDSTRUCT)GetWindowLong (hWnd, GWL_USERDATA);
    LocalFree (pWndData);
    return 0;
}
//----------------------------------------------------------------------
// DrawButton - Draws a button
//
INT DrawButton (HDC hdc, RECT *prect, LPTSTR pChar, BOOL fPressed) {

    if (!fPressed) {
        SelectObject (hdc, GetStockObject (BLACK_PEN));
        SelectObject (hdc, GetStockObject (WHITE_BRUSH));
        SetBkColor (hdc, RGB (255, 255, 255));
        SetTextColor (hdc, RGB (0, 0, 0));
    } else {
        SelectObject (hdc, GetStockObject (BLACK_BRUSH));
        SelectObject (hdc, GetStockObject (WHITE_PEN));
        SetTextColor (hdc, RGB (255, 255, 255));
        SetBkColor (hdc, RGB (0, 0, 0));
    }
    Rectangle (hdc, prect->left, prect->top, prect->right, 
               prect->bottom);
    Rectangle (hdc, prect->left+1, prect->top+1, prect->right+1, 
               prect->bottom+1);
    DrawText (hdc, pChar, 1, prect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
    return 0;
}

⌨️ 快捷键说明

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