📄 listwnd.cpp
字号:
//======================================================================
// ListWnd - List box control window code
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 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 ListWndWindowProc
const struct decodeUINT ListWndMessages[] = {
WM_CREATE, DoCreateListWnd,
WM_COMMAND, DoCommandListWnd,
WM_COMPAREITEM, DoCompareItemListWnd,
WM_DELETEITEM, DoDelItemListWnd,
WM_DRAWITEM, DoDrawItemListWnd,
WM_MEASUREITEM, DoMeasureItemListWnd,
};
// Structure defining the controls in the window
CTLWNDSTRUCT Lists[] = {
{TEXT ("combobox"), IDC_COMBOBOX, TEXT (""), 10, 10, 205, 100,
WS_VSCROLL},
{TEXT ("Listbox"), IDC_SNGLELIST, TEXT (""), 10, 35, 100, 90,
WS_VSCROLL | LBS_NOTIFY | LBS_OWNERDRAWFIXED},
{TEXT ("Listbox"), IDC_MULTILIST, TEXT (""), 115, 35, 100, 90,
WS_VSCROLL | LBS_EXTENDEDSEL | LBS_NOTIFY}
};
// Structure labeling the list box control WM_COMMAND notifications
NOTELABELS nlList[] = {{TEXT ("LBN_ERRSPACE "), (-2)},
{TEXT ("LBN_SELCHANGE"), 1},
{TEXT ("LBN_DBLCLK "), 2},
{TEXT ("LBN_SELCANCEL"), 3},
{TEXT ("LBN_SETFOCUS "), 4},
{TEXT ("LBN_KILLFOCUS"), 5},
};
// Structure labeling the combo box control WM_COMMAND notifications
NOTELABELS nlCombo[] = {{TEXT ("CBN_ERRSPACE "), (-1)},
{TEXT ("CBN_SELCHANGE "), 1},
{TEXT ("CBN_DBLCLK "), 2},
{TEXT ("CBN_SETFOCUS "), 3},
{TEXT ("CBN_KILLFOCUS "), 4},
{TEXT ("CBN_EDITCHANGE "), 5},
{TEXT ("CBN_EDITUPDATE "), 6},
{TEXT ("CBN_DROPDOWN "), 7},
{TEXT ("CBN_CLOSEUP "), 8},
{TEXT ("CBN_SELENDOK "), 9},
{TEXT ("CBN_SELENDCANCEL"), 10},
};
//----------------------------------------------------------------------
// InitListWnd - ListWnd window initialization
//
int InitListWnd (HINSTANCE hInstance) {
WNDCLASS wc;
// Register application ListWnd window class.
wc.style = WS_EX_TOPMOST; // Window style
wc.lpfnWndProc = ListWndProc; // 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 = LISTWND; // Window class name
if (RegisterClass (&wc) == 0) return 1;
return 0;
}
//======================================================================
// Message handling procedures for ListWindow
//----------------------------------------------------------------------
// ListWndProc - Callback function for application window
//
LRESULT CALLBACK ListWndProc (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(ListWndMessages); i++) {
if (wMsg == ListWndMessages[i].Code)
return (*ListWndMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoCreateListWnd - Process WM_CREATE message for window.
//
LRESULT DoCreateListWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
int i;
TCHAR szOut[64];
for (i = 0; i < dim(Lists); i++) {
CreateWindow (Lists[i].szClass, Lists[i].szTitle,
Lists[i].lStyle | WS_VISIBLE | WS_CHILD | WS_BORDER,
Lists[i].x, Lists[i].y, Lists[i].cx, Lists[i].cy,
hWnd, (HMENU) Lists[i].nID, hInst, NULL);
}
// Add items to each listbox/combobox.
for (i = 0; i < 20; i++) {
// Since we're owner draw, we don't pass a string
SendDlgItemMessage (hWnd, IDC_SNGLELIST, LB_ADDSTRING, 0, i);
wsprintf (szOut, TEXT ("Item %d"), i);
SendDlgItemMessage (hWnd, IDC_MULTILIST, LB_ADDSTRING, 0,
(LPARAM)szOut);
SendDlgItemMessage (hWnd, IDC_COMBOBOX, CB_ADDSTRING, 0,
(LPARAM)szOut);
}
// Set initial selection.
SendDlgItemMessage (hWnd, IDC_COMBOBOX, CB_SETCURSEL, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoDelItemListWnd - Process WM_DELETEITEM message for window.
//
LRESULT DoDelItemListWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
LPDELETEITEMSTRUCT lpdis = (LPDELETEITEMSTRUCT) lParam;
RptMessage (hWnd, wParam, TEXT("WM_DELETEITEM item %d"),
lpdis->itemID);
return 0;
}
//----------------------------------------------------------------------
// DoCompareItemListWnd - Process WM_COMPAREITEM message for window.
//
LRESULT DoCompareItemListWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
RptMessage (hWnd, wParam, TEXT("WM_COMPAREITEM"), wMsg, wMsg);
return 0;
}
//----------------------------------------------------------------------
// DoDrawItemListWnd - Process WM_DRAWITEM message for window.
//
LRESULT DoDrawItemListWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
TCHAR szOut[128];
LPDRAWITEMSTRUCT pdi = (LPDRAWITEMSTRUCT)lParam;
HBRUSH hbrBack;
// Print odd lines with greener tint
if (pdi->itemID & 0x01)
hbrBack = CreateSolidBrush (RGB (230, 255, 230));
else
hbrBack = CreateSolidBrush (RGB (240, 255, 240));
if (pdi->itemState & ODS_SELECTED)
SetTextColor (pdi->hDC, RGB (255, 0, 0));
else
SetTextColor (pdi->hDC, RGB (0, 0, 0));
FillRect (pdi->hDC, &pdi->rcItem, hbrBack);
wsprintf (szOut, TEXT("Item %d"), pdi->itemID);
SetBkMode (pdi->hDC, TRANSPARENT);
DrawText (pdi->hDC, szOut, -1, &pdi->rcItem, DT_CENTER);
DeleteObject (hbrBack);
RptMessage (hWnd, wParam, TEXT ("WM_DRAWITEM Act:%x State:%x"),
pdi->itemAction, pdi->itemState);
return 0;
}
//----------------------------------------------------------------------
// DoMeasureItemListWnd - Process WM_MEASUREITEM message for window.
//
LRESULT DoMeasureItemListWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
LPMEASUREITEMSTRUCT lpMI = (LPMEASUREITEMSTRUCT)lParam;
// Measure the height of font in listbox
HDC hdc = GetDC (GetDlgItem (hWnd, lpMI->CtlID));
TEXTMETRIC tm;
GetTextMetrics (hdc, &tm);
lpMI->itemHeight = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC (GetDlgItem (hWnd, lpMI->CtlID), hdc);
RptMessage (hWnd, wParam, TEXT("WM_MEASUREITEM ctl:%d item:%d"),
lpMI->CtlID, lpMI->itemID);
return TRUE;
}
//----------------------------------------------------------------------
// DoCommandListWnd - Process WM_COMMAND message for window.
//
LRESULT DoCommandListWnd (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
if (LOWORD (wParam) == IDC_COMBOBOX)
PrintCmdMessage (hWnd, wParam, lParam, nlCombo, dim (nlCombo));
else
PrintCmdMessage (hWnd, wParam, lParam, nlList, dim (nlList));
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -