Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Form With Shadow

Form With Shadow

Scheduled Pinned Locked Moved C#
questionhelptutorial
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roshihans
    wrote on last edited by
    #1

    I have a form and a png image file (with a transparent shadow in its border) and what I want is to make a drop shadow effect on the form. I know there are CS_DROPSHADOW solution for this, but I think it doesn't look too good. So I create a "fake" form using CreateWindowEx (via DllImport), and draw the png image file into the fake form.

    void CreateFakeForm()
    {
    WNDCLASSEX wndClsEx = new WNDCLASSEX();
    wndClsEx.Init();
    wndClsEx.style = CS_VREDRAW | CS_HREDRAW;
    wndClsEx.lpfnWndProc = m_DefWndProcDelegate;
    wndClsEx.cbClsExtra = 0;
    wndClsEx.cbWndExtra = 0;
    wndClsEx.hInstance = GetModuleHandle(null);
    wndClsEx.hIcon = IntPtr.Zero;
    wndClsEx.hIconSm = IntPtr.Zero;
    wndClsEx.hCursor = IntPtr.Zero;
    wndClsEx.hbrBackground = IntPtr.Zero;
    wndClsEx.lpszClassName = m_WndClsName;
    wndClsEx.lpszMenuName = null;

            bool success = RegisterClassEx(ref wndClsEx) != 0;
    
            UInt32 dwExStyle = WS\_EX\_LAYERED |
                WS\_EX\_TRANSPARENT |
                WS\_EX\_NOACTIVATE |
                WS\_EX\_LEFT;
            UInt32 dwStyle = WS\_VISIBLE | WS\_OVERLAPPED;
            
            FakeWndHandle = CreateWindowEx(dwExStyle
                , m\_WndClsName
                , null
                , dwStyle
                , X
                , Y
                , PngImg.Width
                , PngImg.Height
                , MainFormHandle
                , IntPtr.Zero
                , GetModuleHandle(null)
                , IntPtr.Zero
                );
    

    }

    The problem is my fake form always in the topmost position. Is there a way to put the fake form behind the main form? In other words, how do I set z-order between the main form and the fake form so that the fake form placed at the bottom of the Z order? How to hide the fake form whenever the main form minimized or maximized, and move or resize the fake form whenever the main form moved or resized? Thanks.

    S S 2 Replies Last reply
    0
    • R roshihans

      I have a form and a png image file (with a transparent shadow in its border) and what I want is to make a drop shadow effect on the form. I know there are CS_DROPSHADOW solution for this, but I think it doesn't look too good. So I create a "fake" form using CreateWindowEx (via DllImport), and draw the png image file into the fake form.

      void CreateFakeForm()
      {
      WNDCLASSEX wndClsEx = new WNDCLASSEX();
      wndClsEx.Init();
      wndClsEx.style = CS_VREDRAW | CS_HREDRAW;
      wndClsEx.lpfnWndProc = m_DefWndProcDelegate;
      wndClsEx.cbClsExtra = 0;
      wndClsEx.cbWndExtra = 0;
      wndClsEx.hInstance = GetModuleHandle(null);
      wndClsEx.hIcon = IntPtr.Zero;
      wndClsEx.hIconSm = IntPtr.Zero;
      wndClsEx.hCursor = IntPtr.Zero;
      wndClsEx.hbrBackground = IntPtr.Zero;
      wndClsEx.lpszClassName = m_WndClsName;
      wndClsEx.lpszMenuName = null;

              bool success = RegisterClassEx(ref wndClsEx) != 0;
      
              UInt32 dwExStyle = WS\_EX\_LAYERED |
                  WS\_EX\_TRANSPARENT |
                  WS\_EX\_NOACTIVATE |
                  WS\_EX\_LEFT;
              UInt32 dwStyle = WS\_VISIBLE | WS\_OVERLAPPED;
              
              FakeWndHandle = CreateWindowEx(dwExStyle
                  , m\_WndClsName
                  , null
                  , dwStyle
                  , X
                  , Y
                  , PngImg.Width
                  , PngImg.Height
                  , MainFormHandle
                  , IntPtr.Zero
                  , GetModuleHandle(null)
                  , IntPtr.Zero
                  );
      

      }

      The problem is my fake form always in the topmost position. Is there a way to put the fake form behind the main form? In other words, how do I set z-order between the main form and the fake form so that the fake form placed at the bottom of the Z order? How to hide the fake form whenever the main form minimized or maximized, and move or resize the fake form whenever the main form moved or resized? Thanks.

      S Offline
      S Offline
      Saksida Bojan
      wrote on last edited by
      #2

      Why don't you override CreateParams instead of creating FakeWindow? This example overrides default border and replace it with AERO. to get WS_ numbers, go to your Windows SDK and look under window.h for decleration

      protected override CreateParams CreateParams
      {
      get
      {
      CreateParams cp = base.CreateParams;
      unchecked
      {
      cp.Style |= (int)0x80000000; // WS_POPUP
      cp.Style |= 0x40000; // WS_THICKFRAME
      }
      return cp;
      }
      }

      R 1 Reply Last reply
      0
      • S Saksida Bojan

        Why don't you override CreateParams instead of creating FakeWindow? This example overrides default border and replace it with AERO. to get WS_ numbers, go to your Windows SDK and look under window.h for decleration

        protected override CreateParams CreateParams
        {
        get
        {
        CreateParams cp = base.CreateParams;
        unchecked
        {
        cp.Style |= (int)0x80000000; // WS_POPUP
        cp.Style |= 0x40000; // WS_THICKFRAME
        }
        return cp;
        }
        }

        R Offline
        R Offline
        roshihans
        wrote on last edited by
        #3

        Thanks for the reply, I'm using Windows XP, so I think there are no WS_ for AERO.

        S 1 Reply Last reply
        0
        • R roshihans

          Thanks for the reply, I'm using Windows XP, so I think there are no WS_ for AERO.

          S Offline
          S Offline
          Saksida Bojan
          wrote on last edited by
          #4

          when i said WS_ i meant WS_EX_NOACTIVATE and other. The one I post was uses AERO. It isn't limited for aero. It has all same as unmanaged code

          R 1 Reply Last reply
          0
          • S Saksida Bojan

            when i said WS_ i meant WS_EX_NOACTIVATE and other. The one I post was uses AERO. It isn't limited for aero. It has all same as unmanaged code

            R Offline
            R Offline
            roshihans
            wrote on last edited by
            #5

            When you say override CreateParams, did you refer to the main form's CreateParams? But then, where do I draw the png image files into?. Sorry, but I'm new in this so I'm still confused.

            S 1 Reply Last reply
            0
            • R roshihans

              When you say override CreateParams, did you refer to the main form's CreateParams? But then, where do I draw the png image files into?. Sorry, but I'm new in this so I'm still confused.

              S Offline
              S Offline
              Saksida Bojan
              wrote on last edited by
              #6

              for painting try look into OnPaint event or you can even ovverride

              roshihans wrote:

              When you say override CreateParams, did you refer to the main form's CreateParams?

              That is correct

              R 1 Reply Last reply
              0
              • S Saksida Bojan

                for painting try look into OnPaint event or you can even ovverride

                roshihans wrote:

                When you say override CreateParams, did you refer to the main form's CreateParams?

                That is correct

                R Offline
                R Offline
                roshihans
                wrote on last edited by
                #7

                Saksida Bojan wrote:

                for painting try look into OnPaint event or you can even ovverride

                I thought if I paint form's border with a transparent-shadow background (using png file), it will not become transparent unless I set the main form transparent too. And how to paint the shadow into the border which is located in non-client area?

                S 1 Reply Last reply
                0
                • R roshihans

                  Saksida Bojan wrote:

                  for painting try look into OnPaint event or you can even ovverride

                  I thought if I paint form's border with a transparent-shadow background (using png file), it will not become transparent unless I set the main form transparent too. And how to paint the shadow into the border which is located in non-client area?

                  S Offline
                  S Offline
                  Saksida Bojan
                  wrote on last edited by
                  #8

                  roshihans wrote:

                  And how to paint the shadow into the border which is located in non-client area?

                  Special 'Graphics' Objects to Draw Anywhere on your Window[^]

                  roshihans wrote:

                  it will not become transparent unless I set the main form transparent too

                  To enable Form transparecy you need to enable transparent color.

                  this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);

                  R 2 Replies Last reply
                  0
                  • S Saksida Bojan

                    roshihans wrote:

                    And how to paint the shadow into the border which is located in non-client area?

                    Special 'Graphics' Objects to Draw Anywhere on your Window[^]

                    roshihans wrote:

                    it will not become transparent unless I set the main form transparent too

                    To enable Form transparecy you need to enable transparent color.

                    this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);

                    R Offline
                    R Offline
                    roshihans
                    wrote on last edited by
                    #9

                    Thanks a lot for your help. I will try your suggestion

                    1 Reply Last reply
                    0
                    • S Saksida Bojan

                      roshihans wrote:

                      And how to paint the shadow into the border which is located in non-client area?

                      Special 'Graphics' Objects to Draw Anywhere on your Window[^]

                      roshihans wrote:

                      it will not become transparent unless I set the main form transparent too

                      To enable Form transparecy you need to enable transparent color.

                      this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);

                      R Offline
                      R Offline
                      roshihans
                      wrote on last edited by
                      #10

                      Saksida Bojan wrote:

                      this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);

                      It seems that SetStyle only applied in client area. I can't make the non-client area transparent. Any suggestion?

                      S 1 Reply Last reply
                      0
                      • R roshihans

                        Saksida Bojan wrote:

                        this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);

                        It seems that SetStyle only applied in client area. I can't make the non-client area transparent. Any suggestion?

                        S Offline
                        S Offline
                        Saksida Bojan
                        wrote on last edited by
                        #11

                        In a book i am reading, this is formula for getting transparecy displayColor = sourceColor × alpha / 255 + backgroundColor × (255 - alpha) / 255 ImageTraverser[^] This link is very good. Create a snapshot and you can get pixel. Image.GetPixel is super slow, this example is very fast.

                        1 Reply Last reply
                        0
                        • R roshihans

                          I have a form and a png image file (with a transparent shadow in its border) and what I want is to make a drop shadow effect on the form. I know there are CS_DROPSHADOW solution for this, but I think it doesn't look too good. So I create a "fake" form using CreateWindowEx (via DllImport), and draw the png image file into the fake form.

                          void CreateFakeForm()
                          {
                          WNDCLASSEX wndClsEx = new WNDCLASSEX();
                          wndClsEx.Init();
                          wndClsEx.style = CS_VREDRAW | CS_HREDRAW;
                          wndClsEx.lpfnWndProc = m_DefWndProcDelegate;
                          wndClsEx.cbClsExtra = 0;
                          wndClsEx.cbWndExtra = 0;
                          wndClsEx.hInstance = GetModuleHandle(null);
                          wndClsEx.hIcon = IntPtr.Zero;
                          wndClsEx.hIconSm = IntPtr.Zero;
                          wndClsEx.hCursor = IntPtr.Zero;
                          wndClsEx.hbrBackground = IntPtr.Zero;
                          wndClsEx.lpszClassName = m_WndClsName;
                          wndClsEx.lpszMenuName = null;

                                  bool success = RegisterClassEx(ref wndClsEx) != 0;
                          
                                  UInt32 dwExStyle = WS\_EX\_LAYERED |
                                      WS\_EX\_TRANSPARENT |
                                      WS\_EX\_NOACTIVATE |
                                      WS\_EX\_LEFT;
                                  UInt32 dwStyle = WS\_VISIBLE | WS\_OVERLAPPED;
                                  
                                  FakeWndHandle = CreateWindowEx(dwExStyle
                                      , m\_WndClsName
                                      , null
                                      , dwStyle
                                      , X
                                      , Y
                                      , PngImg.Width
                                      , PngImg.Height
                                      , MainFormHandle
                                      , IntPtr.Zero
                                      , GetModuleHandle(null)
                                      , IntPtr.Zero
                                      );
                          

                          }

                          The problem is my fake form always in the topmost position. Is there a way to put the fake form behind the main form? In other words, how do I set z-order between the main form and the fake form so that the fake form placed at the bottom of the Z order? How to hide the fake form whenever the main form minimized or maximized, and move or resize the fake form whenever the main form moved or resized? Thanks.

                          S Offline
                          S Offline
                          So Sereyboth
                          wrote on last edited by
                          #12

                          how can i use the function 'CreateFakeForm'? could you give me the full project? I 1 2 create a nice shadow behind a customized form... Thanks

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups