📄 fileview.cpp
字号:
//======================================================================
// FileView - A Windows CE file viewer
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include <commctrl.h> // Command bar includes
#include <commdlg.h> // Common dialog includes
#include "FileView.h" // Program-specific stuff
#define BUFFSIZE 16384
//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT ("FileView");
extern TCHAR szViewerCls[];
HINSTANCE hInst; // Program instance handle
HANDLE g_hFile=INVALID_HANDLE_VALUE; // Handle to the opened file
PBYTE g_pBuff = 0; // Pointer to file data buffer
// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_CREATE, DoCreateMain,
WM_SIZE, DoSizeMain,
WM_COMMAND, DoCommandMain,
WM_DESTROY, DoDestroyMain,
};
// Command message dispatch for MainWindowProc
const struct decodeCMD MainCommandItems[] = {
IDM_OPEN, DoMainCommandOpen,
IDM_EXIT, DoMainCommandExit,
IDM_ABOUT, DoMainCommandAbout,
};
//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
HWND hwndMain;
MSG msg;
// 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){
HWND hWnd;
WNDCLASS wc;
INITCOMMONCONTROLSEX icex;
#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
// Save program instance handle in global variable.
hInst = hInstance;
// 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;
InitCommonControlsEx (&icex);
// Create main window.
hWnd = CreateWindow (szAppName, TEXT ("FileView"),
WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
hInstance, NULL);
if (!IsWindow (hWnd)) return 0; // Fail code if window not created.
// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
if (g_hFile != INVALID_HANDLE_VALUE)
CloseHandle (g_hFile); // Close the opened file.
if (g_pBuff)
LocalFree (g_pBuff); // Free buffer.
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 function.
//
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;
LPCREATESTRUCT lpcs;
// Convert lParam into pointer to create structure.
lpcs = (LPCREATESTRUCT) lParam;
// 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);
hwndChild = CreateWindowEx (0,TEXT("edit"), TEXT(""), WS_VISIBLE |
WS_CHILD | ES_MULTILINE | WS_VSCROLL |
WS_HSCROLL | ES_READONLY, 0, 0, lpcs->cx,
lpcs->cy, hWnd, (HMENU)ID_VIEWER, hInst,
0);
// Destroy frame if window not created.
if (!IsWindow (hwndChild)) {
DestroyWindow (hWnd);
return 0;
}
// Allocate a buffer.
g_pBuff = (PBYTE)LocalAlloc (LMEM_FIXED, BUFFSIZE);
if (!g_pBuff) {
MessageBox (NULL, TEXT ("Not enough memory"),
TEXT ("Error"), MB_OK);
return 0;
}
return 0;
}
//----------------------------------------------------------------------
// DoSizeMain - Process WM_SIZE message for window.
//
LRESULT DoSizeMain (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){
RECT rect;
// 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 (GetDlgItem (hWnd, ID_VIEWER), NULL, rect.left,
rect.top, (rect.right - rect.left),
rect.bottom - rect.top, SWP_NOZORDER);
return 0;
}
//----------------------------------------------------------------------
// DoCommandMain - Process WM_COMMAND message for window.
//
LRESULT DoCommandMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
WORD idItem, wNotifyCode;
HWND hwndCtl;
INT i;
// Parse the parameters.
idItem = (WORD) LOWORD (wParam);
wNotifyCode = (WORD) HIWORD (wParam);
hwndCtl = (HWND) lParam;
// Call routine to handle control message.
for (i = 0; i < dim(MainCommandItems); i++) {
if (idItem == MainCommandItems[i].Code)
return (*MainCommandItems[i].Fxn)(hWnd, idItem, hwndCtl,
wNotifyCode);
}
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
//----------------------------------------------------------------------
// DoMainCommandOpen - Process File Open command.
//
LPARAM DoMainCommandOpen (HWND hWnd, WORD idItem, HWND hwndCtl,
WORD wNotifyCode) {
TCHAR szFileName[MAX_PATH];
HWND hwndViewer;
DWORD cBytes;
LPTSTR pXLateBuff = 0;
int lFileSize, i;
BOOL fUnicode = TRUE;
HANDLE hFileTmp;
hwndViewer = GetDlgItem (hWnd, ID_VIEWER);
// Ask the user for the file name
if (MyGetFileName (hWnd, szFileName, dim(szFileName)) == 0)
return 0;
// Open the file.
hFileTmp = CreateFile (szFileName, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFileTmp == INVALID_HANDLE_VALUE) {
MessageBox (hWnd, TEXT("Couldn't open file"), szAppName, MB_OK);
return 0;
}
if (g_hFile) {
CloseHandle (g_hFile);
// clear the edit box
SendMessage (hwndViewer, EM_SETSEL, 0, -1);
SendMessage (hwndViewer, EM_REPLACESEL, 0, (LPARAM)TEXT(""));
}
g_hFile = hFileTmp;
// Get the size of the file
lFileSize = (int)GetFileSize (g_hFile, NULL);
// See if file > 2Gig
if (lFileSize < 0) return 0;
if (!ReadFile (g_hFile, g_pBuff, BUFFSIZE-1, &cBytes, NULL))
return 0;
// Trivial check to see if file Unicode. Assumes english
for (i = 0; (i < 16) && (i < (int)cBytes); i++) {
if (*((TCHAR *)g_pBuff+i) > 0x100)
fUnicode = FALSE;
}
if (!fUnicode) {
pXLateBuff = (LPTSTR)LocalAlloc (LPTR, BUFFSIZE*sizeof (TCHAR));
if (pXLateBuff == 0) return 0;
}
while (lFileSize > 0) {
// Remove any selection
SendMessage (hwndViewer, EM_SETSEL, (WPARAM)-1, 0);
*(g_pBuff+cBytes) = 0;
lFileSize -= cBytes;
if (!fUnicode) {
mbstowcs (pXLateBuff, (char *)g_pBuff, cBytes+1);
SendMessage (hwndViewer, EM_REPLACESEL, 0,
(LPARAM)pXLateBuff);
} else
SendMessage (hwndViewer, EM_REPLACESEL, 0, (LPARAM)g_pBuff);
if (!ReadFile (g_hFile, g_pBuff, BUFFSIZE-1, &cBytes, NULL))
break;
}
if (pXLateBuff) LocalFree ((HLOCAL)pXLateBuff);
// Scroll the control to the top of the file
SendMessage (hwndViewer, EM_SETSEL, 0, 0);
SendMessage (hwndViewer, EM_SCROLLCARET, 0, 0);
return 0;
}
//----------------------------------------------------------------------
// 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 a modal dialog.
DialogBox (hInst, TEXT ("aboutbox"), hWnd, AboutDlgProc);
return 0;
}
//======================================================================
// About Dialog procedure
//
BOOL CALLBACK AboutDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch (wMsg) {
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDOK:
case IDCANCEL:
EndDialog (hWnd, 0);
return TRUE;
}
break;
}
return FALSE;
}
//----------------------------------------------------------------------
// MyGetFileName - Returns a filename using the common dialog
//
INT MyGetFileName (HWND hWnd, LPTSTR szFileName, INT nMax) {
OPENFILENAME of;
const LPTSTR pszOpenFilter = TEXT ("All Documents (*.*)\0*.*\0\0");
szFileName[0] = '\0'; // Initial filename
memset (&of, 0, sizeof (of)); // Initial file open structure
of.lStructSize = sizeof (of);
of.hwndOwner = hWnd;
of.lpstrFile = szFileName;
of.nMaxFile = nMax;
of.lpstrFilter = pszOpenFilter;
of.Flags = 0;
if (GetOpenFileName (&of))
return lstrlen (szFileName);
else
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -