Полезные, самые часто используемые функции открытия диалоговых окон открытия файла, сохранения файла, выбора цвета.
Прототипы:
bool OpenFileDialogA(std::string& sOutFileName, const char* szFilter);
bool OpenFileDialogW(std::wstring& sOutFileName, const WCHAR* szFilter);
bool SaveFileDialogA(std::string& sOutFileName, const char* szFilter ) ;
bool SaveFileDialogW( std::wstring& sOutFileName, const WCHAR* szFilter);
bool SelectColorDialog( float& r, float& g, float& b ) ;
Реализация:
#ifndef WIN32
#error only win32
#endif
#ifdef WIN32
#ifndef _WINDOWS_
#error windows.h not included
#endif
#endif
#pragma warning(push)
#pragma warning(disable:4996)
#if ( defined(WIN32) && defined(_WINDOWS_) )
#include <commdlg.h>
//======================================================
bool OpenFileDialogA(std::string& sOutFileName,
const char* szFilter )
{
sOutFileName = "";
OPENFILENAMEA ofn;
char fileName[MAX_PATH+1] = "";
fileName[0]=0;
strcpy( fileName , sOutFileName.c_str() );
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAMEA);
ofn.hwndOwner = 0;
// ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.lpstrFilter = szFilter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "*.*";
if ( GetOpenFileNameA(&ofn) )
{
sOutFileName = fileName;
return true;
}
else
{
return false;
}
return true;
}
bool OpenFileDialogW( std::wstring& sOutFileName, const WCHAR* szFilter)
{
sOutFileName = L"";
OPENFILENAMEW ofn;
wchar_t fileName[MAX_PATH+1] = L"";
fileName[0]=0;
wcscpy( fileName , sOutFileName.c_str() );
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAMEA);
ofn.hwndOwner = 0;
// ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.lpstrFilter = szFilter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = L"*.*";
if ( GetOpenFileNameW(&ofn) )
{
sOutFileName = fileName;
return true;
}
else
{
return false;
}
return true;
}
//=====================================================
bool SaveFileDialogA(std::string& sOutFileName, const char* szFilter )
{
sOutFileName = "";
OPENFILENAMEA ofn;
char fileName[MAX_PATH+1] = "";
fileName[0]=0;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAMEA);
ofn.hwndOwner = 0;
// if(szFilter)
ofn.lpstrFilter = szFilter;
// else ofn.lpstrFilter = "*.*";
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "";
if ( GetSaveFileNameA(&ofn) )
{
sOutFileName = fileName;
return true;
}
return false;
}
//=====================================================
bool SaveFileDialogW( std::wstring& sOutFileName, const WCHAR* szFilter)
{
sOutFileName = L"";
OPENFILENAMEW ofn;
WCHAR fileName[MAX_PATH+1] = L"";
fileName[0]=0;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAMEA);
ofn.hwndOwner = 0;
// if(szFilter)
ofn.lpstrFilter = szFilter;
// else ofn.lpstrFilter = "*.*";
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = L"";
if ( GetSaveFileNameW(&ofn) )
{
sOutFileName = fileName;
return true;
}
return false;
}
//=======================================================
static bool __DialogSelectColor( DWORD* pdwOutColor )
{
CHOOSECOLORA cc; // common dialog box structure
static COLORREF acrCustClr[16]; // array of custom colors
//HBRUSH hbrush; // brush handle
static DWORD rgbCurrent; // initial color selection
// Initialize CHOOSECOLOR
ZeroMemory(&cc, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.hwndOwner = 0;
cc.lpCustColors = (LPDWORD) acrCustClr;
cc.rgbResult = rgbCurrent;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
cc.rgbResult = *pdwOutColor;
if (ChooseColorA(&cc)==TRUE)
{
//hbrush = CreateSolidBrush(cc.rgbResult);
rgbCurrent = cc.rgbResult;
*pdwOutColor = cc.rgbResult;
return true;
}
else
{
return false;
}
return false;
};
//==================================================
bool SelectColorDialog( float& r, float& g, float& b )
{
DWORD dwRes =0;
dwRes |= (DWORD) ( ( (DWORD)r * 255) );
dwRes |= (DWORD) ( ( (DWORD)b * 255) << 8 );
dwRes |= (DWORD) ( ( (DWORD)g * 255) << 16 );
if(__DialogSelectColor(&dwRes) )
{
r = (float)( (( (dwRes ) | 0xffffff00 ) ^ 0xffffff00 )/255.0f );
g = (float)( (( (dwRes >> 8 ) | 0xffffff00 ) ^ 0xffffff00 )/255.0f );
b = (float)( (( (dwRes >> 16 ) | 0xffffff00 ) ^ 0xffffff00 )/255.0f );
return true;
}
else
{
return false;
}
return true;
};
#endif // WIN32
#pragma warning( pop )
|