mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-05 04:39:08 +08:00
Shifted three functions to common (#1101)
This commit is contained in:
parent
5dc60b9f35
commit
c1232a7001
@ -97,6 +97,8 @@
|
||||
<ClInclude Include="d2d_text.h" />
|
||||
<ClInclude Include="d2d_window.h" />
|
||||
<ClInclude Include="dpi_aware.h" />
|
||||
<ClInclude Include="window_helpers.h" />
|
||||
<ClInclude Include="icon_helpers.h" />
|
||||
<ClInclude Include="hwnd_data_cache.h" />
|
||||
<ClInclude Include="json.h" />
|
||||
<ClInclude Include="monitors.h" />
|
||||
@ -130,10 +132,12 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="settings_helpers.cpp" />
|
||||
<ClCompile Include="settings_objects.cpp" />
|
||||
<ClCompile Include="icon_helpers.cpp" />
|
||||
<ClCompile Include="start_visible.cpp" />
|
||||
<ClCompile Include="tasklist_positions.cpp" />
|
||||
<ClCompile Include="common.cpp" />
|
||||
<ClCompile Include="windows_colors.cpp" />
|
||||
<ClCompile Include="window_helpers.cpp" />
|
||||
<ClCompile Include="winstore.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
|
@ -81,6 +81,12 @@
|
||||
<ClInclude Include="winstore.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="icon_helpers.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="window_helpers.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="d2d_svg.cpp">
|
||||
@ -132,5 +138,11 @@
|
||||
<ClCompile Include="winstore.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="icon_helpers.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="window_helpers.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
90
src/common/icon_helpers.cpp
Normal file
90
src/common/icon_helpers.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
#include "icon_helpers.h"
|
||||
#include "pch.h"
|
||||
|
||||
HRESULT GetIconIndexFromPath(_In_ PCWSTR path, _Out_ int* index)
|
||||
{
|
||||
*index = 0;
|
||||
|
||||
HRESULT hr = E_FAIL;
|
||||
|
||||
SHFILEINFO shFileInfo = { 0 };
|
||||
|
||||
if (!PathIsRelative(path))
|
||||
{
|
||||
DWORD attrib = GetFileAttributes(path);
|
||||
HIMAGELIST himl = (HIMAGELIST)SHGetFileInfo(path, attrib, &shFileInfo, sizeof(shFileInfo), (SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES));
|
||||
if (himl)
|
||||
{
|
||||
*index = shFileInfo.iIcon;
|
||||
// We shouldn't free the HIMAGELIST.
|
||||
hr = S_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return hr;
|
||||
}
|
||||
|
||||
HBITMAP CreateBitmapFromIcon(_In_ HICON hIcon, _In_opt_ UINT width, _In_opt_ UINT height)
|
||||
{
|
||||
HBITMAP hBitmapResult = NULL;
|
||||
|
||||
// Create compatible DC
|
||||
HDC hDC = CreateCompatibleDC(NULL);
|
||||
if (hDC != NULL)
|
||||
{
|
||||
// Get bitmap rectangle size
|
||||
RECT rc = { 0 };
|
||||
rc.left = 0;
|
||||
rc.right = (width != 0) ? width : GetSystemMetrics(SM_CXSMICON);
|
||||
rc.top = 0;
|
||||
rc.bottom = (height != 0) ? height : GetSystemMetrics(SM_CYSMICON);
|
||||
|
||||
// Create bitmap compatible with DC
|
||||
BITMAPINFO BitmapInfo;
|
||||
ZeroMemory(&BitmapInfo, sizeof(BITMAPINFO));
|
||||
|
||||
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
BitmapInfo.bmiHeader.biWidth = rc.right;
|
||||
BitmapInfo.bmiHeader.biHeight = rc.bottom;
|
||||
BitmapInfo.bmiHeader.biPlanes = 1;
|
||||
BitmapInfo.bmiHeader.biBitCount = 32;
|
||||
BitmapInfo.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
HDC hDCBitmap = GetDC(NULL);
|
||||
|
||||
HBITMAP hBitmap = CreateDIBSection(hDCBitmap, &BitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0);
|
||||
|
||||
ReleaseDC(NULL, hDCBitmap);
|
||||
|
||||
if (hBitmap != NULL)
|
||||
{
|
||||
// Select bitmap into DC
|
||||
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDC, hBitmap);
|
||||
if (hBitmapOld != NULL)
|
||||
{
|
||||
// Draw icon into DC
|
||||
if (DrawIconEx(hDC, 0, 0, hIcon, rc.right, rc.bottom, 0, NULL, DI_NORMAL))
|
||||
{
|
||||
// Restore original bitmap in DC
|
||||
hBitmapResult = (HBITMAP)SelectObject(hDC, hBitmapOld);
|
||||
hBitmapOld = NULL;
|
||||
hBitmap = NULL;
|
||||
}
|
||||
|
||||
if (hBitmapOld != NULL)
|
||||
{
|
||||
SelectObject(hDC, hBitmapOld);
|
||||
}
|
||||
}
|
||||
|
||||
if (hBitmap != NULL)
|
||||
{
|
||||
DeleteObject(hBitmap);
|
||||
}
|
||||
}
|
||||
|
||||
DeleteDC(hDC);
|
||||
}
|
||||
|
||||
return hBitmapResult;
|
||||
}
|
5
src/common/icon_helpers.h
Normal file
5
src/common/icon_helpers.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
|
||||
HRESULT GetIconIndexFromPath(_In_ PCWSTR path, _Out_ int* index);
|
||||
HBITMAP CreateBitmapFromIcon(_In_ HICON hIcon, _In_opt_ UINT width = 0, _In_opt_ UINT height = 0);
|
30
src/common/window_helpers.cpp
Normal file
30
src/common/window_helpers.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include "window_helpers.h"
|
||||
#include "pch.h"
|
||||
|
||||
HWND CreateMsgWindow(_In_ HINSTANCE hInst, _In_ WNDPROC pfnWndProc, _In_ void* p)
|
||||
{
|
||||
WNDCLASS wc = { 0 };
|
||||
|
||||
PCWSTR wndClassName = L"MsgWindow";
|
||||
|
||||
wc.lpfnWndProc = DefWindowProc;
|
||||
wc.cbWndExtra = sizeof(void*);
|
||||
wc.hInstance = hInst;
|
||||
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
|
||||
wc.lpszClassName = wndClassName;
|
||||
|
||||
RegisterClass(&wc);
|
||||
|
||||
HWND hwnd = CreateWindowEx(
|
||||
0, wndClassName, nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hInst, nullptr);
|
||||
if (hwnd)
|
||||
{
|
||||
SetWindowLongPtr(hwnd, 0, (LONG_PTR)p);
|
||||
if (pfnWndProc)
|
||||
{
|
||||
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)pfnWndProc);
|
||||
}
|
||||
}
|
||||
|
||||
return hwnd;
|
||||
}
|
4
src/common/window_helpers.h
Normal file
4
src/common/window_helpers.h
Normal file
@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include "common.h"
|
||||
|
||||
HWND CreateMsgWindow(_In_ HINSTANCE hInst, _In_ WNDPROC pfnWndProc, _In_ void* p);
|
@ -5,6 +5,7 @@
|
||||
#include <PowerRenameManager.h>
|
||||
#include <trace.h>
|
||||
#include <Helpers.h>
|
||||
#include <icon_helpers.h>
|
||||
#include <Settings.h>
|
||||
#include "resource.h"
|
||||
|
||||
|
@ -2,94 +2,6 @@
|
||||
#include "Helpers.h"
|
||||
#include <ShlGuid.h>
|
||||
|
||||
HRESULT GetIconIndexFromPath(_In_ PCWSTR path, _Out_ int* index)
|
||||
{
|
||||
*index = 0;
|
||||
|
||||
HRESULT hr = E_FAIL;
|
||||
|
||||
SHFILEINFO shFileInfo = { 0 };
|
||||
|
||||
if (!PathIsRelative(path))
|
||||
{
|
||||
DWORD attrib = GetFileAttributes(path);
|
||||
HIMAGELIST himl = (HIMAGELIST)SHGetFileInfo(path, attrib, &shFileInfo, sizeof(shFileInfo), (SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES));
|
||||
if (himl)
|
||||
{
|
||||
*index = shFileInfo.iIcon;
|
||||
// We shouldn't free the HIMAGELIST.
|
||||
hr = S_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return hr;
|
||||
}
|
||||
|
||||
HBITMAP CreateBitmapFromIcon(_In_ HICON hIcon, _In_opt_ UINT width, _In_opt_ UINT height)
|
||||
{
|
||||
HBITMAP hBitmapResult = NULL;
|
||||
|
||||
// Create compatible DC
|
||||
HDC hDC = CreateCompatibleDC(NULL);
|
||||
if (hDC != NULL)
|
||||
{
|
||||
// Get bitmap rectangle size
|
||||
RECT rc = { 0 };
|
||||
rc.left = 0;
|
||||
rc.right = (width != 0) ? width : GetSystemMetrics(SM_CXSMICON);
|
||||
rc.top = 0;
|
||||
rc.bottom = (height != 0) ? height : GetSystemMetrics(SM_CYSMICON);
|
||||
|
||||
// Create bitmap compatible with DC
|
||||
BITMAPINFO BitmapInfo;
|
||||
ZeroMemory(&BitmapInfo, sizeof(BITMAPINFO));
|
||||
|
||||
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
BitmapInfo.bmiHeader.biWidth = rc.right;
|
||||
BitmapInfo.bmiHeader.biHeight = rc.bottom;
|
||||
BitmapInfo.bmiHeader.biPlanes = 1;
|
||||
BitmapInfo.bmiHeader.biBitCount = 32;
|
||||
BitmapInfo.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
HDC hDCBitmap = GetDC(NULL);
|
||||
|
||||
HBITMAP hBitmap = CreateDIBSection(hDCBitmap, &BitmapInfo, DIB_RGB_COLORS, NULL, NULL, 0);
|
||||
|
||||
ReleaseDC(NULL, hDCBitmap);
|
||||
|
||||
if (hBitmap != NULL)
|
||||
{
|
||||
// Select bitmap into DC
|
||||
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDC, hBitmap);
|
||||
if (hBitmapOld != NULL)
|
||||
{
|
||||
// Draw icon into DC
|
||||
if (DrawIconEx(hDC, 0, 0, hIcon, rc.right, rc.bottom, 0, NULL, DI_NORMAL))
|
||||
{
|
||||
// Restore original bitmap in DC
|
||||
hBitmapResult = (HBITMAP)SelectObject(hDC, hBitmapOld);
|
||||
hBitmapOld = NULL;
|
||||
hBitmap = NULL;
|
||||
}
|
||||
|
||||
if (hBitmapOld != NULL)
|
||||
{
|
||||
SelectObject(hDC, hBitmapOld);
|
||||
}
|
||||
}
|
||||
|
||||
if (hBitmap != NULL)
|
||||
{
|
||||
DeleteObject(hBitmap);
|
||||
}
|
||||
}
|
||||
|
||||
DeleteDC(hDC);
|
||||
}
|
||||
|
||||
return hBitmapResult;
|
||||
}
|
||||
|
||||
HRESULT _ParseEnumItems(_In_ IEnumShellItems* pesi, _In_ IPowerRenameManager* psrm, _In_ int depth = 0)
|
||||
{
|
||||
HRESULT hr = E_INVALIDARG;
|
||||
@ -167,33 +79,6 @@ HRESULT EnumerateDataObject(_In_ IUnknown* dataSource, _In_ IPowerRenameManager*
|
||||
return hr;
|
||||
}
|
||||
|
||||
HWND CreateMsgWindow(_In_ HINSTANCE hInst, _In_ WNDPROC pfnWndProc, _In_ void* p)
|
||||
{
|
||||
WNDCLASS wc = { 0 };
|
||||
PWSTR wndClassName = L"MsgWindow";
|
||||
|
||||
wc.lpfnWndProc = DefWindowProc;
|
||||
wc.cbWndExtra = sizeof(void*);
|
||||
wc.hInstance = hInst;
|
||||
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
|
||||
wc.lpszClassName = wndClassName;
|
||||
|
||||
RegisterClass(&wc);
|
||||
|
||||
HWND hwnd = CreateWindowEx(
|
||||
0, wndClassName, nullptr, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hInst, nullptr);
|
||||
if (hwnd)
|
||||
{
|
||||
SetWindowLongPtr(hwnd, 0, (LONG_PTR)p);
|
||||
if (pfnWndProc)
|
||||
{
|
||||
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)pfnWndProc);
|
||||
}
|
||||
}
|
||||
|
||||
return hwnd;
|
||||
}
|
||||
|
||||
BOOL GetEnumeratedFileName(__out_ecount(cchMax) PWSTR pszUniqueName, UINT cchMax, __in PCWSTR pszTemplate, __in_opt PCWSTR pszDir, unsigned long ulMinLong, __inout unsigned long* pulNumUsed)
|
||||
{
|
||||
PWSTR pszName = nullptr;
|
||||
|
@ -4,9 +4,6 @@
|
||||
#include <lib/PowerRenameInterfaces.h>
|
||||
|
||||
HRESULT EnumerateDataObject(_In_ IUnknown* pdo, _In_ IPowerRenameManager* psrm);
|
||||
HRESULT GetIconIndexFromPath(_In_ PCWSTR path, _Out_ int* index);
|
||||
HBITMAP CreateBitmapFromIcon(_In_ HICON hIcon, _In_opt_ UINT width = 0, _In_opt_ UINT height = 0);
|
||||
HWND CreateMsgWindow(_In_ HINSTANCE hInst, _In_ WNDPROC pfnWndProc, _In_ void* p);
|
||||
BOOL GetEnumeratedFileName(
|
||||
__out_ecount(cchMax) PWSTR pszUniqueName,
|
||||
UINT cchMax,
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include "stdafx.h"
|
||||
#include "PowerRenameItem.h"
|
||||
#include "helpers.h"
|
||||
#include "icon_helpers.h"
|
||||
|
||||
int CPowerRenameItem::s_id = 0;
|
||||
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <algorithm>
|
||||
#include <shlobj.h>
|
||||
#include "helpers.h"
|
||||
#include "window_helpers.h"
|
||||
#include <filesystem>
|
||||
#include "trace.h"
|
||||
|
||||
|
@ -185,6 +185,11 @@
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="PowerRenameTest.rc" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\common\common.vcxproj">
|
||||
<Project>{74485049-c722-400f-abe5-86ac52d929b3}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
@ -192,6 +192,11 @@
|
||||
<ClCompile Include="PowerRenameRegExTests.cpp" />
|
||||
<ClCompile Include="TestFileHelper.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\common\common.vcxproj">
|
||||
<Project>{74485049-c722-400f-abe5-86ac52d929b3}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
Loading…
Reference in New Issue
Block a user