回答編集履歴
1
修正
answer
CHANGED
@@ -1,112 +1,4 @@
|
|
1
1
|
RESIZEの配列が少なかったのが、エラー原因でした。
|
2
2
|
(いまの所は、この方法で何とか切り抜けれそうです)
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
```C#
|
7
|
-
|
8
|
-
using System;
|
9
|
-
using System.Runtime.InteropServices;
|
10
|
-
using System.Text;
|
11
|
-
|
12
|
-
namespace ExcelVbaExt
|
13
|
-
{
|
14
|
-
[ComVisible(true)]
|
15
|
-
[ClassInterface(ClassInterfaceType.AutoDual)]
|
16
|
-
public class InnerClass
|
17
|
-
{
|
18
|
-
public int WHnd;
|
19
|
-
public string APPName;
|
20
|
-
public int Left;
|
21
|
-
public int Top;
|
22
|
-
public int Right;
|
23
|
-
public int Bottom;
|
24
|
-
}
|
25
|
-
|
26
|
-
[ComVisible(true)]
|
27
|
-
[ClassInterface(ClassInterfaceType.AutoDual)]
|
28
|
-
public class ClassApp
|
29
|
-
{
|
30
|
-
public InnerClass[] Interface;
|
31
|
-
}
|
32
|
-
|
33
|
-
[ComVisible(true)]
|
34
|
-
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
|
35
|
-
public interface IWindowWhnd
|
36
|
-
{
|
37
|
-
ClassApp WindowTitleList();
|
38
|
-
}
|
39
|
-
|
40
|
-
[ClassInterface(ClassInterfaceType.None)]
|
41
|
-
public class WindowWhnd : IWindowWhnd
|
42
|
-
{
|
43
|
-
public static ClassApp _hWndList;
|
44
|
-
public static int count;
|
45
|
-
|
46
|
-
[DllImport("user32.dll")]
|
47
|
-
private static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
|
48
|
-
|
49
|
-
[StructLayout(LayoutKind.Sequential)]
|
50
|
-
private struct RECT
|
51
|
-
{
|
52
|
-
public int left;
|
53
|
-
public int top;
|
54
|
-
public int right;
|
55
|
-
public int bottom;
|
56
|
-
}
|
57
|
-
|
58
|
-
[DllImport("user32.dll")]
|
59
|
-
[return: MarshalAs(UnmanagedType.Bool)]
|
60
|
-
private extern static bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lparam);
|
61
|
-
|
62
|
-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
63
|
-
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
|
64
|
-
|
65
|
-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
66
|
-
private static extern int GetWindowTextLength(IntPtr hWnd);
|
67
|
-
|
68
|
-
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
69
|
-
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
|
70
|
-
|
71
|
-
private delegate bool EnumWindowsDelegate(IntPtr hWnd, IntPtr lparam);
|
72
|
-
|
73
|
-
public ClassApp WindowTitleList()
|
74
|
-
{
|
75
|
-
count = 0;
|
76
|
-
_hWndList = new ClassApp();
|
77
|
-
_hWndList.Interface = new InnerClass[1];
|
78
|
-
|
79
|
-
EnumWindows(new EnumWindowsDelegate(EnumWindowCallBack), IntPtr.Zero);
|
80
|
-
|
81
|
-
return _hWndList;
|
82
|
-
}
|
83
|
-
|
84
|
-
private static bool EnumWindowCallBack(IntPtr hWnd, IntPtr lparam)
|
85
|
-
{
|
86
|
-
int textLen = GetWindowTextLength(hWnd);
|
87
|
-
if (0 < textLen)
|
88
|
-
{
|
89
|
-
StringBuilder tsb = new StringBuilder(textLen + 1);
|
90
|
-
GetWindowText(hWnd, tsb, tsb.Capacity);
|
91
|
-
|
92
|
-
RECT rect;
|
93
|
-
bool flag = GetWindowRect(hWnd, out rect);
|
94
|
-
|
95
|
-
_hWndList.Interface[count] = new InnerClass();
|
96
|
-
_hWndList.Interface[count].WHnd = (int)hWnd;
|
97
|
-
_hWndList.Interface[count].APPName = tsb.ToString();
|
98
|
-
_hWndList.Interface[count].Left = rect.left;
|
99
|
-
_hWndList.Interface[count].Top = rect.top;
|
100
|
-
_hWndList.Interface[count].Right = rect.right;
|
101
|
-
_hWndList.Interface[count].Bottom = rect.bottom;
|
102
|
-
count += 1;
|
103
|
-
|
4
|
+
Array.Resize(ref _hWndList.Interface, count **+1** );
|
104
|
-
}
|
105
|
-
return true;
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
```
|