浮动小游戏已经基本搞好
新做了两个设置界面 一些bug修复和优化
This commit is contained in:
@@ -21,6 +21,9 @@ public static class WebViewWin32
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
||||
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct RECT
|
||||
{
|
||||
@@ -45,10 +48,26 @@ public static class WebViewWin32
|
||||
public IntPtr lpData;
|
||||
}
|
||||
|
||||
// SetWindowPos flags
|
||||
const uint SWP_NOZORDER = 0x0004;
|
||||
const uint SWP_NOACTIVATE = 0x0010;
|
||||
const uint SWP_NOMOVE = 0x0002;
|
||||
const uint SWP_NOSIZE = 0x0001;
|
||||
|
||||
// HWND insert-after special values
|
||||
static readonly IntPtr HWND_TOP = new IntPtr(0);
|
||||
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
|
||||
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
|
||||
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
|
||||
|
||||
const uint WM_COPYDATA = 0x004A;
|
||||
|
||||
// ShowWindow commands
|
||||
const int SW_HIDE = 0;
|
||||
const int SW_SHOW = 5;
|
||||
const int SW_MINIMIZE = 6;
|
||||
const int SW_RESTORE = 9;
|
||||
|
||||
public static bool TrySetWindowRect(Process process, int x, int y, int width, int height)
|
||||
{
|
||||
if (process == null || process.HasExited)
|
||||
@@ -74,6 +93,44 @@ public static class WebViewWin32
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Place the given process window directly above the target process window in Z-order
|
||||
/// and set its position/size. This ensures the webview sits just above the Unity window.
|
||||
/// </summary>
|
||||
public static bool TrySetWindowRectAboveProcess(Process process, Process targetProcess, int x, int y, int width, int height)
|
||||
{
|
||||
if (process == null || process.HasExited || targetProcess == null || targetProcess.HasExited)
|
||||
return false;
|
||||
|
||||
if (process.MainWindowHandle == IntPtr.Zero)
|
||||
{
|
||||
process.Refresh();
|
||||
if (process.MainWindowHandle == IntPtr.Zero)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (targetProcess.MainWindowHandle == IntPtr.Zero)
|
||||
{
|
||||
targetProcess.Refresh();
|
||||
if (targetProcess.MainWindowHandle == IntPtr.Zero)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Place process window after targetProcess window in Z-order (i.e. on top of target)
|
||||
// Do not use SWP_NOZORDER so that z-order is changed.
|
||||
bool ok = SetWindowPos(
|
||||
process.MainWindowHandle,
|
||||
targetProcess.MainWindowHandle,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
SWP_NOACTIVATE
|
||||
);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
public static bool TryGetWindowRect(Process process, out RECT rect)
|
||||
{
|
||||
rect = new RECT();
|
||||
@@ -114,6 +171,48 @@ public static class WebViewWin32
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to show or hide the given process main window.
|
||||
/// </summary>
|
||||
public static bool TryShowWindow(Process process, bool show)
|
||||
{
|
||||
if (process == null || process.HasExited)
|
||||
return false;
|
||||
|
||||
if (process.MainWindowHandle == IntPtr.Zero)
|
||||
{
|
||||
process.Refresh();
|
||||
if (process.MainWindowHandle == IntPtr.Zero)
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!show)
|
||||
{
|
||||
// Minimize instead of hide
|
||||
return ShowWindow(process.MainWindowHandle, SW_MINIMIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Restore and bring to top
|
||||
bool r = ShowWindow(process.MainWindowHandle, SW_RESTORE);
|
||||
try
|
||||
{
|
||||
// bring to top without making it topmost
|
||||
SetWindowPos(process.MainWindowHandle, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
|
||||
}
|
||||
catch { }
|
||||
return r;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
UnityEngine.Debug.LogWarning("TryShowWindow failed: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试向指定进程的主窗口发送 WM_COPYDATA 消息,lParam 为包含 Unicode 字符串的 COPYDATASTRUCT。
|
||||
/// 接收方需要处理 WM_COPYDATA 并解释字符串为 URL 并导航。
|
||||
|
||||
Reference in New Issue
Block a user