User32dllStyles.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Drawing;
4 using System.Linq;
5 using System.Runtime.InteropServices;
6 using System.Text;
7 using System.Windows.Forms;
8 
9 namespace sage.ew.conta.plugins
10 {
14  static public class StyleHBDialog
15  {
16  private static readonly string TMDICHILD = "TMdiChildSP";
17 
23  public static void _ApplyStyle(IntPtr handleHB, Control controlContainer)
24  {
25  //redimensionamos al tamaño del control
26  if (_IsMdiChild(handleHB))
27  {
28  _SetOwner(handleHB, controlContainer.Handle);
29  User32dllCommands.MoveWindow(handleHB, 0, 0, controlContainer.Size.Width, controlContainer.Height, false);
30  }
31  }
32 
38  public static bool _IsMdiChild(IntPtr handleHB)
39  {
40  StringBuilder className = new StringBuilder();
41  StringBuilder typeMDI = new StringBuilder(TMDICHILD);
42  User32dllCommands.GetClassName(handleHB, className, 12);
43 
44  return className.Equals(typeMDI);
45  }
46 
52  public static void _SetParentAndCentre(IntPtr handleHB, Form mainFrm)
53  {
54  try
55  {
56  int styleGet = User32dllCommands.GetWindowLong(handleHB, (int)WindowLongFlags.GWL_STYLE);
57  styleGet &= ~(int)WindowStyles.WS_VISIBLE;
58  styleGet &= Convert.ToInt32(WindowStyles.WS_POPUP);
59 
60  User32dllCommands.SetWindowLong(handleHB, (int)WindowLongFlags.GWL_STYLE, (uint)styleGet);
61  User32dllCommands.SetParent(handleHB, mainFrm.Handle);
62 
63  User32dllCommands.SendMessage(handleHB, User32dllCommands.WM_SYSCOMMAND, User32dllCommands.SC_RESTORE, 0);
64  _CenterWindow(handleHB, mainFrm);
65  }
66  catch (Exception ex)
67  {
68  MessageBox.Show(ex.Message);
69  }
70  }
71 
77  public static void _SetOwner(IntPtr handleHB, IntPtr handleParent)
78  {
79  User32dllCommands.SetWindowLong32(handleHB, -8, handleParent);
80  }
81 
86  public static void _BringToFront(IntPtr handleHB)
87  {
88  User32dllCommands.ShowWindowAsync(handleHB, (int)ShowWindowCommands.ShowDefault);
89  User32dllCommands.ShowWindowAsync(handleHB, (int)ShowWindowCommands.Show);
90  User32dllCommands.SetForegroundWindow(handleHB);
91  }
92 
98  static void _CenterWindow(IntPtr handleHB, Form mainFrm)
99  {
100  RECT rect;
101  User32dllCommands.GetWindowRect(new HandleRef(mainFrm, handleHB), out rect);
102 
103  Size monitorDimensions = mainFrm.Size;
104  int x1Pos = monitorDimensions.Width / 2 - (rect.Right - rect.Left) / 2;
105  int x2Pos = rect.Right - rect.Left;
106  int y1Pos = monitorDimensions.Height / 2 - (rect.Bottom - rect.Top) / 2;
107  int y2Pos = rect.Bottom - rect.Top;
108 
109  User32dllCommands.SetWindowPos(handleHB, (IntPtr)0, x1Pos, y1Pos, x2Pos, y2Pos, SetWindowPosFlags.SWP_SHOWWINDOW);
110  }
111 
112 
113  }
114 }
SetWindowPosFlags
Representa la posición de una ventana