回答編集履歴

1

見直しキャンペーン中

2023/07/20 15:33

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,219 +1,110 @@
1
1
  この手法は裏技?だと思うので、アプリごとに試行錯誤が必要になると思います。
2
-
3
2
  APIの呼び出し順やパラメータがシビアで、ちょっとの違いでできたりできなかったりします。
4
3
 
5
-
6
-
7
- ```xaml
4
+ ```xml
8
-
9
5
  <Window
10
-
11
6
  x:Class="Questions243916.MainWindow"
12
-
13
7
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
14
-
15
8
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
16
-
17
9
  xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
18
-
19
10
  Width="800"
20
-
21
11
  Height="450">
22
-
23
12
  <Grid>
24
-
25
13
  <Grid.ColumnDefinitions>
26
-
27
14
  <ColumnDefinition />
28
-
29
15
  <ColumnDefinition Width="Auto" />
30
-
31
16
  <ColumnDefinition />
32
-
33
17
  </Grid.ColumnDefinitions>
34
-
35
18
  <WindowsFormsHost>
36
-
37
19
  <forms:Control x:Name="notepad" />
38
-
39
20
  </WindowsFormsHost>
40
-
41
21
  <GridSplitter
42
-
43
22
  Grid.Column="1"
44
-
45
23
  Width="8"
46
-
47
24
  HorizontalAlignment="Stretch"
48
-
49
25
  Background="DarkGray" />
50
-
51
26
  <WindowsFormsHost Grid.Column="2">
52
-
53
27
  <forms:Control x:Name="paint" />
54
-
55
28
  </WindowsFormsHost>
56
-
57
29
  </Grid>
58
-
59
30
  </Window>
60
-
61
31
  ```
62
32
 
63
-
64
-
65
- ```C#
33
+ ```cs
66
-
67
34
  using System;
68
-
69
35
  using System.Diagnostics;
70
-
71
36
  using System.Runtime.InteropServices;
72
-
73
37
  using System.Threading;
74
-
75
38
  using System.Windows;
76
39
 
77
-
78
-
79
40
  namespace Questions243916
80
-
81
41
  {
82
-
83
42
  public partial class MainWindow : Window
84
-
85
43
  {
86
-
87
44
  [DllImport("user32")] private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
88
-
89
45
  [DllImport("user32")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
90
-
91
46
  [DllImport("user32")] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
92
-
93
47
  [DllImport("user32")] private static extern int MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, int bRepaint);
94
48
 
95
-
96
-
97
49
  private const int GWL_STYLE = -16;
98
-
99
50
  private const int WS_THICKFRAME = 0x00040000;
100
-
101
51
  private const int WS_CAPTION = 0x00C00000;
102
-
103
52
  private const int WS_CHILD = 0x40000000;
104
53
 
105
54
 
106
-
107
-
108
-
109
55
  public MainWindow()
110
-
111
56
  {
112
-
113
57
  InitializeComponent();
114
58
 
115
-
116
-
117
59
  {
118
-
119
60
  var p = Process.Start(new ProcessStartInfo
120
-
121
61
  {
122
-
123
62
  FileName = "notepad",
124
63
 
125
-
126
-
127
64
  // HiddenとしたいがMainWindowHandleが0になる
128
-
129
65
  // FindWindowで探す必要あり 面倒なのでMinimized
130
-
131
66
  //WindowStyle = ProcessWindowStyle.Hidden,
132
-
133
67
  WindowStyle = ProcessWindowStyle.Minimized,
134
-
135
68
  });
136
-
137
69
  p.WaitForInputIdle();
138
70
 
139
-
140
-
141
71
  var style = GetWindowLong(p.MainWindowHandle, GWL_STYLE);
142
-
143
72
  style = style & ~WS_CAPTION & ~WS_THICKFRAME;
144
-
145
73
  //style |= WS_CHILD; // メニュー有無
146
-
147
74
  SetWindowLong(p.MainWindowHandle, GWL_STYLE, style);
148
-
149
-
150
75
 
151
76
  SetParent(p.MainWindowHandle, notepad.Handle);
152
77
 
153
-
154
-
155
78
  notepad.SizeChanged += (s, e)
156
-
157
79
  => MoveWindow(p.MainWindowHandle, 0, 0, notepad.Width, notepad.Height, 1);
158
-
159
80
  }
160
81
 
161
-
162
-
163
82
  {
164
-
165
83
  var p = Process.Start(new ProcessStartInfo
166
-
167
84
  {
168
-
169
85
  FileName = "mspaint",
170
-
171
86
  WindowStyle = ProcessWindowStyle.Minimized,
172
-
173
87
  });
174
-
175
88
  p.WaitForInputIdle();
176
-
177
89
  Thread.Sleep(500); // ウェイトがないと入らなかった
178
90
 
179
-
180
-
181
91
  var style = GetWindowLong(p.MainWindowHandle, GWL_STYLE);
182
-
183
92
  style = style & ~WS_CAPTION & ~WS_THICKFRAME;
184
-
185
93
  SetWindowLong(p.MainWindowHandle, GWL_STYLE, style);
186
-
187
-
188
94
 
189
95
  SetParent(p.MainWindowHandle, paint.Handle);
190
96
 
191
-
192
-
193
97
  // なんかずれる それっぽくなったが根拠なし
194
-
195
98
  var offset = (int)SystemParameters.MenuHeight
196
-
197
99
  + (int)SystemParameters.ResizeFrameHorizontalBorderHeight * 2;
198
-
199
100
  paint.SizeChanged += (s, e)
200
-
201
101
  => MoveWindow(p.MainWindowHandle, 0, -offset, paint.Width, paint.Height + offset, 1);
202
-
203
102
  }
204
-
205
103
  }
206
-
207
104
  }
208
-
209
105
  }
210
-
211
106
  ```
212
-
213
-
214
107
 
215
108
  ---
216
109
 
217
-
218
-
219
110
  メモ帳とペイントを入れるだけでかなり苦労しました^^;