📄 listview.cpp
字号:
/****************************************************************************
*
*
* PROGRAM: ListView.cpp
*
* PURPOSE: Demonstrates the use of the list view control
*
****************************************************************************/
#include <windows.h> // includes basic windows functionality
#include <commctrl.h> // includes the common control header
#include <string.h>
#include "listview.h"
// global current instance
HINSTANCE hInst;
// global array of houses
HOUSEINFO rgHouseInfo[] =
{
{TEXT("100 Berry Lane"), TEXT("Redmond"), 175000, 3, 2 },
{TEXT("523 Apple Road"), TEXT("Redmond"), 125000, 4, 2},
{TEXT("1212 Peach Street"), TEXT("Redmond"),200000, 4, 3},
{TEXT("22 Daffodil Lane"), TEXT("Bellevue"), 2500000, 4, 4},
{TEXT("33542 Orchid Road"), TEXT("Bellevue"), 180000, 3, 2},
{TEXT("64134 Lily Street"), TEXT("Bellevue"), 250000, 4, 3},
{TEXT("33 Nicholas Lane"), TEXT("Seattle"), 350000, 3, 2},
{TEXT("555 Tracy Road"), TEXT("Seattle"), 140000, 3, 2},
{TEXT("446 Jean Street"), TEXT("Seattle"), 225000, 4, 3}
};
//handle to the MenuBar
HWND hwndCB;
HMENU hmenuCB;
/////////////////////////// Pocket PC only samples ///////////////////////
#if _WIN32_WCE < 212//non-Pocket PC devices
#error 'This sample works on Pocket PC devices only'
#endif
#include <aygshell.h>
////////////////////////// SHGetMenu Macro's
#if _WIN32_WCE > 211 //Pocket PC devices
//#define SHCMBM_SETSUBMENU (WM_USER + 400)
//#define SHCMBM_GETSUBMENU (WM_USER + 401) // lParam == ID
//#define SHCMBM_GETMENU (WM_USER + 402) // get the owning hmenu (as specified in the load resource)
#define SHGetMenu(hWndMB) (HMENU)SendMessage((hWndMB), SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
#define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU);
#define SHSetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_SETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU);
//Alternate definition
#define SHMenuBar_GetMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU);
#else //non-Pocket PC devices
#define SHGetMenu(hWndCB) (HMENU)CommandBar_GetMenu(hWndCB, 0)
#define SHGetSubMenu(hWndCB,ID_MENU) (HMENU)GetSubMenu((HMENU)CommandBar_GetMenu(hWndCB, 0), ID_MENU)
#define SHSetSubMenu(hWndMB,ID_MENU)
#endif
/////////////////////////
//Menu Bar Height
#define MENU_HEIGHT 26
/****************************************************************************
*
* FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
*
* PURPOSE: calls initialization function, processes message loop
*
****************************************************************************/
int APIENTRY WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow
)
{
MSG msg;
if (!InitApplication(hInstance))
return (FALSE);
/* Perform initializations that apply to a specific instance */
if (!InitInstance(hInstance, nCmdShow))
return (FALSE);
/* Acquire and dispatch messages until a WM_QUIT message is received. */
while (GetMessage(&msg,
NULL,
0,
0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
/****************************************************************************
*
* FUNCTION: InitApplication(HANDLE)
*
* PURPOSE: Initializes window data and registers window class
*
****************************************************************************/
BOOL InitApplication(HINSTANCE hInstance) /* current instance */
{
WNDCLASS wcListview;
/* Fill in window class structure with parameters that describe the */
/* main window. */
wcListview.style = 0;
wcListview.lpfnWndProc = (WNDPROC)MainWndProc;
wcListview.cbClsExtra = 0;
wcListview.cbWndExtra = 0;
wcListview.hInstance = hInstance;
wcListview.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(APP_ICON));
wcListview.hCursor = 0;
wcListview.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcListview.lpszMenuName = 0;
wcListview.lpszClassName = TEXT("ListviewWClass");
return (RegisterClass(&wcListview));
}
/****************************************************************************
*
* FUNCTION: InitInstance(HANDLE, int)
*
* PURPOSE: Saves instance handle and creates main window
*
****************************************************************************/
BOOL InitInstance(
HINSTANCE hInstance,
int nCmdShow)
{
HWND hWndMain;
hInst = hInstance;
hWndMain = CreateWindow(
TEXT("ListviewWClass"),
TEXT("Listview Sample"),
WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
/* If window could not be created, return "failure" */
if (!hWndMain)
return (FALSE);
/* Make the window visible; update its client area; and return "success" */
ShowWindow(hWndMain, nCmdShow);
UpdateWindow(hWndMain);
return (TRUE);
}
/****************************************************************************
*
* FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
*
* PURPOSE: Processes messages
*
****************************************************************************/
LONG APIENTRY MainWndProc(
HWND hWnd, /* window handle */
UINT message, /* type of message */
UINT wParam, /* additional information */
LONG lParam) /* additional information */
{
static HWND hWndListView;
DWORD dwStyle;
switch (message)
{
case WM_CREATE:
//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
{
RECT rc;
GetWindowRect(hWnd, &rc);
// adjust for menu / titlebar height
rc.bottom -= (2*MENU_HEIGHT -1);
MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
}
//Create a MenuBar
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDM_MAIN_MENU;
mbi.hInstRes = hInst;
mbi.nBmpId = 0;
mbi.cBmpImages = 0;
if (!SHCreateMenuBar(&mbi))
MessageBox(hWnd, L"SHCreateMenuBar Failed", L"Error", MB_OK);
hwndCB = mbi.hwndMB;
hmenuCB = SHGetSubMenu(hwndCB, IDM_MAIN_MENUITEM1);
dwStyle = GetWindowLong(hWndListView, GWL_STYLE);
// set inital radio check to the detail/report view
if ((dwStyle & LVS_TYPEMASK) != LVS_REPORT)
SetWindowLong(hWndListView, GWL_STYLE,
(dwStyle & ~LVS_TYPEMASK) | LVS_REPORT);
CheckMenuRadioItem(hmenuCB, IDM_LARGEICON, IDM_REPORTVIEW, IDM_REPORTVIEW, MF_BYCOMMAND);
hWndListView = CreateListView( hWnd );
if (hWndListView == NULL)
MessageBox (NULL, TEXT("Listview not created!"), NULL, MB_OK );
break;
case WM_NOTIFY:
return( NotifyHandler(hWnd, message, wParam, lParam));
break;
case WM_COMMAND:
switch( LOWORD( wParam ))
{
case IDM_LARGEICON:
dwStyle = GetWindowLong(hWndListView, GWL_STYLE);
if ((dwStyle & LVS_TYPEMASK) != LVS_ICON)
SetWindowLong(hWndListView, GWL_STYLE,
(dwStyle & ~LVS_TYPEMASK) | LVS_ICON);
CheckMenuRadioItem(hmenuCB, IDM_LARGEICON, IDM_REPORTVIEW, IDM_LARGEICON, MF_BYCOMMAND);
break;
case IDM_SMALLICON:
dwStyle = GetWindowLong(hWndListView, GWL_STYLE);
if ((dwStyle & LVS_TYPEMASK) != LVS_SMALLICON)
SetWindowLong(hWndListView, GWL_STYLE,
(dwStyle & ~LVS_TYPEMASK) | LVS_SMALLICON);
CheckMenuRadioItem(hmenuCB, IDM_LARGEICON, IDM_REPORTVIEW, IDM_SMALLICON, MF_BYCOMMAND);
break;
case IDM_LISTVIEW:
dwStyle = GetWindowLong(hWndListView, GWL_STYLE);
if ((dwStyle & LVS_TYPEMASK) != LVS_LIST)
SetWindowLong(hWndListView, GWL_STYLE,
(dwStyle & ~LVS_TYPEMASK) | LVS_LIST);
CheckMenuRadioItem(hmenuCB, IDM_LARGEICON, IDM_REPORTVIEW, IDM_LISTVIEW, MF_BYCOMMAND);
break;
case IDM_REPORTVIEW:
dwStyle = GetWindowLong(hWndListView, GWL_STYLE);
if ((dwStyle & LVS_TYPEMASK) != LVS_REPORT)
SetWindowLong(hWndListView, GWL_STYLE,
(dwStyle & ~LVS_TYPEMASK) | LVS_REPORT);
CheckMenuRadioItem(hmenuCB, IDM_LARGEICON, IDM_REPORTVIEW, IDM_REPORTVIEW, MF_BYCOMMAND);
break;
case IDM_EXIT:
PostQuitMessage(0);
break;
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, (DLGPROC)About);
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
break;
case WM_SIZE:
MoveWindow(hWndListView, 0, 0, LOWORD(lParam),HIWORD(lParam),TRUE);
break;
case WM_DESTROY: /* message: window being destroyed */
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
/****************************************************************************
*
* FUNCTION: CreateListView(HWND)
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -