WPF using WindowsXamlHost can set it from quadrilateral to triangle ?

GuoLearn 251 Reputation points
2021-09-01T06:55:52.623+00:00

when WPF use WindowsXamlHost it default quadrilateral and background is white
can we make it triangle?

128149-catchd735.jpg

Developer technologies | Windows Presentation Foundation
{count} votes

Answer accepted by question author
  1. Castorix31 91,506 Reputation points
    2021-09-08T07:30:33.123+00:00

    A way to change its shape is with SetWindowRgn

    A test :

    130116-windowsxamlhost-diamond.jpg

    IntPtr hWndHost = windowsXamlHostControl.Handle;  
    POINT[] pt;  
    pt = new POINT[5];        
    pt[0].x = (int)windowsXamlHostControl.Width/2;  
    pt[0].y = 0;  
    pt[1].x = (int)windowsXamlHostControl.Width;  
    pt[1].y = (int)windowsXamlHostControl.Height / 2;  
    pt[2].x = (int)windowsXamlHostControl.Width / 2;  
    pt[2].y = (int)windowsXamlHostControl.Height;  
    pt[3].x = 0;  
    pt[3].y = (int)windowsXamlHostControl.Height / 2;  
    pt[4].x = (int)windowsXamlHostControl.Width / 2;  
    pt[4].y = 0;  
    IntPtr hRgn = CreatePolygonRgn(ref pt[0], 10, 1);  
    SetWindowRgn(hWndHost, hRgn, true);  
    

    Declarations :

    [DllImport("user32.dll", SetLastError = true)]  
    private static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);  
      
    [DllImport("gdi32.dll", SetLastError = true)]  
    private static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int cx, int cy);  
      
    [DllImport("gdi32.dll", SetLastError = true)]  
    private static extern IntPtr CreatePolygonRgn(ref POINT lpPoint, int nCount, int nPolyFillMode);  
      
    [StructLayout(LayoutKind.Sequential)]  
    public struct POINT  
    {  
        public int x;  
        public int y;  
        public POINT(int X, int Y)  
        {  
            this.x = X;  
            this.y = Y;  
        }  
    }  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.