头文件:
#ifndef _WIN32FUNC_H__
#define _WIN32FUNC_H__
class CWin32Func
{
public:
static BOOL CenterWindow (HWND hwndChild, HWND hwndParent);
static long GetModuleDir(LPSTR lpBuff);
static int SelectFolder(HWND hParent, char* pFolder);
static BOOL OpenFileDlg(LPSTR lpFile,long lFileLen = MAX_PATH,LPSTR lpFilter = NULL,HWND hMain = NULL);
static BOOL SaveFileDlg(LPSTR lpFile,long lFileLen = MAX_PATH,LPSTR lpFilter = NULL,HWND hMain = NULL);
};
#endif
实现文件:
#include <Windows.h>
#include<shlobj.h>
#include "win32Func.h"
#pragma comment(lib,"shell32.lib")
//
// FUNCTION: CenterWindow(HWND, HWND)
//
// PURPOSE: Centers one window over another.
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
// This functionwill center one window over another ensuring that
// the placement of the window is within the 'working area', meaning
// that it is both within the display limits of the screen, and not
// obscured by the tray or other framing elements of the desktop.
BOOL CWin32Func::CenterWindow (HWND hwndChild, HWND hwndParent)
{
RECT rChild, rParent, rWorkArea;
int wChild, hChild, wParent, hParent;
int xNew, yNew;
BOOL bResult;
// Get the Height and Width of the child window
GetWindowRect (hwndChild, &rChild);
wChild = rChild.right - rChild.left;
hChild = rChild.bottom - rChild.top;
// Get the Height and Width of the parent window
GetWindowRect (hwndParent, &rParent);
wParent = rParent.right - rParent.left;
hParent = rParent.bottom - rParent.top;
// Get the limits of the 'workarea'
bResult = SystemParametersInfo(
SPI_GETWORKAREA, // system parameter to query or set
sizeof(RECT),
&rWorkArea,
0);
if (!bResult) {
rWorkArea.left = rWorkArea.top = 0;
rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
}
// Calculate new X position, then adjust for workarea
xNew = rParent.left + ((wParent - wChild) /2);
if (xNew < rWorkArea.left) {
xNew = rWorkArea.left;
} else if ((xNew+wChild) > rWorkArea.right) {
xNew = rWorkArea.right - wChild;
}
// Calculate new Y position, then adjust for workarea
yNew = rParent.top + ((hParent - hChild) /2);
if (yNew < rWorkArea.top) {
yNew = rWorkArea.top;
} else if ((yNew+hChild) > rWorkArea.bottom) {
yNew = rWorkArea.bottom - hChild;
}
// Set it, and return
return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
long CWin32Func::GetModuleDir(LPSTR lpBuff)
{
char szPath[MAX_PATH] = {0};
if(lpBuff == NULL)
return 0;
GetModuleFileName(NULL,szPath,sizeof(szPath));
char* pPos = NULL;
pPos = strrchr(szPath,'//');
pPos++;
*pPos = '/0';
lstrcpy(lpBuff,szPath);
return lstrlen(lpBuff);
}
BOOL CWin32Func::OpenFileDlg(LPSTR lpFile,long lFileLen ,LPSTR lpFilter ,HWND hMain )
{
OPENFILENAME ofn; // common dialog box structure
//char szFile[260]; // buffer for file name
// owner window
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hMain;
ofn.lpstrFile = lpFile;
//
// Set lpstrFile[0] to '/0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
ofn.lpstrFile[0] = '/0';
ofn.nMaxFile = lFileLen;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if(lpFilter == NULL)
ofn.lpstrFilter = "All/0*.*/0";
else
ofn.lpstrFilter = lpFilter;
// Display the Open dialog box.
if (GetOpenFileName(&ofn)==TRUE)
return TRUE;
else
return FALSE;
}
BOOL CWin32Func::SaveFileDlg(LPSTR lpFile,long lFileLen ,LPSTR lpFilter ,HWND hMain )
{
OPENFILENAME ofn; // common dialog box structure
//char szFile[260]; // buffer for file name
// owner window
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hMain;
ofn.lpstrFile = lpFile;
//
// Set lpstrFile[0] to '/0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
//
ofn.lpstrFile[0] = '/0';
ofn.nMaxFile = lFileLen;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if(lpFilter == NULL)
ofn.lpstrFilter = "All/0*.*/0";
else
ofn.lpstrFilter = lpFilter;
// Display the Open dialog box.
if (GetSaveFileName(&ofn)==TRUE)
return TRUE;
else
return FALSE;
}
int CWin32Func::SelectFolder(HWND hParent, char* pFolder)
{
char szDisplayName[_MAX_PATH];
char szBuffer[_MAX_PATH];
LPMALLOC lpMalloc;
if (::SHGetMalloc(&lpMalloc) != NOERROR)
return 0;
BROWSEINFO browseInfo;
browseInfo.hwndOwner = hParent;
browseInfo.pidlRoot = NULL; // set root at Desktop
browseInfo.pszDisplayName = szDisplayName;
browseInfo.lpszTitle = "文件夹选择";
browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
browseInfo.lpfn = NULL;
browseInfo.lParam = 0;
LPITEMIDLIST lpItemIDList;
if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
{
// Get the path of the selected folder from the item ID list.
if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
{
// At this point, szBuffer contains the path the user chose.
if (szBuffer[0] == '/0')
return 0;
// We have a path in szBuffer! Return it.
lstrcpy(pFolder,szBuffer) ;
if(pFolder[lstrlen(pFolder)-1] != '//')
lstrcat(pFolder,"//");
return 1;
}
else return 1; // strResult is empty
lpMalloc->Free(lpItemIDList);
lpMalloc->Release();
}
return 1;
}