📄 lview.c
字号:
//======================================================================
// LView - ListView control demonstration
//
// Written for the book Programming Windows CE
// Copyright (C) 1998 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commctrl.h> // Command bar includes
#include "LView.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("LView");
HINSTANCE hInst; // Program instance handle
HWND hMain;
//
// Data for simulated database
//
#define LVCNT 2000
LVDATAITEM lvdatabase[LVCNT];
// Defines and data for list view control cache
#define CACHESIZE 100
#define TOPCACHESIZE 100
#define BOTCACHESIZE 100
INT nCacheItemStart = 0, nCacheSize = 0;
LVDATAITEM lvdiCache[CACHESIZE];
LVDATAITEM lvdiTopCache[TOPCACHESIZE];
LVDATAITEM lvdiBotCache[BOTCACHESIZE];
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_SIZE, DoSizeMain,
WM_NOTIFY, DoNotifyMain,
WM_COMMAND, DoCommandMain,
WM_DESTROY, DoDestroyMain,
};
// Command message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
IDM_EXIT, DoMainCommandExit,
IDC_LICON, DoMainCommandChView,
IDC_SICON, DoMainCommandChView,
IDC_LIST, DoMainCommandChView,
IDC_RPT, DoMainCommandChView,
IDM_ABOUT, DoMainCommandAbout,
};
// Standard file bar button structure
const TBBUTTON tbCBCmboBtns[] = {
// BitmapIndex Command State Style UserData String
{0, 0, 0, TBSTYLE_SEP, 0, 0},
{VIEW_LARGEICONS, IDC_LICON, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_SMALLICONS, IDC_SICON, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_LIST, IDC_LIST, TBSTATE_ENABLED,
TBSTYLE_CHECKGROUP, 0, 0},
{VIEW_DETAILS, IDC_RPT, TBSTATE_ENABLED | TBSTATE_CHECKED,
TBSTYLE_CHECKGROUP, 0, 0}
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
HWND hwndMain;
int rc = 0;
// Initialize application.
rc = InitApp (hInstance);
if (rc) return rc;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
hMain = hwndMain;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitApp - Application initialization
//
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
INITCOMMONCONTROLSEX icex;
// 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 = NULL; // Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 1;
// Load the command bar common control class.
icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx (&icex);
// Initialize the fictional database.
InitDatabase ();
return 0;
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT ("LView"), // Window title
WS_VISIBLE, // Style flags
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
// Return fail code if window not created.
if (!IsWindow (hWnd)) return 0;
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
// Flush caches used with list view control.
FlushMainCache ();
FlushEndCaches ();
return nDefRC;
}
//======================================================================
// Message-handling procedures for MainWindow
//----------------------------------------------------------------------
// 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) {
HWND hwndCB, hwndLV;
INT i, nHeight;
LPCREATESTRUCT lpcs;
HIMAGELIST himlLarge, himlSmall;
HICON hIcon;
// Convert lParam into pointer to create structure.
lpcs = (LPCREATESTRUCT) lParam;
// Create a command bar.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Insert a menu.
CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);
// Add bitmap list followed by buttons.
CommandBar_AddBitmap (hwndCB, HINST_COMMCTRL, IDB_VIEW_SMALL_COLOR,
VIEW_BMPS, 0, 0);
CommandBar_AddButtons (hwndCB, dim(tbCBCmboBtns), tbCBCmboBtns);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
nHeight = CommandBar_Height (hwndCB);
//
// Create the list view control.
//
hwndLV = CreateWindowEx (0, WC_LISTVIEW, TEXT (""),
LVS_REPORT | LVS_SINGLESEL |
LVS_OWNERDATA | WS_VISIBLE | WS_CHILD |
WS_VSCROLL, 0, nHeight, lpcs->cx,
lpcs->cy - nHeight, hWnd,
(HMENU)IDC_LISTVIEW,
lpcs->hInstance, NULL);
// Destroy frame if window not created.
if (!IsWindow (hwndLV)) {
DestroyWindow (hWnd);
return 0;
}
// Add columns.
{
LVCOLUMN lvc;
lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT | LVCF_SUBITEM;
lvc.fmt = LVCFMT_LEFT;
lvc.cx = 150;
lvc.pszText = TEXT ("Name");
lvc.iSubItem = 0;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc);
lvc.mask |= LVCF_SUBITEM;
lvc.pszText = TEXT ("Type");
lvc.cx = 100;
lvc.iSubItem = 1;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc);
lvc.mask |= LVCF_SUBITEM;
lvc.pszText = TEXT ("Size");
lvc.cx = 100;
lvc.iSubItem = 2;
SendMessage (hwndLV, LVM_INSERTCOLUMN, 2, (LPARAM)&lvc);
}
// Add items.
ListView_SetItemCount (hwndLV, LVCNT);
LoadTopCache ();
LoadBotCache ();
// Create image list control for bitmaps for minimized bands.
i = GetSystemMetrics (SM_CXICON);
himlLarge = ImageList_Create(i, i, ILC_COLOR, 2, 0);
i = GetSystemMetrics (SM_CXSMICON);
himlSmall = ImageList_Create(i, i, ILC_COLOR, 2, 0);
// Load large and small icons into their respective image lists.
hIcon = LoadIcon (hInst, TEXT ("DocIcon"));
i = ImageList_AddIcon (himlLarge, hIcon);
hIcon = LoadImage (hInst, TEXT ("DocIcon"), IMAGE_ICON, 16, 16,
LR_DEFAULTCOLOR);
ImageList_AddIcon (himlSmall, hIcon);
ListView_SetImageList (hwndLV, himlLarge, LVSIL_NORMAL);
ListView_SetImageList (hwndLV, himlSmall, LVSIL_SMALL);
// Set cool new styles.
ListView_SetExtendedListViewStyle (hwndLV, LVS_EX_GRIDLINES |
LVS_EX_HEADERDRAGDROP |
LVS_EX_FULLROWSELECT);
return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){
HWND hwndLV;
RECT rect;
hwndLV = GetDlgItem (hWnd, IDC_LISTVIEW);
// Adjust the size of the client rect to take into account
// the command bar height.
GetClientRect (hWnd, &rect);
rect.top += CommandBar_Height (GetDlgItem (hWnd, IDC_CMDBAR));
SetWindowPos (hwndLV, NULL, rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
SWP_NOZORDER);
return 0;
}
//----------------------------------------------------------------------
// DoNotifyMain - Process WM_NOTIFY message for window.
//
LRESULT DoNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
int idItem;
LPNMHDR pnmh;
LPNMLISTVIEW pnmlv;
NMLVDISPINFO *pLVdi;
PLVDATAITEM pdi; // Pointer to data
LPNMLVCACHEHINT pLVch;
HWND hwndLV;
// Parse the parameters.
idItem = (int) wParam;
pnmh = (LPNMHDR)lParam;
hwndLV = pnmh->hwndFrom;
if (idItem == IDC_LISTVIEW) {
pnmlv = (LPNMLISTVIEW)lParam;
switch (pnmh->code) {
case LVN_GETDISPINFO:
pLVdi = (NMLVDISPINFO *)lParam;
// Get a pointer to the data either from the cache
// or from the actual database.
pdi = GetItemData (pLVdi->item.iItem);
if (pLVdi->item.mask & LVIF_IMAGE)
pLVdi->item.iImage = pdi->nImage;
if (pLVdi->item.mask & LVIF_PARAM)
pLVdi->item.lParam = 0;
if (pLVdi->item.mask & LVIF_STATE)
pLVdi->item.state = pdi->nState;
if (pLVdi->item.mask & LVIF_TEXT) {
switch (pLVdi->item.iSubItem) {
case 0:
lstrcpy (pLVdi->item.pszText, pdi->szName);
break;
case 1:
lstrcpy (pLVdi->item.pszText, pdi->szType);
break;
case 2:
wsprintf (pLVdi->item.pszText, TEXT ("%d"),
pdi->nSize);
break;
}
}
break;
case LVN_ODCACHEHINT:
pLVch = (LPNMLVCACHEHINT)lParam;
LoadMainCache (pLVch->iFrom, pLVch->iTo);
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -