Windows下Win32 SDK控制窗口居中显示及窗口文件拖放

xingyun86 2020-8-15 1072

Windows下Win32 SDK控制窗口居中显示及窗口文件拖放

#include <shellapi.h>
#include <string>
#include <unordered_map>
#if !defined(_UNICODE) && !defined(UNICODE)
#define TSTRING std::string
#else
#define TSTRING std::wstring
#endif
__inline static 
void RegisterDropFilesEvent(HWND hWnd)
{
#ifndef WM_COPYGLOBALDATA
#define WM_COPYGLOBALDAYA	0x0049
#endif
#ifndef MSGFLT_ADD
#define MSGFLT_ADD 1
#endif
#ifndef MSGFLT_REMOVE
#define MSGFLT_REMOVE 2
#endif
	SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_ACCEPTFILES);
	typedef BOOL(WINAPI* LPFN_ChangeWindowMessageFilter)(__in UINT message, __in DWORD dwFlag);
	LPFN_ChangeWindowMessageFilter pfnChangeWindowMessageFilter = (LPFN_ChangeWindowMessageFilter)GetProcAddress(GetModuleHandle(TEXT("USER32.DLL")), ("ChangeWindowMessageFilter"));
	if (pfnChangeWindowMessageFilter)
	{
		pfnChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
		pfnChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
		pfnChangeWindowMessageFilter(WM_COPYGLOBALDAYA, MSGFLT_ADD);// 0x0049 == WM_COPYGLOBALDATA
	}
}
__inline static
size_t MyGetDropFiles(std::unordered_map<std::string, std::string>& mapss, HDROP hDropInfo)
{
	UINT nIndex = 0;
	UINT nNumOfFiles = 0;
	CHAR tszFilePathName[MAX_PATH + 1] = { 0 };
	//得到文件个数
	nNumOfFiles = ::DragQueryFileA(hDropInfo, 0xFFFFFFFF, NULL, 0);
	for (nIndex = 0; nIndex < nNumOfFiles; nIndex++)
	{
		//得到文件名
		::DragQueryFileA(hDropInfo, nIndex, (LPSTR)tszFilePathName, _MAX_PATH);
		mapss.emplace(tszFilePathName, tszFilePathName);
	}
	::DragFinish(hDropInfo);
	return nNumOfFiles;
}
__inline static
size_t MyGetDropFiles(std::unordered_map<std::wstring, std::wstring>& mapwsws, HDROP hDropInfo)
{
	UINT nIndex = 0;
	UINT nNumOfFiles = 0;
	WCHAR tszFilePathName[MAX_PATH + 1] = { 0 };
	//得到文件个数
	nNumOfFiles = ::DragQueryFileW(hDropInfo, 0xFFFFFFFF, NULL, 0);
	for (nIndex = 0; nIndex < nNumOfFiles; nIndex++)
	{
		//得到文件名
		::DragQueryFileW(hDropInfo, nIndex, (LPTSTR)tszFilePathName, _MAX_PATH);
		mapwsws.emplace(tszFilePathName, tszFilePathName);
	}
	::DragFinish(hDropInfo);
	return nNumOfFiles;
}
__inline static
size_t MyGetDropFiles(std::vector<std::string>& vs, HDROP hDropInfo)
{
	UINT nIndex = 0;
	UINT nNumOfFiles = 0;
	CHAR tszFilePathName[MAX_PATH + 1] = { 0 };
	//得到文件个数
	nNumOfFiles = ::DragQueryFileA(hDropInfo, 0xFFFFFFFF, NULL, 0);
	for (nIndex = 0; nIndex < nNumOfFiles; nIndex++)
	{
		//得到文件名
		::DragQueryFileA(hDropInfo, nIndex, (LPSTR)tszFilePathName, _MAX_PATH);
		vs.emplace_back(tszFilePathName);
	}
	::DragFinish(hDropInfo);
	return nNumOfFiles;
}
__inline static
size_t MyGetDropFiles(std::vector<std::wstring>& vws, HDROP hDropInfo)
{
	UINT nIndex = 0;
	UINT nNumOfFiles = 0;
	WCHAR tszFilePathName[MAX_PATH + 1] = { 0 };
	//得到文件个数
	nNumOfFiles = ::DragQueryFileW(hDropInfo, 0xFFFFFFFF, NULL, 0);
	for (nIndex = 0; nIndex < nNumOfFiles; nIndex++)
	{
		//得到文件名
		::DragQueryFileW(hDropInfo, nIndex, (LPWSTR)tszFilePathName, _MAX_PATH);
		vws.emplace_back(tszFilePathName);
	}
	::DragFinish(hDropInfo);
	return nNumOfFiles;
}
//显示在屏幕中央
__inline static 
void CenterWindowInScreen(HWND hWnd)
{
	RECT rcWindow = { 0 };
	RECT rcScreen = { 0 };
	SIZE szAppWnd = { 0, 0 };
	POINT ptAppWnd = { 0, 0 };
	// Get workarea rect.
	BOOL fResult = SystemParametersInfo(SPI_GETWORKAREA,   // Get workarea information
		0,              // Not used
		&rcScreen,    // Screen rect information
		0);             // Not used
	GetWindowRect(hWnd, &rcWindow);
	szAppWnd.cx = rcWindow.right - rcWindow.left;
	szAppWnd.cy = rcWindow.bottom - rcWindow.top;
	//居中显示
	ptAppWnd.x = (rcScreen.right - rcScreen.left - szAppWnd.cx) / 2;
	ptAppWnd.y = (rcScreen.bottom - rcScreen.top - szAppWnd.cy) / 2;
	MoveWindow(hWnd, ptAppWnd.x, ptAppWnd.y, szAppWnd.cx, szAppWnd.cy, TRUE);
}
//显示在父窗口中央
__inline static 
void CenterWindowInParent(HWND hWnd, HWND hParentWnd)
{
	RECT rcWindow = { 0 };
	RECT rcScreen = { 0 };
	RECT rcParent = { 0 };
	SIZE szAppWnd = { 0, 0 };
	POINT ptAppWnd = { 0, 0 };
	// Get workarea rect.
	BOOL fResult = SystemParametersInfo(SPI_GETWORKAREA,   // Get workarea information
		0,              // Not used
		&rcScreen,    // Screen rect information
		0);             // Not used
	GetWindowRect(hParentWnd, &rcParent);
	GetWindowRect(hWnd, &rcWindow);
	szAppWnd.cx = rcWindow.right - rcWindow.left;
	szAppWnd.cy = rcWindow.bottom - rcWindow.top;
	//居中显示
	if ((rcWindow.right - rcWindow.left) > (rcParent.right - rcParent.left))
	{
		ptAppWnd.x = (rcScreen.right - rcScreen.left - szAppWnd.cx) / 2;
	}
	else
	{
		ptAppWnd.x = (rcParent.right + rcParent.left - szAppWnd.cx) / 2;
	}
	if ((rcWindow.bottom - rcWindow.top) > (rcParent.bottom - rcParent.top))
	{
		ptAppWnd.y = (rcScreen.bottom - rcScreen.top - szAppWnd.cy) / 2;
	}
	else
	{
		ptAppWnd.y = (rcParent.bottom + rcParent.top - szAppWnd.cy) / 2;
	}
	MoveWindow(hWnd, ptAppWnd.x, ptAppWnd.y, szAppWnd.cx, szAppWnd.cy, TRUE);
}
__inline static
LPCWSTR AppDir() {
    static WCHAR GWAppDir[4096] = L"\0";
    if ((*GWAppDir) == L'\0')
    {
        for (DWORD dwLen = GetModuleFileNameW(NULL, GWAppDir, MAX_PATH_LEN) - 1;
            (dwLen >= 0) && (GWAppDir[dwLen] != L'\\');
            GWAppDir[dwLen] = L'\0', dwLen--);
    }
    return GWAppDir;
}


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回