Handle the message mechanism of WPF/WinForm changes in the form size

Source: Internet
Author: User
Tags win32 window

Because I have removed the default border of the WPF form, to zoom and maximize the form with the mouse dragging without blocking the taskbar, you must dynamically call the win32 api, use the message mechanism to process such operations.

The following code is found on the Internet. It does not block the taskbar when the form is maximized. I will comment and analyze the code here to make it easy for Tom to understand (I am also a little white)

Also: [StructLayout (LayoutKind. Sequential)] This feature is often used in control development or definition of API function parameters.

Private const int WM_SYSCOMMAND = 0x112; private System. windows. interop. hwndSource _ HwndSource; public static IntPtr WndProc (IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {switch (msg) {case 0x0024: /* WM_GETMINMAXINFO: The Windows message code, which refers to the change of the window size or position. Each different message has a corresponding encoding * // and then corresponding operations, in wpf, you need to use the HwndSource class to capture messages. In winform, you only need to reload this function and you can WmGetMinMaxInfo (hwnd, lParam); handled = true; break; Default: break;} return (System. intPtr) 0;} private static void WmGetMinMaxInfo (System. intPtr hwnd, System. intPtr lParam) {MinMaxInfo mmi = (MinMaxInfo) Marshal. ptrToStructure (lParam, typeof (MinMaxInfo); // maximize the size and position to adapt to int MONITOR_DEFAULTTONEAREST = 0x00000002; System. intPtr monitor = MonitorFromWindow (hwnd, MONITOR_DEFAULTTONEAREST); if (monitor! = System. intPtr. zero) {MONITORINFO monitorInfo = new MONITORINFO (); GetMonitorInfo (monitor, monitorInfo); RECT rcWorkArea = monitorInfo. rcWork; RECT rcMonitorArea = monitorInfo. rcMonitor; mmi. ptMaxPosition. x = Math. abs (rcWorkArea. left-rcMonitorArea. left); mmi. ptMaxPosition. y = Math. abs (rcWorkArea. top-rcMonitorArea. top); mmi. ptMaxSize. x = Math. abs (rcWorkArea. right-rcWorkArea. left); mmi. ptMaxS Ize. y = Math. abs (rcWorkArea. bottom-rcWorkArea. top); mmi. ptMinTrackSize. x = 400; // The width of mmi. ptMinTrackSize. y = 300; // height} Marshal. structureToPtr (mmi, lParam, true) ;}/// <summary> // POINTAPI: POINT indicates that the structure of the compiler uses an ordered layout, sort the x and Y axes of the vertex in the memory successively, and use it as the attribute type of MinMaxInfo. // </summary> [StructLayout (LayoutKind. sequential)] public struct POINT {// <summary> // x coordinate POINT /// </summary> public int x; // <summary> // /Y coordinate point // </summary> public int y; /// <summary> /// POINT constructed by x and y /// </summary> public POINT (int x, int y) {this. x = x; this. y = y ;}/// <summary> // maximize the default position and size, and the default minimum and maximum trace size of a MINMAXINFO structure pointer /// </summary> [StructLayout (LayoutKind. sequential)] public struct MinMaxInfo {public POINT ptReserved; // <summary> // maximum height (width) of the primary monitor /// </summary> public POINT ptMaxSize; /// <summary> /// maximum height of the display /// </Summary> public POINT ptMaxPosition; /// <summary> /// the smallest high width, it can be used to specify the minimum region /// </summary> public POINT ptMinTrackSize; /// <summary> /// the maximum height. /// </summary> public POINT ptMaxTrackSize;}; [StructLayout (LayoutKind. sequential, Pack = 0)] public struct RECT {public int left; public int top; public int right; public int bottom; public static readonly RECT Empty = new RECT (); /// <summary> Win3 2 window Width </summary> public int Width {get {return Math. abs (right-left );} // Abs needed for bidi OS} // <summary> Win32 window Height </summary> public int Height {get {return bottom-top ;}} /// <summary> Win32 window </summary> public RECT (int left, int top, int right, int bottom) {this. left = left; this. top = top; this. right = right; this. bottom = bottom;} // <summary> Win32 window </summary> public RECT (RECT rcSrc) {this. left = rcSrc. left; this. top = rcSrc. top; this. right = rcSrc. right; this. bottom = rcSrc. bottom ;}/// <summary> /// type of display information /// </summary> [StructLayout (LayoutKind. sequential, CharSet = CharSet. auto)] public class MONITORINFO {// <summary> // you must specify the cbSize in the monitorinfo structure. The size is sizeof (MONITORINFO) or sizeof (MONITORINFOEX) /// </summary> public int cbSize = Marshal. sizeOf (typeof (MONITORINFO); public RECT rcMonitor = new RECT (); // public RECT rcWork = new RECT (); // public int dwFlags = 0 in the work display area ;} /// <summary> /// the GetMonitorInfo function returns the information of a display. /// </summary> /// <param name = "hMonitor"> display handle </param> /// <param name = "lpmi"> display information (output parameters) </param> // <returns> </returns> [DllImport ("user32")] internal static extern bool GetMonitorInfo (IntPtr hMonitor, MONITORINFO lpmi); // <s Ummary> // obtain a specified window handle of the boundary rectangle with the largest intersection area on the screen. (Capable of handling multiple monitors) /// </summary> /// <param name = "handle"> window handle </param> /// <param name = "flags"> may be null or the most close Window and main display </param> /// <returns> </returns> [DllImport ("User32")] internal static extern IntPtr MonitorFromWindow (IntPtr handle, int flags );

In this way, you only need to call the WndProc function in the window you want to process.

 this.SourceInitialized += delegate(object sender, EventArgs e)            {                this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;                this._HwndSource.AddHook(Win32.WndProc);            };

In addition, if there is a more concise code implementation only in the master display maximization process:

This. MaximumSize = new Size (Screen. PrimaryScreen. WorkingArea. Width, Screen. PrimaryScreen. WorkingArea. Height );

 

Finally, I would like to talk about a few points, because some people may doubt that there are so many technologies on the. NET platform, and they don't want to die on a tree. They want to be a comprehensive cross-person. My sweat ~ I am working on a player client interface for my company. The wpf is used and the kernel is vlc. At present, it is also in the development process. In fact, I don't understand wpf at all, but the technology is like this, and you can't do everything, but the syntax is basically the same. I copy a lot of knowledge online, and change it to what I want and understand other people's code. Sometimes you may wonder whether this kind of "Stealing" is good for you. There is no need to question too much, and new technologies are endless. You can't finish learning what you are good at. You can also learn what you are good at when you use it.

Okay. I hope my article will be useful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.