Skip to main content

 none
C# - Modifying form to disable WS_EX_LAYERED and without borders RRS feed

  • Question

  • Hello all

    I created a form with WS_EX_LAYERED and the UpdateLayeredWindow like so:

    IntPtr screenDc = NativeMethods.GetDC ( IntPtr.Zero );
    IntPtr memDc = NativeMethods.CreateCompatibleDC ( screenDc );
    IntPtr hBitmap = IntPtr.Zero;
    IntPtr oldBitmap = IntPtr.Zero;
    
    hBitmap = bm.GetHbitmap ( Color.FromArgb ( 0 ) );
    oldBitmap = NativeMethods.SelectObject ( memDc, hBitmap );
    
    Size size = new Size ( bm.Width, bm.Height );
    Point pointSource = new Point ( 0, 0 );
    Point topPos = new Point ( Left, Top );
    NativeMethods.BLENDFUNCTION blend = new NativeMethods.BLENDFUNCTION
    {
    BlendOp = NativeMethods.AC_SRC_OVER,
    BlendFlags = 0,
    SourceConstantAlpha = opacity,
    AlphaFormat = NativeMethods.AC_SRC_ALPHA
    };
    
    bool result = NativeMethods.UpdateLayeredWindow ( Handle, screenDc, ref topPos, ref size, 
    memDc, ref pointSource, 0, ref blend, NativeMethods.ULW_ALPHA );
    

    That was to do some animation while the form was loading. Now, when everything is done, I would like to be able to disable the WS_EX_LAYERED bit and make sure the form has no borders around it. I was able to correctly get rid of the WS_EX_LAYERED or the borders by using WS_BORDER. But I can't seem to find a way to do both... Here is what I used for code:

    private void SetWindowAsOpaque ( Form f )
    
    // This method will make the window opaque again.
    {
    int result1 = NativeMethods.SetWindowLong ( Handle, NativeMethods.GWL_EXSTYLE, NativeMethods.GetWindowLong ( f.Handle, NativeMethods.GWL_EXSTYLE) & ~ NativeMethods.WS_EX_LAYERED );
    
    int result2 = NativeMethods.SetWindowLong ( Handle, NativeMethods.WS_STYLE, NativeMethods.GetWindowLong ( f.Handle, NativeMethods.WS_STYLE) & ~ NativeMethods.WS_BORDER );
    
    f.Refresh ( );
    }

    I guess I am missing something but I can't put my finger on it. Can anyone help?

    Many thanks

    Rick


    Thursday, December 21, 2017 8:19 PM

Answers

  • Hi,

    Thank you for posting here.

    As far as i know that the window is a layered window. This style cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.

    Did you use the class style in your code? 

    Each child window has the WS_BORDER style, each has a thin line border. Did you try to hide the thin line border. 

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RickHille Thursday, April 19, 2018 3:24 AM
    Tuesday, December 26, 2017 7:30 AM
    Moderator

All replies

  • Have you tried to change the value of f.FormBorderStyle?

    Thursday, December 21, 2017 8:55 PM
  • @ Viorel: Yes, I have tried it but it did not work.
    Thursday, December 21, 2017 9:24 PM
  • Hi,

    Thank you for posting here.

    As far as i know that the window is a layered window. This style cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.

    Did you use the class style in your code? 

    Each child window has the WS_BORDER style, each has a thin line border. Did you try to hide the thin line border. 

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RickHille Thursday, April 19, 2018 3:24 AM
    Tuesday, December 26, 2017 7:30 AM
    Moderator