#include "AWin32CommonModule.h"
#include <stdlib.h>
#include <stdio.h>
#include <Commdlg.h>
#include <shlobj.h>
#include <tchar.h>
#include <io.h>
#include <direct.h>
#include <atlstr.h>
#include <shellapi.h>
#include <string>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "Rpcrt4.lib")
std::string AWin32CommonModule::GetDesktopFolder()
{
char path[255];
SHGetSpecialFolderPath(0, path, CSIDL_DESKTOPDIRECTORY, 0);
std::string rlt = path;
return rlt;
}
std::wstring AWin32CommonModule::AsciiToUnicode(std::string asciiStr)
{
int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, NULL, 0);
if (0 == widesize)
{
return std::wstring();
}
std::vector<wchar_t> resultstring(widesize);
int count = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, &resultstring[0], widesize);
if (0 == count)
{
return std::wstring();
}
return std::wstring(&resultstring[0]);
}
std::wstring AWin32CommonModule::Utf8ToUnicode(const std::string &s)
{
if (s.length() == 0) {
return std::wstring();
}
// compute the length of the buffer we'll need
int charcount = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
if (charcount == 0) {
return std::wstring();
}
// convert
wchar_t* buf = new wchar_t[charcount];
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, charcount);
std::wstring result(buf);
delete[] buf;
return result;
}
std::string AWin32CommonModule::UnicodeToAscii(std::wstring unicodeStr)
{
// compute the length of the buffer we'll need
int charcount = WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1,
NULL, 0, NULL, NULL);
if (charcount == 0) {
return std::string();
}
// convert
char *buf = new char[charcount];
WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, buf, charcount,
NULL, NULL);
std::string result(buf);
delete[] buf;
return result;
}
std::string AWin32CommonModule::UnicodeToUtf8(std::wstring unicodeStr)
{
int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL);
if (0 == utf8size)
{
return std::string();
}
std::vector<char> resultstr(utf8size);
int count = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, &resultstr[0], utf8size, NULL, NULL);
if (0 == count)
{
return std::string();
}
std::string rlt = std::string(&resultstr[0]);
return rlt;
}
std::string AWin32CommonModule::AsciiToUtf8(std::string asic)
{
return UnicodeToUtf8(AsciiToUnicode(asic));
}
std::string AWin32CommonModule::Utf8ToAscii(std::string utf8)
{
return UnicodeToAscii(Utf8ToUnicode(utf8));
}
AWin32CommonModule::AWin32CommonModule()
{
}
AWin32CommonModule::~AWin32CommonModule()
{
}
//获取当前模块名
static std::string _GetCurrentDllFullFilename()
{
char strFilePath[1024];
memset(strFilePath, 0, 1024);
//::GetModuleFileName(NULL, strFilePath, 2048);//第一个参数如果提供NULL,只能得到创建进程的那个EXE文件的名称。
HMODULE hModule = NULL;
::GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCSTR)&_GetCurrentDllFullFilename, //这是函数名,强转
&hModule);
DWORD len = ::GetModuleFileNameA(hModule, strFilePath, 1024);
std::string _strFullPathName = std::string(strFilePath);
return _strFullPathName;
}
//获取现有模块的文件名
std::string AWin32CommonModule::GetCurrentDllFullFilename(){
return _GetCurrentDllFullFilename();
}
//分割全路径
#define Max_Path 512
static void _SplitFullFilename(std::string fullfilename, std::string* _path, std::string* _filenameNoExt, std::string* _ext){
char path_buffer[Max_Path] = { 0 };
char drive[_MAX_DRIVE] = { 0 };
char dir[Max_Path] = { 0 };
char fname[_MAX_FNAME] = { 0 };
char ext[_MAX_EXT] = { 0 };
_splitpath_s(fullfilename.c_str(), drive, dir, fname, ext);
//路径
if (_path){
*_path = drive;
*_path = *_path + dir;
}
//无扩展文件名
if (_filenameNoExt)
{
*_filenameNoExt = fname;
}
//扩展
if (_ext)
{
*_ext = ext;
}
}
void AWin32CommonModule::SplitFullFilename(std::string fullfilename, std::string* _path, std::string* _filenameNoExt, std::string* _ext){
_SplitFullFilename(fullfilename, _path, _filenameNoExt, _ext);
}
void _CenterWindow(HWND hWnd)
{
int scrWidth, scrHeight;
RECT rect;
//获得屏幕尺寸
scrWidth = GetSystemMetrics(SM_CXSCREEN);
scrHeight = GetSystemMetrics(SM_CYSCREEN);
//取得窗口尺寸
GetWindowRect(hWnd, &rect);
//重新设置rect里的值
long width = rect.right - rect.left;
long height = rect.bottom - rect.top;
rect.left = (scrWidth - width) / 2;
rect.top = (scrHeight - height) / 2;
//移动窗口到指定的位置
SetWindowPos(hWnd, HWND_TOP, rect.left, rect.top, width, height, SWP_NOSIZE | SWP_NOZORDER);
}
void AWin32CommonModule::CenterWindow(HWND hWnd){
_CenterWindow(hWnd);
}
//打开文件
static std::string _GetOpenFile(char* filter = NULL, char* title = NULL, char* initDirectory = NULL)
{
//文件名
std::string filename;
//打开文件
OPENFILENAME ofn = { 0 };
TCHAR strFilename[MAX_PATH] = { 0 };//用于接收文件名
ofn.lStructSize = sizeof(OPENFILENAME);//结构体大小
ofn.hwndOwner = GetForegroundWindow();//拥有着窗口句柄
if (filter)
{
ofn.lpstrFilter = filter;//设置过滤
}
else
{
ofn.lpstrFilter = TEXT("所有文件\0*.*\0\0");//设置过滤
}
ofn.nFilterIndex = 1;//过滤器索引
ofn.lpstrFile = strFilename;//接收返回的文件名,注意第一个字符需要为NULL
ofn.nMaxFile = sizeof(strFilename);//缓冲区长度
ofn.lpstrInitialDir = initDirectory;//初始目录为默认
//对话框标题
if (title)
{
ofn.lpstrTitle = title;
}
else
{
ofn.lpstrTitle = TEXT("请选择一个文件");
}
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;//文件、目录必须存在,隐藏只读选项
if (GetOpenFileName(&ofn))
{
filename = strFilename;
}
return filename;
}
//打开文件
std::string AWin32CommonModule::GetOpenFile(char* filter = NULL, char* title = NULL, char* initDirectory = NULL){
return _GetOpenFile(filter, title, initDirectory);
}
//保存文件
static std::string _GetSaveFile(char* filter = NULL, char* title = NULL, char* initDirectory = NULL)
{
//文件名
std::string filename;
//打开文件
OPENFILENAME ofn = { 0 };
TCHAR strFilename[MAX_PATH] = { 0 };//用于接收文件名
ofn.lStructSize = sizeof(OPENFILENAME);//结构体大小
ofn.hwndOwner = GetForegroundWindow();//拥有着窗口句柄
if (filter)
{
ofn.lpstrFilter = filter;//设置过滤
}
else
{
ofn.lpstrFilter = TEXT("所有文件\0*.*\0\0");//设置过滤
}
ofn.nFilterIndex = 1;//过滤器索引
ofn.lpstrFile = strFilename;//接收返回的文件名,注意第一个字符需要为NULL
ofn.nMaxFile = sizeof(strFilename);//缓冲区长度
ofn.lpstrInitialDir = initDirectory;//初始目录为默认
//对话框标题
if (title)
{
ofn.lpstrTitle = title;
}
else
{
ofn.lpstrTitle = TEXT("请选择一个文件");
}
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;//目录必须存在,覆盖文件前发出警告
ofn.lpstrDefExt = TEXT("aep");//默认追加的扩展名
if (GetSaveFileName(&ofn))
{
filename = strFilename;
}
return filename;
}
//保存文件
std::string AWin32CommonModule::GetSaveFile(char* filter = NULL, char* title = NULL, char* initDirectory = NULL){
return _GetSaveFile(filter, title, initDirectory);
}
//选择文件夹窗口回调函数
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
switch (uMsg)
{
case BFFM_INITIALIZED:
{
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)lpData);
}
break;
default:
break;
}
return 0;
}
//选择文件夹
std::string AWin32CommonModule::SelectFolder(const char* initDirectory = NULL){ // 注意路径中不要带'\..\'或'\.\'符号,否则设置默认路径失败;
BROWSEINFO bi = {0};
bi.hwndOwner = GetForegroundWindow(); // 父窗口;
bi.lpszTitle = _T("选择目录");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI /*包含一个编辑框 用户�