📄 fileopen.cpp
字号:
// FileOpen.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <winbase.h>
#include <windowsx.h>
#include <commctrl.h>
#include <Commdlg.h>
#include "FileOpen.h"
#define MAX_LOADSTRING 100
//constants for FileOpen
#define FILE_SAVE 0
#define FILE_OPEN 1
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
DWORD dwFileCreateFlags; // Flags for CreateFile
CHAR szPath[MAX_PATH]; // Initial Directory for OpenFile Dlg
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE hInstance, LPTSTR szWindowClass);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK ChooseFilePathDlgProc(HWND, UINT, WPARAM, LPARAM );
BOOL CALLBACK ChooseFilePermsDlgProc(HWND, UINT, WPARAM, LPARAM );
LPOPENFILENAME DoFileOpen (HWND, int);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FILEOPEN);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FILEOPEN));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name
hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
LoadString(hInstance, IDC_FILEOPEN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance, szWindowClass);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (hwndCB)
CommandBar_Show(hwndCB, TRUE);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
int wmId, wmEvent;
PAINTSTRUCT ps;
TCHAR szHello[MAX_LOADSTRING];
LPOPENFILENAME lpof;
HANDLE hTestFile;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_OPEN_FILE:
DialogBox( hInst, TEXT("Pathnames"), hWnd,
ChooseFilePathDlgProc );
DialogBox( hInst, TEXT("FILE_PERMISSIONS"), hWnd,
ChooseFilePermsDlgProc );
lpof = DoFileOpen( hWnd, FILE_OPEN );
if(!lpof)
{
break;
}
//Open the file for reading.
hTestFile = CreateFile( (LPTSTR)lpof->lpstrFile,
GENERIC_READ | GENERIC_WRITE ,
FILE_SHARE_READ, NULL, dwFileCreateFlags,
FILE_ATTRIBUTE_NORMAL, NULL);
if( !hTestFile )
{
MessageBox( hWnd, TEXT("Try Different Permissions"),
TEXT("File Open Failed"), MB_OK );
break;
}
if( dwFileCreateFlags == OPEN_EXISTING )
MessageBox( hWnd, lpof->lpstrFile,
TEXT("Existing File Opened"), MB_OK );
{
}
if( dwFileCreateFlags == CREATE_NEW )
{
MessageBox( hWnd, lpof->lpstrFile,
TEXT("New File Created"), MB_OK );
}
CloseHandle(hTestFile );
break;
case IDM_HELP_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd,
(DLGPROC)About);
break;
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
hwndCB = CommandBar_Create(hInst, hWnd, 1);
CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
CommandBar_AddAdornments(hwndCB, 0, 0);
break;
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
DrawText(hdc, szHello, _tcslen(szHello), &rt,
DT_SINGLELINE | DT_VCENTER | DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(hwndCB);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hDlg, &rt1)) {
GetClientRect(GetParent(hDlg), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
BOOL CALLBACK ChooseFilePathDlgProc(HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
TCHAR tszPlatType[12];
int i, rc, iBaseStringID, iMaxStringID;
int iSel, iLen;
short int wCharCnt;
LPCTSTR lpszPathString;
LPTSTR lpszPath;
LPBYTE lpbStrlen;
HWND hWndList;
switch (message)
{
case WM_INITDIALOG:
hWndList = GetDlgItem(hDlg,IDC_PATHNAMES );
//Init paths list based on platform type
memset( &tszPlatType, 0x0, sizeof(tszPlatType));
rc = SystemParametersInfo( SPI_GETPLATFORMTYPE,
sizeof(tszPlatType),
&tszPlatType, 0);
if( (lstrcmp( tszPlatType, TEXT("Jupiter") ) == 0 )
||
( lstrcmp( tszPlatType, TEXT("HPC") ) == 0 ) )
{
iBaseStringID = IDS_HPC_PATH1;
iMaxStringID = IDS_HPC_PATH11;
}
if( lstrcmp( tszPlatType, TEXT("Palm PC") ) == 0 )
{
iBaseStringID = IDS_PPC_PATH1;
iMaxStringID = IDS_PPC_PATH7;
}
for( i = iBaseStringID; i < iMaxStringID; i++ )
{
lpszPathString =
(LPCTSTR)LoadString(hInst, i, NULL, NULL);
lpbStrlen = (LPBYTE)lpszPathString;
lpbStrlen -= 2 * sizeof( BYTE );
wCharCnt = (short int)(*lpbStrlen);
lpszPath = (LPTSTR)LocalAlloc( LPTR, (wCharCnt + 1) * sizeof(TCHAR));
LoadString(hInst, i, lpszPath, (int)wCharCnt + 1);
ListBox_InsertString(hWndList, -1, lpszPath);
LocalFree(lpszPath);
}
return TRUE;
case WM_COMMAND:
if(LOWORD(wParam) == IDOK)
{
//set path from ctrl input
//get the sel item
hWndList = GetDlgItem(hDlg,IDC_PATHNAMES );
iSel = SendMessage(hWndList, LB_GETCURSEL, 0, 0 );
//copy sel item text
memset( szPath, 0x0, sizeof(szPath) );
SendMessage(hWndList,LB_GETTEXT, iSel,
(LPARAM)(LPCTSTR) &szPath );
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
if (LOWORD(wParam) == IDCANCEL)
{
//set path NULL
memset( szPath, 0x0, sizeof(szPath) );
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
//fail and bail
FAIL:
MessageBox( hDlg, TEXT( "Couldn't Allocate Memory"),
TEXT( "Choose Path Failed" ), MB_OK);
return FALSE;
}
BOOL CALLBACK ChooseFilePermsDlgProc(HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
int i, iButtonState;
switch (message)
{
case WM_INITDIALOG:
SendDlgItemMessage( hDlg, IDC_CREATE_NEW,
BM_SETCHECK, BST_CHECKED ,0 );
break;
case WM_COMMAND:
if(LOWORD(wParam) == IDOK)
{
//set open permissions from ctrl input
iButtonState =
SendDlgItemMessage( hDlg, IDC_CREATE_NEW, BM_GETSTATE, 0,0 );
if( iButtonState == BST_CHECKED )
{
dwFileCreateFlags = CREATE_NEW;
}
else
{
dwFileCreateFlags = OPEN_EXISTING;
}
EndDialog(hDlg, LOWORD(wParam));
}
if(LOWORD(wParam) == IDCANCEL)
{
//set open permissions NULL
dwFileCreateFlags = NULL;
EndDialog(hDlg, LOWORD(wParam));
}
return TRUE;
break;
}
return FALSE;
}
//----------------------------------------------------------------------
// Common File Dialog - Open a schema
//----------------------------------------------------------------------
LPOPENFILENAME DoFileOpen(HWND hWnd, int iOpenSaveFlag)
{
TCHAR* pszFileName;
const LPTSTR pszOpenFilter = TEXT ("All Documents (*.*)\0*.*\0\0");
LPOPENFILENAME lpof;
// Allocate space for the OPENFILENAME struct
lpof = (LPOPENFILENAME)LocalAlloc(LPTR,sizeof (OPENFILENAME));
if (!lpof)
{ goto FAIL;}
// Allocate space for the FILENAME string
pszFileName = (TCHAR*)LocalAlloc(LPTR, MAX_PATH);
if (!pszFileName)
{ goto FAIL;}
// Make the first char null, as we don't want the
// dialog to init the filename edit ctl.
// pszFileName[0] = '\0';
// Initialize File Open structure.
lpof->lStructSize = sizeof (OPENFILENAME);
lpof->hwndOwner = hWnd;
lpof->lpstrFile = pszFileName;
lpof->nMaxFile = MAX_PATH;
lpof->lpstrFilter = pszOpenFilter;
lpof->lpstrInitialDir = (LPCWSTR)&szPath[0];
if (iOpenSaveFlag == FILE_SAVE)
{
//prompt for overwrite, send files to sync directory
lpof->Flags = OFN_OVERWRITEPROMPT;
if( !GetSaveFileName (lpof))
{
lpof = NULL;
}
}
else
{
if( !GetOpenFileName (lpof))
{
lpof = NULL;
}
}
//it's all good
return lpof;
//fail and bail
FAIL:
MessageBox( hWnd, TEXT( "Couldn't Allocate Memory"),
TEXT( "OpenFile Failed" ), MB_OK);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -