细节优化

This commit is contained in:
Xu Liu 2021-03-17 21:18:39 +08:00
commit 43535211e4
5 changed files with 103 additions and 42 deletions

View file

@ -41,7 +41,7 @@ namespace Magpie.CursorHook {
// 原光标到透明光标的映射
private readonly Dictionary<IntPtr, IntPtr> _hCursorToTptCursor = new Dictionary<IntPtr, IntPtr>();
private static readonly string HOST_WINDOW_CLASS_NAME = "Window_Magpie_967EB565-6F73-4E94-AE53-00CC42592A22";
private const string HOST_WINDOW_CLASS_NAME = "Window_Magpie_967EB565-6F73-4E94-AE53-00CC42592A22";
// 运行时注入的入口
@ -71,13 +71,17 @@ namespace Magpie.CursorHook {
#endif
}
#if DEBUG
// DEBUG 时连接 IPC server
private void ConnectToServer(string channelName) {
_server = EasyHook.RemoteHooking.IpcConnectClient<ServerInterface>(channelName);
// 测试连接性,如果失败会抛出异常静默的失败因此 Run 方法不会执行
_server.Ping();
}
#endif
private void ReportMessageQueue() {
#if DEBUG

View file

@ -104,8 +104,8 @@ namespace Magpie.CursorHook {
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern uint RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)]string lpString);
private readonly static int SM_CXCURSOR = 13;
private readonly static int SM_CYCURSOR = 14;
private const int SM_CXCURSOR = 13;
private const int SM_CYCURSOR = 14;
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);

View file

@ -81,7 +81,7 @@ namespace Magpie {
this.cbbScaleMode.DropDownWidth = 196;
this.cbbScaleMode.ItemHeight = 15;
this.cbbScaleMode.Items.AddRange(new object[] {
"通用(jinc2+锐化",
"通用(Lanczos",
"动漫Anime4K"});
this.cbbScaleMode.Location = new System.Drawing.Point(79, 27);
this.cbbScaleMode.Name = "cbbScaleMode";
@ -167,7 +167,7 @@ namespace Magpie {
this.groupBox1.Controls.Add(this.ckbNoVSync);
this.groupBox1.Location = new System.Drawing.Point(12, 162);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(302, 121);
this.groupBox1.Size = new System.Drawing.Size(292, 121);
this.groupBox1.TabIndex = 9;
this.groupBox1.TabStop = false;
//
@ -206,7 +206,7 @@ namespace Magpie {
this.groupBox3.Controls.Add(this.label3);
this.groupBox3.Location = new System.Drawing.Point(12, 66);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(302, 90);
this.groupBox3.Size = new System.Drawing.Size(292, 90);
this.groupBox3.TabIndex = 11;
this.groupBox3.TabStop = false;
//
@ -236,7 +236,7 @@ namespace Magpie {
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(328, 358);
this.ClientSize = new System.Drawing.Size(318, 358);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.textBox1);

View file

@ -16,7 +16,7 @@ namespace Magpie {
public partial class MainForm : Form {
public static readonly int WM_SHOWME = NativeMethods.RegisterWindowMessage("WM_SHOWME");
private static readonly string AnimeEffectJson = @"[
private const string AnimeEffectJson = @"[
{
""effect"": ""scale"",
""type"": ""Anime4KxDenoise""
@ -33,19 +33,22 @@ namespace Magpie {
""curveHeight"": 0.2
}
]";
private static readonly string CommonEffectJson = @"[
private const string CommonEffectJson = @"[
{
""effect"": ""scale"",
""type"": ""jinc2"",
""scale"": [0,0],
""windowSinc"": 0.35,
""sinc"": 0.825,
""ARStrength"": 0.7
""type"": ""lanczos6"",
""scale"": [0,0]
},
{
""effect"": ""sharpen"",
""type"": ""adaptive"",
""curveHeight"": 0.3
""curveHeight"": 0.6
},
{
""effect"": ""sharpen"",
""type"": ""builtIn"",
""sharpness"": 0.5,
""threshold"": 0.5
}
]";
@ -125,6 +128,13 @@ namespace Magpie {
}
private void HookCursorAtRuntime() {
IntPtr hwndSrc = NativeMethods.GetSrcWnd();
int pid = NativeMethods.GetWindowProcessId(hwndSrc);
if (pid == Process.GetCurrentProcess().Id) {
// 不能 hook 本进程
return;
}
#if DEBUG
string channelName = null;
// DEBUG 时创建 IPC server
@ -138,13 +148,6 @@ namespace Magpie {
);
// 使用 EasyHook 注入
IntPtr hwndSrc = NativeMethods.GetSrcWnd();
int pid = NativeMethods.GetWindowProcessId(hwndSrc);
if(pid == Process.GetCurrentProcess().Id) {
// 不能 hook 本进程
return;
}
try {
EasyHook.RemoteHooking.Inject(
pid, // 要注入的进程的 ID

View file

@ -27,24 +27,44 @@ private:
ComPtr<IWICBitmapSource> _GetFrameWithNoBitblt() {
SIZE srcSize = Utils::GetSize(_srcRect);
RECT windowRect{};
GetWindowRect(_hwndSrc, &windowRect);
Debug::ThrowIfWin32Failed(
GetWindowRect(_hwndSrc, &windowRect),
L"GetWindowRectʧ°Ü"
);
HDC hdcSrc = GetDC(_hwndSrc);
HDC hdcSrc = GetWindowDC(_hwndSrc);
Debug::ThrowIfWin32Failed(
hdcSrc,
L"GetDCʧ°Ü"
);
// 直接获取要放大窗口的 DC 关联的 HBITMAP而不是自己创建一个
HBITMAP hBmpDest = (HBITMAP)GetCurrentObject(hdcSrc, OBJ_BITMAP);
ReleaseDC(_hwndSrc, hdcSrc);
Debug::ThrowIfWin32Failed(
hBmpDest,
L"GetCurrentObjectʧ°Ü"
);
Debug::ThrowIfWin32Failed(
ReleaseDC(_hwndSrc, hdcSrc),
L"ReleaseDCʧ°Ü"
);
ComPtr<IWICBitmap> wicBmp = nullptr;
_wicImgFactory->CreateBitmapFromHBITMAP(
hBmpDest,
NULL,
WICBitmapAlphaChannelOption::WICBitmapIgnoreAlpha,
&wicBmp
Debug::ThrowIfComFailed(
_wicImgFactory->CreateBitmapFromHBITMAP(
hBmpDest,
NULL,
WICBitmapAlphaChannelOption::WICBitmapIgnoreAlpha,
&wicBmp
),
L"CreateBitmapFromHBITMAPʧ°Ü"
);
// 裁剪出客户区
ComPtr<IWICBitmapClipper> wicBmpClipper = nullptr;
_wicImgFactory->CreateBitmapClipper(&wicBmpClipper);
Debug::ThrowIfComFailed(
_wicImgFactory->CreateBitmapClipper(&wicBmpClipper),
L"CreateBitmapClipperʧ°Ü"
);
WICRect wicRect = {
_srcRect.left - windowRect.left,
@ -52,7 +72,10 @@ private:
srcSize.cx,
srcSize.cy
};
wicBmpClipper->Initialize(wicBmp.Get(), &wicRect);
Debug::ThrowIfComFailed(
wicBmpClipper->Initialize(wicBmp.Get(), &wicRect),
L"wicBmpClipper³õʼ»¯Ê§°Ü"
);
return wicBmpClipper;
}
@ -61,23 +84,54 @@ private:
SIZE srcSize = Utils::GetSize(_srcRect);
HDC hdcSrc = GetDC(_hwndSrc);
Debug::ThrowIfWin32Failed(
hdcSrc,
L"GetDCʧ°Ü"
);
HDC hdcDest = CreateCompatibleDC(hdcSrc);
Debug::ThrowIfWin32Failed(
hdcDest,
L"CreateCompatibleDCʧ°Ü"
);
HBITMAP hBmpDest = CreateCompatibleBitmap(hdcSrc, srcSize.cx, srcSize.cy);
SelectObject(hdcDest, hBmpDest);
BitBlt(hdcDest, 0, 0, srcSize.cx, srcSize.cy, hdcSrc, 0, 0, SRCCOPY);
ReleaseDC(_hwndSrc, hdcSrc);
ComPtr<IWICBitmap> wicBmp = nullptr;
_wicImgFactory->CreateBitmapFromHBITMAP(
Debug::ThrowIfWin32Failed(
hBmpDest,
NULL,
WICBitmapAlphaChannelOption::WICBitmapIgnoreAlpha,
&wicBmp
L"CreateCompatibleBitmapʧ°Ü"
);
DeleteBitmap(hBmpDest);
DeleteDC(hdcDest);
Debug::ThrowIfWin32Failed(
SelectObject(hdcDest, hBmpDest),
L"SelectObjectʧ°Ü"
);
Debug::ThrowIfWin32Failed(
BitBlt(hdcDest, 0, 0, srcSize.cx, srcSize.cy, hdcSrc, 0, 0, SRCCOPY),
L"BitBltʧ°Ü"
);
Debug::ThrowIfWin32Failed(
ReleaseDC(_hwndSrc, hdcSrc),
L"ReleaseDCʧ°Ü"
);
ComPtr<IWICBitmap> wicBmp = nullptr;
Debug::ThrowIfComFailed(
_wicImgFactory->CreateBitmapFromHBITMAP(
hBmpDest,
NULL,
WICBitmapAlphaChannelOption::WICBitmapIgnoreAlpha,
&wicBmp
),
L"CreateBitmapFromHBITMAPʧ°Ü"
);
Debug::ThrowIfWin32Failed(
DeleteBitmap(hBmpDest),
L"DeleteBitmapʧ°Ü"
);
Debug::ThrowIfWin32Failed(
DeleteDC(hdcDest),
L"DeleteDCʧ°Ü"
);
return wicBmp;
}