Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. public static class NativeWindowHelpers {
  2. public static void ResizeWindowFromHandle(IntPtr windowHandle, int width, int heigth)
  3. {
  4. RECT lpRect;
  5. if (!GetWindowRect(windowHandle, out lpRect)) return;
  6.  
  7. Rectangle windowRectangle = new Rectangle
  8. {
  9. X = lpRect.Left,
  10. Y = lpRect.Top,
  11. Width = lpRect.Right - lpRect.Left + 1,
  12. Height = lpRect.Bottom - lpRect.Top + 1
  13. };
  14.  
  15. SetWindowPos(windowHandle, (IntPtr)SpecialWindowHandles.HWND_TOP, Screen.FromHandle(windowHandle).WorkingArea.Left,
  16. Screen.FromHandle(windowHandle).WorkingArea.Top, CalculateResizeNumber(width, windowRectangle.Size.Width), CalculateResizeNumber(heigth, windowRectangle.Size.Height),
  17. SetWindowPosFlags.SWP_NOACTIVATE);
  18. }
  19.  
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. private static int CalculateResizeNumber(int width, int resizeWidth)
  22. {
  23. var widthExcess = width - resizeWidth;
  24. return resizeWidth + (widthExcess < 0 ? 0 : widthExcess);
  25. }
  26.  
  27. [DllImport("user32.dll", SetLastError = true)]
  28. internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
  29.  
  30. [DllImport("user32.dll", SetLastError = true)]
  31. static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  32.  
  33. [Flags]
  34. public enum SetWindowPosFlags : uint
  35. {
  36. // ReSharper disable InconsistentNaming
  37.  
  38. /// <summary>
  39. /// If the calling thread and the thread that owns the window are attached to different input queues,
  40. /// the system posts the request to the thread that owns the window. This prevents the calling thread from
  41. /// blocking its execution while other threads process the request.
  42. /// </summary>
  43. SWP_ASYNCWINDOWPOS = 0x4000,
  44.  
  45. /// <summary>
  46. /// Prevents generation of the WM_SYNCPAINT message.
  47. /// </summary>
  48. SWP_DEFERERASE = 0x2000,
  49.  
  50. /// <summary>
  51. /// Draws a frame (defined in the window's class description) around the window.
  52. /// </summary>
  53. SWP_DRAWFRAME = 0x0020,
  54.  
  55. /// <summary>
  56. /// Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the
  57. /// window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
  58. /// </summary>
  59. SWP_FRAMECHANGED = 0x0020,
  60.  
  61. /// <summary>
  62. /// Hides the window.
  63. /// </summary>
  64. SWP_HIDEWINDOW = 0x0080,
  65.  
  66. /// <summary>
  67. /// Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost
  68. /// or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
  69. /// </summary>
  70. SWP_NOACTIVATE = 0x0010,
  71.  
  72. /// <summary>
  73. /// Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area
  74. /// are saved and copied back into the client area after the window is sized or repositioned.
  75. /// </summary>
  76. SWP_NOCOPYBITS = 0x0100,
  77.  
  78. /// <summary>
  79. /// Retains the current position (ignores X and Y parameters).
  80. /// </summary>
  81. SWP_NOMOVE = 0x0002,
  82.  
  83. /// <summary>
  84. /// Does not change the owner window's position in the Z order.
  85. /// </summary>
  86. SWP_NOOWNERZORDER = 0x0200,
  87.  
  88. /// <summary>
  89. /// Does not redraw changes. If this flag is set, no repainting of any kind occurs.
  90. /// This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered
  91. /// as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window
  92. /// and parent window that need redrawing.
  93. /// </summary>
  94. SWP_NOREDRAW = 0x0008,
  95.  
  96. /// <summary>
  97. /// Same as the SWP_NOOWNERZORDER flag.
  98. /// </summary>
  99. SWP_NOREPOSITION = 0x0200,
  100.  
  101. /// <summary>
  102. /// Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
  103. /// </summary>
  104. SWP_NOSENDCHANGING = 0x0400,
  105.  
  106. /// <summary>
  107. /// Retains the current size (ignores the cx and cy parameters).
  108. /// </summary>
  109. SWP_NOSIZE = 0x0001,
  110.  
  111. /// <summary>
  112. /// Retains the current Z order (ignores the hWndInsertAfter parameter).
  113. /// </summary>
  114. SWP_NOZORDER = 0x0004,
  115.  
  116. /// <summary>
  117. /// Displays the window.
  118. /// </summary>
  119. SWP_SHOWWINDOW = 0x0040,
  120.  
  121. // ReSharper restore InconsistentNaming
  122. }
  123.  
  124. [StructLayout(LayoutKind.Sequential)]
  125. public struct RECT
  126. {
  127. public int Left; // x position of upper-left corner
  128. public int Top; // y position of upper-left corner
  129. public int Right; // x position of lower-right corner
  130. public int Bottom; // y position of lower-right corner
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement