[翻译]-WinCE 程序设计 (3rd 版)--4.3 Windows控件(续五)

  //----------------------------------------------------------------------
// DoCommandStatWnd - Process WM_COMMAND message for window.
//
LRESULT DoCommandStatWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
                          LPARAM lParam) {
    TCHAR szOut[128];
    int i;
  
    for (i = 0; i < dim(nlStatic); i++) {
        if (HIWORD (wParam) == nlStatic[i].wNotification) {
            lstrcpy (szOut, nlStatic[i].pszLabel);
            break;
        }
    }
    if (i == dim(nlStatic))
        wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam));
  
    SendMessage (GetParent (hWnd), MYMSG_ADDLINE, wParam,
                 (LPARAM)szOut);
    return 0;
}
ScrollWnd.cpp
//======================================================================
// ScrollWnd - Scroll bar control window code
//
// Written for the book Programming Windows CE
// Copyright (C) 2001 Douglas Boling
//======================================================================
#include <windows.h>                 // For all that Windows stuff
#include "Ctlview.h"                 // Program-specific stuff
  
extern HINSTANCE hInst;
//----------------------------------------------------------------------
// Global data
//
// Message dispatch table for ScrollWndWindowProc
const struct decodeUINT ScrollWndMessages[] = {
    WM_CREATE, DoCreateScrollWnd,
    WM_HSCROLL, DoVScrollScrollWnd,
    WM_VSCROLL, DoVScrollScrollWnd,
};
  
// Structure defining the controls in the window
CTLWNDSTRUCT Scrolls [] = {
    {TEXT ("Scrollbar"), IDC_LRSCROLL, TEXT (""),
     10, 10, 150, 23, SBS_HORZ},
  
    {TEXT ("Scrollbar"), IDC_UDSCROLL, TEXT (""),
     180, 10, 23, 120, SBS_VERT},
};
  
// Structure labeling the scroll bar control scroll codes for WM_VSCROLL
NOTELABELS nlVScroll[] = {{TEXT ("SB_LINEUP       "), 0},
                          {TEXT ("SB_LINEDOWN     "), 1},
                          {TEXT ("SB_PAGEUP       "), 2},
                          {TEXT ("SB_PAGEDOWN     "), 3},
                          {TEXT ("SB_THUMBPOSITION"), 4},
                          {TEXT ("SB_THUMBTRACK   "), 5},
                          {TEXT ("SB_TOP          "), 6},
                          {TEXT ("SB_BOTTOM       "), 7},
                          {TEXT ("SB_ENDSCROLL    "), 8},
};
// Structure labeling the scroll bar control scroll codes for WM_HSCROLL
NOTELABELS nlHScroll[] = {{TEXT ("SB_LINELEFT     "), 0},
                          {TEXT ("SB_LINERIGHT    "), 1},
                          {TEXT ("SB_PAGELEFT     "), 2},
                          {TEXT ("SB_PAGERIGHT    "), 3},
                          {TEXT ("SB_THUMBPOSITION"), 4},
                          {TEXT ("SB_THUMBTRACK   "), 5},
                          {TEXT ("SB_LEFT         "), 6},
                          {TEXT ("SB_RIGHT        "), 7},
                          {TEXT ("SB_ENDSCROLL    "), 8},
};
//----------------------------------------------------------------------
// InitScrollWnd - ScrollWnd window initialization
//
int InitScrollWnd (HINSTANCE hInstance) {
    WNDCLASS wc;
  
    // Register application ScrollWnd window class.
    wc.style = 0;                             // Window style
    wc.lpfnWndProc = ScrollWndProc;           // 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 = SCROLLWND;             // Window class name
  
    if (RegisterClass (&wc) == 0) return 1;
  
    return 0;
}
//======================================================================
// Message handling procedures for ScrollWindow
//----------------------------------------------------------------------
// ScrollWndProc - Callback function for application window
//
LRESULT CALLBACK ScrollWndProc (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(ScrollWndMessages); i++) {
        if (wMsg == ScrollWndMessages[i].Code)
            return (*ScrollWndMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
    }
    return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateScrollWnd - Process WM_CREATE message for window.
//
LRESULT DoCreateScrollWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
                           LPARAM lParam) {
    int i;
    for (i = 0; i < dim(Scrolls); i++) {
        CreateWindow (Scrolls[i].szClass, Scrolls[i].szTitle,
                      Scrolls[i].lStyle | WS_VISIBLE | WS_CHILD,
                      Scrolls[i].x, Scrolls[i].y, Scrolls[i].cx,
                      Scrolls[i].cy,
                      hWnd, (HMENU) Scrolls[i].nID, hInst, NULL);
    }
    return 0;
}
//----------------------------------------------------------------------
// DoVScrollScrollWnd - Process WM_VSCROLL message for window.
//
LRESULT DoVScrollScrollWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
                            LPARAM lParam) {
    TCHAR szOut[128];
    SCROLLINFO si;
    int i, sPos;
  
    // Update the report window.
    if (GetDlgItem (hWnd, 101) == (HWND)lParam) {
  
        for (i = 0; i < dim(nlVScroll); i++) {
            if (LOWORD (wParam) == nlVScroll[i].wNotification) {
                lstrcpy (szOut, nlVScroll[i].pszLabel);
                break;
            }
        }
        if (i == dim(nlVScroll))
            wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam));
    } else {
        for (i = 0; i < dim(nlHScroll); i++) {
            if (LOWORD (wParam) == nlHScroll[i].wNotification) {
                lstrcpy (szOut, nlHScroll[i].pszLabel);
                break;
            }
        }
        if (i == dim(nlHScroll))
            wsprintf (szOut, TEXT ("notification: %x"), HIWORD (wParam));
    }
    SendMessage (GetParent (hWnd), MYMSG_ADDLINE, -1, (LPARAM)szOut);
  
    // Get scroll bar position.
    si.cbSize = sizeof (si);
    si.fMask = SIF_POS;
    GetScrollInfo ((HWND)lParam, SB_CTL, &si);
    sPos = si.nPos;
    // Act on the scroll code.
    switch (LOWORD (wParam)) {
    case SB_LINEUP:      // Also SB_LINELEFT
        sPos -= 2;
        break;
    case SB_LINEDOWN:    // Also SB_LINERIGHT
        sPos += 2;
        break;
  
    case SB_PAGEUP:      // Also SB_PAGELEFT
        sPos -= 10;
        break;
  
    case SB_PAGEDOWN:    // Also SB_PAGERIGHT
        sPos += 10;
        break;
  
    case SB_THUMBPOSITION:
        sPos = HIWORD (wParam);
        break;
    }
    // Check range.
    if (sPos < 0)
        sPos = 0;
    if (sPos > 100)
        sPos = 100;
  
    // Update scroll bar position.
    si.cbSize = sizeof (si);
    si.nPos = sPos;
    si.fMask = SIF_POS;
    SetScrollInfo ((HWND)lParam, SB_CTL, &si, TRUE);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值