05.12.2014, 16:04
#38826401
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Нужно вставить другое окно в свое приложение
Использую такой код:
1. 2. 3. 4. 5. 6. 7.
int style = GetWindowLong(_childHWnd, GWL_STYLE);
RECT rect = new RECT();
GetClientRect(_parentHWnd, out rect);
SetParent(_childHWnd, _parentHWnd);
SetWindowPos(_childHWnd, IntPtr.Zero, 0, 0, rect.Width, rect.Height, 0);
SetWindowLong(_childHWnd, GWL_STYLE, style);
Но есть проблема - потеря фокуса моего приложения при активации встраиваемого окна.
При добавлении стиля WS_CHILD дочернему окну оно теряет фокус вообще
Можно ли это исправить?
Полный код демо примера:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270.
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WinTest
{
public partial class Form1 : Form
{
[DllImport("user32")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int newValue);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr child, IntPtr newParent);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndAfter, Int32 X, Int32 Y, Int32 cx, Int32 cy, Int32 uFlags);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
public const int GWL_STYLE = (-16);
IntPtr _childHWnd;
IntPtr _parentHWnd;
public Form1()
{
InitializeComponent();
Process p = new Process();
p.StartInfo = new ProcessStartInfo("notepad");
p.Start();
p.WaitForInputIdle();
_parentHWnd = this.Handle;
_childHWnd = p.MainWindowHandle;
}
private void Form1_Shown(object sender, EventArgs e)
{
int style = GetWindowLong(_childHWnd, GWL_STYLE);
int newStyle = style;
newStyle |= (int)WindowStyles.WS_CHILD;
RECT rect = new RECT();
GetClientRect(_parentHWnd, out rect);
SetParent(_childHWnd, _parentHWnd);
SetWindowPos(_childHWnd, IntPtr.Zero, 0, 0, rect.Width, rect.Height, 0);
SetWindowLong(_childHWnd, GWL_STYLE, newStyle);
}
}
[Flags]
enum WindowStyles
{
WS_BORDER = 0x00800000,
WS_CAPTION = 0x00C00000,
WS_CHILD = 0x40000000,
WS_CHILDWINDOW = 0x40000000,
WS_CLIPCHILDREN = 0x02000000,
WS_CLIPSIBLINGS = 0x04000000,
WS_DISABLED = 0x08000000,
WS_DLGFRAME = 0x00400000,
WS_GROUP = 0x00020000,
WS_HSCROLL = 0x00100000,
WS_ICONIC = 0x20000000,
WS_MAXIMIZE = 0x01000000,
WS_MAXIMIZEBOX = 0x00010000,
WS_MINIMIZE = 0x20000000,
WS_MINIMIZEBOX = 0x00020000,
WS_OVERLAPPED = 0x00000000,
WS_OVERLAPPEDWINDOW = (int)WS_OVERLAPPED | (int)WS_CAPTION | (int)WS_SYSMENU | (int)WS_THICKFRAME | (int)WS_MINIMIZEBOX | (int)WS_MAXIMIZEBOX,
WS_POPUP = unchecked((int)0x80000000),
WS_POPUPWINDOW = (int)WS_POPUP | (int)WS_BORDER | (int)WS_SYSMENU,
WS_SIZEBOX = 0x00040000,
WS_SYSMENU = 0x00080000,
WS_TABSTOP = 0x00010000,
WS_THICKFRAME = 0x00040000,
WS_TILED = 0x00000000,
WS_TILEDWINDOW = (int)WS_OVERLAPPED | (int)WS_CAPTION | (int)WS_SYSMENU | (int)WS_THICKFRAME | (int)WS_MINIMIZEBOX | (int)WS_MAXIMIZEBOX,
WS_VISIBLE = 0x10000000,
WS_VSCROLL = 0x00200000
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
int _Left;
int _Top;
int _Right;
int _Bottom;
public RECT(RECT Rectangle)
: this(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom)
{
}
public RECT(int Left, int Top, int Right, int Bottom)
{
_Left = Left;
_Top = Top;
_Right = Right;
_Bottom = Bottom;
}
public int X
{
get
{
return _Left;
}
set
{
_Right -= (_Left - value);
_Left = value;
}
}
public int Y
{
get
{
return _Top;
}
set
{
_Bottom -= (_Top - value);
_Top = value;
}
}
public int Left
{
get
{
return _Left;
}
set
{
_Left = value;
}
}
public int Top
{
get
{
return _Top;
}
set
{
_Top = value;
}
}
public int Right
{
get
{
return _Right;
}
set
{
_Right = value;
}
}
public int Bottom
{
get
{
return _Bottom;
}
set
{
_Bottom = value;
}
}
public int Height
{
get
{
return _Bottom - _Top;
}
set
{
_Bottom = value + _Top;
}
}
public int Width
{
get
{
return _Right - _Left;
}
set
{
_Right = value + _Left;
}
}
public Point Location
{
get
{
return new Point(Left, Top);
}
set
{
_Right -= (_Left - value.X);
_Bottom -= (_Top - value.Y);
_Left = value.X;
_Top = value.Y;
}
}
public Size Size
{
get
{
return new Size(Width, Height);
}
set
{
_Right = value.Width + _Left;
_Bottom = value.Height + _Top;
}
}
public static implicit operator Rectangle(RECT Rectangle)
{
return new Rectangle(Rectangle.Left, Rectangle.Top, Rectangle.Width, Rectangle.Height);
}
public static implicit operator RECT(Rectangle Rectangle)
{
return new RECT(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom);
}
public static bool operator ==(RECT Rectangle1, RECT Rectangle2)
{
return Rectangle1.Equals(Rectangle2);
}
public static bool operator !=(RECT Rectangle1, RECT Rectangle2)
{
return !Rectangle1.Equals(Rectangle2);
}
public override string ToString()
{
return "{Left: " + _Left + "; " + "Top: " + _Top + "; Right: " + _Right + "; Bottom: " + _Bottom + "}";
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public bool Equals(RECT Rectangle)
{
return Rectangle.Left == _Left && Rectangle.Top == _Top && Rectangle.Right == _Right && Rectangle.Bottom == _Bottom;
}
public override bool Equals(object Object)
{
if (Object is RECT)
{
return Equals((RECT)Object);
}
else if (Object is Rectangle)
{
return Equals(new RECT((Rectangle)Object));
}
return false;
}
}
}
|
|