📄 regview.cpp
字号:
//======================================================================
// RegView - Windows CE registry viewer
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commctrl.h> // Common control includes
#include "RegView.h" // Program-specific stuff
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("RegView");
HINSTANCE hInst; // Program instance handle
int nDivPct = 40; // Divider setting between windows
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_SIZE, DoSizeMain,
WM_COMMAND, DoCommandMain,
WM_NOTIFY, DoNotifyMain,
WM_DESTROY, DoDestroyMain,
};
// Command message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
IDM_EXIT, DoMainCommandExit,
IDM_ABOUT, DoMainCommandAbout,
};
// Notification message dispatch for MainWindowProc
const struct decodeNotify MainNotifyItems[] = {
ID_TREEV, DoMainNotifyTreeV,
};
//======================================================================
//
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
HWND hwndMain;
MSG msg;
int rc = 0;
// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0)
return 0x10;
// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
WNDCLASS wc;
INITCOMMONCONTROLSEX icex;
HWND hWnd;
// Save program instance handle in global variable.
hInst = hInstance;
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// For Windows Mobile devices, allow only one instance of the app
hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return 0;
}
#endif
// 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 = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name
if (RegisterClass (&wc) == 0) return 0;
// Load the command bar common control class.
icex.dwSize = sizeof (INITCOMMONCONTROLSEX);
icex.dwICC = ICC_BAR_CLASSES | ICC_TREEVIEW_CLASSES |
ICC_LISTVIEW_CLASSES;
InitCommonControlsEx (&icex);
// Create main window.
hWnd = CreateWindow (szAppName, TEXT ("RegView"), WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
// 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) {
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, hwndChild;
RECT rect;
// Create a minimal command bar that has only a menu and an
// exit button.
hwndCB = CommandBar_Create (hInst, hWnd, IDC_CMDBAR);
// Insert the menu.
CommandBar_InsertMenubar (hwndCB, hInst, ID_MENU, 0);
// Add exit button to command bar.
CommandBar_AddAdornments (hwndCB, 0, 0);
// The position of the child windows will be set in WM_SIZE
SetRect (&rect, 0, 0, 10, 10);
// Create the tree view control
hwndChild = CreateTV (hWnd, &rect);
if (!IsWindow (hwndChild)) {
DestroyWindow (hWnd);
return 0;
}
// Create the list view control
hwndChild = CreateLV (hWnd, &rect);
// Destroy frame if window not created.
if (!IsWindow (hwndChild)) {
DestroyWindow (hWnd);
return 0;
}
// Insert the base keys.
InsertTV (hWnd, NULL, TEXT ("HKEY_CLASSES_ROOT"),
(LPARAM)HKEY_CLASSES_ROOT, 1);
InsertTV (hWnd, NULL, TEXT ("HKEY_CURRENT_USER"),
(LPARAM)HKEY_CURRENT_USER, 1);
InsertTV (hWnd, NULL, TEXT ("HKEY_LOCAL_MACHINE"),
(LPARAM)HKEY_LOCAL_MACHINE, 1);
InsertTV (hWnd, NULL, TEXT ("HKEY_USERS"),
(LPARAM)HKEY_USERS, 1);
return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){
HWND hwndLV, hwndTV;
RECT rect, rectLV, rectTV;
int nDivPos, cx, cy;
hwndTV = GetDlgItem (hWnd, ID_TREEV);
hwndLV = GetDlgItem (hWnd, ID_LISTV);
// 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));
cx = rect.right - rect.left;
cy = rect.bottom - rect.top;
// Narrow screens, stack the windows; otherwise, they're side by side.
if (GetSystemMetrics (SM_CXSCREEN) < GetSystemMetrics (SM_CYSCREEN)){
nDivPos = (cy * nDivPct)/100;
SetRect (&rectTV, rect.left, rect.top, cx, nDivPos);
SetRect (&rectLV, rect.left, nDivPos + rect.top, cx, cy - nDivPos);
} else {
nDivPos = (cx * nDivPct)/100;
SetRect (&rectTV, rect.left, rect.top, nDivPos, cy);
SetRect (&rectLV, nDivPos, rect.top, cx - nDivPos, cy);
}
// The child window positions
SetWindowPos (hwndTV, NULL, rectTV.left, rectTV.top,
rectTV.right, rectTV.bottom, SWP_NOZORDER);
SetWindowPos (hwndLV, NULL, rectLV.left, rectLV.top,
rectLV.right, rectLV.bottom, SWP_NOZORDER);
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
// Parse the parameters.
WORD idItem = (WORD) LOWORD (wParam);
WORD wNotifyCode = (WORD) HIWORD (wParam);
HWND hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (int i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
return 0;
}
//----------------------------------------------------------------------
// DoNotifyMain - Process WM_NOTIFY message for window.
//
LRESULT DoNotifyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
// Parse the parameters.
UINT idItem = wParam;
LPNMHDR pHdr = (LPNMHDR) lParam;
HWND hCtl = pHdr->hwndFrom;
// Call routine to handle control message.
for (int i = 0; i < dim(MainNotifyItems); i++) {
if (idItem == MainNotifyItems[i].Code)
return (*MainNotifyItems[i].Fxn)(hWnd, idItem, hCtl, pHdr);
}
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
//======================================================================
// Command handler routines
//----------------------------------------------------------------------
// DoMainCommandExit - Process Program Exit command.
//
LPARAM DoMainCommandExit (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
SendMessage (hWnd, WM_CLOSE, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// DoMainCommandAbout - Process the Help | About menu command.
//
LPARAM DoMainCommandAbout(HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
// Use DialogBox to create modal dialog box.
DialogBox (hInst, TEXT ("aboutbox"), hWnd, AboutDlgProc);
return 0;
}
//======================================================================
// Notify handler routines
//----------------------------------------------------------------------
// DoMainNotifyTreeV - Process notify message for list view.
//
LPARAM DoMainNotifyTreeV (HWND hWnd, UINT idItem, HWND hwndCtl,
LPNMHDR pnmh) {
TCHAR szKey[256];
HKEY hRoot;
HTREEITEM hChild, hNext;
int nMax;
LPNM_TREEVIEW pNotifyTV = (LPNM_TREEVIEW) pnmh;
switch (pnmh->code) {
case TVN_ITEMEXPANDED:
if (pNotifyTV->action == TVE_COLLAPSE) {
// Delete the children so that on next open, they will
// be reenumerated.
hChild = TreeView_GetChild (hwndCtl,
pNotifyTV->itemNew.hItem);
while (hChild) {
hNext = TreeView_GetNextItem (hwndCtl, hChild,
TVGN_NEXT);
TreeView_DeleteItem (hwndCtl, hChild);
hChild = hNext;
}
}
break;
case TVN_SELCHANGED:
nMax = dim(szKey);
GetTree (hWnd, pNotifyTV->itemNew.hItem, &hRoot,
szKey, &nMax);
EnumValues (hWnd, hRoot, szKey);
break;
case TVN_ITEMEXPANDING:
if (pNotifyTV->action == TVE_EXPAND) {
nMax = dim(szKey);
GetTree (hWnd, pNotifyTV->itemNew.hItem, &hRoot,
szKey, &nMax);
EnumChildren (hWnd, pNotifyTV->itemNew.hItem,
hRoot, szKey, dim (szKey));
TreeView_SortChildren (hwndCtl,
pNotifyTV->itemNew.hItem, TRUE);
}
break;
}
return 0;
}
//----------------------------------------------------------------------
// CreateLV - Create list view control.
//
HWND CreateLV (HWND hWnd, RECT *prect) {
HWND hwndLV;
LVCOLUMN lvc;
// Create report window. Size it so that it fits under
// the command bar and fills the remaining client area.
hwndLV = CreateWindowEx (0, WC_LISTVIEW, TEXT (""),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -