First of all, the problem with the incompatibility of the irregular form in WPF with the WindowsFormsHost control is that the solution given on the web does not meet all the circumstances and is conditional, such as the compatibility problem resolution of irregular forms and WebBrowser controls in WPF. The Netizen's solution is also ingenuity, why do you say that, you downloaded his program seriously read Handy know, his WebBrowser control is placed in a single form, so that the form and WPF in a Bord control to associate, to move synchronously, However, when moving, there will be flashing, and there will be a white point of movement, the user experience is certainly not good.
OK, around a big circle, or to get to the point, why this problem, what causes the transparent form in WPF, embedded in the WinForm control will not show. At first I thought it was not normal to load, and I have uispy, through this software, I capture the current running program, I found that my WPF embedded in the WinForm control has been loaded, just did not see it. Very depressed AH.
Sad reminder of the procedure, headache ah, what causes it, online search information, found http://msdn.microsoft.com/zh-cn/library/aa970688.aspx, let me know a lot of knowledge. Because the project is going to use transparent form also to make the fillet form, saying that originally intended not to change the setting of window in WPF, that is, do not change windowstyle= "None" and allowtransparent = "True" These settings, Want to make some settings on the WindowsFormsHost, found that the road does not go through. Wasted a lot of time.
Blocked only change ideas, then put allowtransparent = "false", then you can display, hehe ... Of course, there's a change. The WPF form is ugly, with a border outside. How to do, how to do, this is also a problem ah. To use the features of WPF, tragedy, there seems to be no relevant method ah.
OK, the road is still there, the programmer is to solve the solution, how to do, can only invoke the Windows API, the outermost layer of the border is removed. So what is needed, the idea is there, right, then action, Google and Baidu a pass, found there are many examples, C + + examples of the most comprehensive, you can refer to. Well, then, just sort it out. These functions are required:
SetWindowLong setting values for window styles
GetWindowLong get the style of window
SetWindowRgn Setting the window's workspace
CreateRoundRectRgn creating an area with rounded corners
SetLayeredWindowAttributes setting up a hierarchy form for transparency
Direct Baidu, the encyclopedia has a detailed explanation of them, but given is the C + + interpretation, then need you to C + + things to convert to C # things, about C # How to call C + + DLL files, Baidu and Google have you want to answer, I have to fill how much, but pay attention to the type of conversion and character
Set of conversions.
Below I put the function that I converted good to everybody to put up, readers.
public class NativeMethods
{
<summary>
Styles for Windows with outline and caption
</summary>
Public Const Long ws_caption = 0x00c0000l;
Public Const Long Ws_border = 0x0080000l;
<summary>
Window extended style layered display
</summary>
Public Const Long ws_ex_layered = 0x00080000l;
<summary>
A style with alpha
</summary>
Public Const Long Lwa_alpha = 0x00000002l;
<summary>
Color settings
</summary>
Public Const Long Lwa_colorkey = 0x00000001l;
<summary>
Basic style of window
</summary>
public const INT Gwl_style =-16;
<summary>
Extended style of window
</summary>
public const INT Gwl_exstyle =-20;
<summary>
Set the style of a form
</summary>
<param Name= the handle to the handle > Action Form </param>
<param name= "Oldstyle" > To set the style type of the form .</param>
<param name= "NewStyle" > New style </param>
[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern void SetWindowLong (IntPtr handle, int oldstyle, long newstyle);
<summary>
Gets the style specified by the form.
</summary>
<param Name= the handle to the handle > Action Form </param>
<param name= "style" > Style to be returned </param>
<returns> style of the current window </returns>
[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern long GetWindowLong (IntPtr handle, int style);
<summary>
Sets the work area of the form.
</summary>
<param name= "Handle" > manipulate the handle of the form .</param>
<param name= "Handleregion" > manipulate the handle of the form region .</param>
<param name= "Regraw" >if set to <c>true</c> [regraw].</param>
<returns> return value </returns>
[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern int SetWindowRgn (INTPTR handle, IntPtr handleregion, bool Regraw);
<summary>
Creates an area with rounded corners.
</summary>
<param name= "x1" > X-Value of the upper-left coordinate .</param>
<param name= "y1" > Y-value of the upper-left coordinate .</param>
<param name= "x2" > lower-Right coordinate X-value .</param>
<param name= "y2" > Y-value of the lower-right coordinate .</param>
<param name= "width" > rounded oval width.</param>
<param name= "height" > rounded oval height.</param>
Handle of <returns>hrgn </returns>
[System.Runtime.InteropServices.DllImport ("Gdi32.dll")]
public static extern IntPtr createroundrectrgn (int x1, int y1, int x2, int y2, int width, int height);
<summary>
Sets the layered window attributes.
</summary>
<param name= "handle" > Window handle to be manipulated </param>
<param name= value of "ColorKey" >rgb </param>
<param name= "Alpha" >alpha value, transparency </param>
<param name= "Flags" > included Parameters </param>
<returns>true or false</returns>
[System.Runtime.InteropServices.DllImport ("User32.dll")]
public static extern bool SetLayeredWindowAttributes (INTPTR handle, ulong ColorKey, Byte alpha, long flags);
}
The following question is how to do this, first add a Load event to the WPF form that embeds the WinForm control, and add the following code to the event:
Get form Handle
INTPTR hwnd = new System.Windows.Interop.WindowInteropHelper (this). Handle;
Get the style of a form
Long Oldstyle = Nativemethods.getwindowlong (hwnd, Nativemethods.gwl_style);
Change the style of a form to a borderless form
Nativemethods.setwindowlong (hwnd, Nativemethods.gwl_style, Oldstyle & ~nativemethods.ws_caption);
SetWindowLong (hwnd, Gwl_exstyle, Oldstyle & ~ws_ex_layered);
1 | 2 << 8 | 3 << r=1,g=2,b=3 See Winuse.h file
Set form as transparent form
Nativemethods.setlayeredwindowattributes (hwnd, 1 | 2 << 8 | 3 <<, 0, Nativemethods.lwa_alpha);
Create rounded Corners Form 12 This value can be set according to its own project
Nativemethods.setwindowrgn (hwnd, NATIVEMETHODS.CREATEROUNDRECTRGN (0, 0, Convert.ToInt32 (this. ActualWidth), Convert.ToInt32 (this. ActualHeight), true);
There is the form after the change in size and also to redraw the fillet form, otherwise there is a very undesirable display effect, add the following event code, to solve the change in size of the form, redraw the form's fillet area:
<summary>
Handles the SizeChanged event of the Desktopshell control.
</summary>
<param name= "Sender" >the source of the event.</param>
<param name= "E" >the <see cref= "System.Windows.SizeChangedEventArgs"/> Instance containing the event Data.</param>
private void Desktopshell_sizechanged (object sender, Sizechangedeventargs e)
{
Get form Handle
INTPTR hwnd = new System.Windows.Interop.WindowInteropHelper (this). Handle;
Create a fillet form
Nativemethods.setwindowrgn (hwnd,nativemethods.createroundrectrgn (0, 0, Convert.ToInt32 (this. ActualWidth), Convert.ToInt32 (this. ActualHeight), true);
}
At this point to solve all the problem, if a friend has doubts, can leave a message, can help you, I am honored.
Compatibility of irregular forms with windowsformshost controls in WPF perfect solution