Unity3d user32.dll拖动窗口并重新获得焦点

时间:2017-04-29 12:07:51

标签: c# unity3d user32

我制作了一个无边框窗口应用程序和一个假的"标题栏拖动它。

我正在使用user32.dll,

这开始拖动窗口(由IBeginDragHandler统一触发):

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int WM_NCLBUTTONUP = 0x00A2;
public const int WM_LBUTTONUP = 0x0202;

[DllImport("User32.dll")]
public static extern bool ReleaseCapture();

[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

IntPtr window = GetActiveWindow();
...
...
ReleaseCapture();
SendMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, 0);

这是为了停止拖动(不确定这部分):

    ReleaseCapture();
    SendMessage(window, WM_NCLBUTTONUP, HTCAPTION, 0);
    SendMessage(window, WM_LBUTTONUP, HTCAPTION, 0);

它在编辑器和构建上运行良好,但在developpement build上产生错误:

  

发生了异常情况:PlayerLoop内部功能   已被称为递归。请通过以下方式联系客户支持   示例项目,以便我们可以重现问题并进行故障排除   它。 LauncherWindow:SendMessage(IntPtr,Int32,Int32,Int32)   LauncherWindow:StartWindowDrag()(在E:\ Unity Projects \ Crime Club   启动\资产\脚本\ LIB \ LauncherWindow.cs:115)   WindowDragZone:UnityEngine.EventSystems.IBeginDragHandler.OnBeginDrag(PointerEventData)   (在E:\ Unity Projects \ Crime Club Launcher \ Assets \ WindowDragZone.cs:9)   UnityEngine.EventSystems.ExecuteEvents:执行(IBeginDragHandler,   BaseEventData)(at   C:\ buildslave \团结\构建\扩展\ guisystem \ UnityEngine.UI \的EventSystem \ ExecuteEvents.cs:64)   UnityEngine.EventSystems.ExecuteEvents:执行(游戏对象,   BaseEventData,EventFunction`1)(at   C:\ buildslave \团结\编译\扩展\ guisystem \ UnityEngine.UI \的EventSystem \ ExecuteEvents.cs:261)   UnityEngine.EventSystems.PointerInputModule:ProcessDrag(PointerEventData)   (在   C:\ buildslave \团结\编译\扩展\ guisystem \ UnityEngine.UI \的EventSystem \ InputModules \ PointerInputModule.cs:261)   UnityEngine.EventSystems.StandaloneInputModule:ProcessMouseEvent(Int32)已   (在   C:\ buildslave \团结\编译\扩展\ guisystem \ UnityEngine.UI \的EventSystem \ InputModules \ StandaloneInputModule.cs:434)   UnityEngine.EventSystems.StandaloneInputModule:ProcessMouseEvent()(at   C:\ buildslave \团结\编译\扩展\ guisystem \ UnityEngine.UI \的EventSystem \ InputModules \ StandaloneInputModule.cs:412)   UnityEngine.EventSystems.StandaloneInputModule:Process()(at   C:\ buildslave \团结\编译\扩展\ guisystem \ UnityEngine.UI \的EventSystem \ InputModules \ StandaloneInputModule.cs:186)   UnityEngine.EventSystems.EventSystem:Update()(at   C:\ buildslave \团结\构建\扩展\ guisystem \ UnityEngine.UI \的EventSystem \ EventSystem.cs:283)

您如何看待这个?

编辑: 好的,所以我最终通过替换

摆脱了这个错误
SendMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, 0);

by:

private const int WM_SYSCOMMAND = 0x112;
private const int MOUSE_MOVE = 0xF012;
SendMessage(window, WM_SYSCOMMAND, MOUSE_MOVE, 0);

现在我有一个最后的小问题: 窗口被拖动,在鼠标释放时掉落,但我看起来窗口正在失去对以下内容的关注: 第一次点击总是错过,我必须点击两次才能再次拖动或只是与统一应用程序进行交互。

我在OnEndDrag中尝试了User32的功能: ShowWindow,SetActiveWindow,SetFocus等...... 我发现的所有内容都与该问题有关,但它们都没有明显效果,我仍然必须点击两次。

1 个答案:

答案 0 :(得分:3)

最后通过以下方式替换SendMessage()来消除此错误:

SendMessageCallback(window, WM_SYSCOMMAND, MOUSE_MOVE, 0, WindowDropCallback, 0);