Skip to content

Commit 4dfdbce

Browse files
committed
UniWinApi.IsActiveではウィンドウの存在チェックも追加。
エディタでWindowController.pickedColorがリードオンリーとなるようにした。
1 parent 69cacf3 commit 4dfdbce

3 files changed

Lines changed: 38 additions & 15 deletions

File tree

Assets/UniWinApi/Scripts/Editor/WindowControllerEditor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public override void OnInspectorGUI()
6060
}
6161
}
6262

63+
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
64+
public class WindowControllerReadOnlyDrawer : PropertyDrawer
65+
{
66+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
67+
{
68+
GUI.enabled = false;
69+
EditorGUI.PropertyField(position, property, label, true);
70+
GUI.enabled = true;
71+
}
72+
}
73+
6374
/// <summary>
6475
/// Set to readonly during playing
6576
/// Reference: http://ponkotsu-hiyorin.hateblo.jp/entry/2015/10/20/003042

Assets/UniWinApi/Scripts/UniWinApi.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public override string ToString()
7575
/// ウィンドウ操作ができる状態ならtrueを返す
7676
/// </summary>
7777
/// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
78-
public bool IsActive { get{return hWnd != IntPtr.Zero;} }
78+
public bool IsActive { get{return (hWnd != IntPtr.Zero && WinApi.IsWindow(hWnd));} }
7979

8080
/// <summary>
8181
/// ウィンドウが最大化されていればtrue
@@ -664,6 +664,12 @@ private static IntPtr MessageCallback(int nCode, IntPtr wParam, ref WinApi.MSG l
664664
/// </summary>
665665
public void BeginFileDrop()
666666
{
667+
if (!IsActive)
668+
{
669+
EndFileDrop();
670+
return;
671+
}
672+
667673
if (!instances.Contains(this)) {
668674
instances.Add(this);
669675
}
@@ -684,26 +690,31 @@ public void EndFileDrop()
684690
EndHook();
685691
instances.Remove(this);
686692

693+
if (!IsActive) return;
694+
695+
// ドロップを受け付ける状態を元に戻す
687696
long exstyle = this.CurrentWindowExStyle;
688-
exstyle &= ~WinApi.WS_EX_ACCEPTFILES; // ドロップ受付をやめる
697+
exstyle &= ~WinApi.WS_EX_ACCEPTFILES;
689698
exstyle |= (WinApi.WS_EX_ACCEPTFILES & this.OriginalWindowExStyle); // 元からドロップ許可ならやはり受付
690699
this.CurrentWindowExStyle = exstyle;
691700
WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE, this.CurrentWindowExStyle);
692-
693701
}
694702

695703
/// <summary>
696704
/// メッセージフックを開始
697705
/// </summary>
698706
private void BeginHook()
699707
{
700-
if (IsHookSet) return;
708+
// ウィンドウが操作不可なら設定はできない
701709
if (!IsActive)
702710
{
703-
IsHookSet = false;
711+
EndHook(); // フック設定なしということにしておく
704712
return;
705713
}
706714

715+
// フック設定済みなら二重には設定しない
716+
if (IsHookSet) return;
717+
707718
// フックを設定
708719
uint threadId = WinApi.GetCurrentThreadId();
709720
IntPtr module = WinApi.GetModuleHandle(null);
@@ -718,7 +729,6 @@ private void BeginHook()
718729
}
719730

720731
IsHookSet = true;
721-
722732
//Debug.Log("BeginHook");
723733
}
724734

@@ -733,9 +743,6 @@ private void EndHook()
733743
myHook = IntPtr.Zero;
734744
myHookCallback = null;
735745

736-
// Unityだと通常はドラッグ不可のはずなので戻しても良いのかも
737-
WinApi.DragAcceptFiles(hWnd, false);
738-
739746
//Debug.Log("EndHook");
740747
}
741748
IsHookSet = false;
@@ -746,7 +753,7 @@ private void EndHook()
746753
/// </summary>
747754
public void Dispose()
748755
{
749-
if (hWnd != IntPtr.Zero && WinApi.IsWindow(hWnd))
756+
if (IsActive)
750757
{
751758
EndFileDrop();
752759
#if UNITY_EDITOR

Assets/UniWinApi/Scripts/WindowController.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
using UnityEngine;
1010

1111
/// <summary>
12-
/// Set to readonly during playing
12+
/// Set editable the bool property
1313
/// </summary>
1414
[System.AttributeUsage(System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
15-
public class BoolPropertyAttribute : PropertyAttribute
16-
{
17-
}
15+
public class BoolPropertyAttribute : PropertyAttribute {}
16+
17+
/// <summary>
18+
/// Set the attribute as readonly
19+
/// </summary>
20+
[System.AttributeUsage(System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
21+
public class ReadOnlyAttribute : PropertyAttribute { }
22+
1823

1924
/// <summary>
2025
/// デスクトップマスコット風の利用法を想定した UniWinApi サンプル。
@@ -113,7 +118,7 @@ public bool enableFileDrop {
113118
/// <summary>
114119
/// Pixel color under the mouse pointer. (Read only)
115120
/// </summary>
116-
[Tooltip("Pixel color under the mouse pointer. (Read only)")]
121+
[ReadOnly, Tooltip("Pixel color under the mouse pointer. (Read only)")]
117122
public Color pickedColor;
118123

119124
private bool isDragging = false;

0 commit comments

Comments
 (0)