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. Visual Basic
  4. VB.Net WS_EX_LAYERED Forms

VB.Net WS_EX_LAYERED Forms

Scheduled Pinned Locked Moved Visual Basic
csharpwinformsquestion
6 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.
  • A Offline
    A Offline
    andrew
    wrote on last edited by
    #1

    Hello! Im new to these forums so please forgive me if I have posted in the wrong section for this. Using winforms under vb.net, how does one set the GWL_EXSTYLE to include WS_EX_LAYERED? I need to set this flag so that I may use UpdateLayeredWindow on the form. This is what I am using : Dim lStyle As Long = GetWindowLong(Me.Handle.ToInt32, GWL_EXSTYLE) lStyle = lStyle And WS_EX_LAYERED Dim retval As Long = SetWindowLong(Me.Handle.ToInt32, GWL_EXSTYLE, lStyle) I tried it after a .Show() call and in the _Load event as well. Someone, please enlighten me! This is holding up my project atm. Thanks! Edited by - andrew_ on 12/4/2003 8:09:57 PM

    C 1 Reply Last reply
    0
    • A andrew

      Hello! Im new to these forums so please forgive me if I have posted in the wrong section for this. Using winforms under vb.net, how does one set the GWL_EXSTYLE to include WS_EX_LAYERED? I need to set this flag so that I may use UpdateLayeredWindow on the form. This is what I am using : Dim lStyle As Long = GetWindowLong(Me.Handle.ToInt32, GWL_EXSTYLE) lStyle = lStyle And WS_EX_LAYERED Dim retval As Long = SetWindowLong(Me.Handle.ToInt32, GWL_EXSTYLE, lStyle) I tried it after a .Show() call and in the _Load event as well. Someone, please enlighten me! This is holding up my project atm. Thanks! Edited by - andrew_ on 12/4/2003 8:09:57 PM

      C Offline
      C Offline
      cnurse
      wrote on last edited by
      #2

      Andrew, I sincerely hope this example, which I found for you, helps out... Using Layered Windows To have a dialog box come up as a translucent window, first create the dialog as usual. Then, on WM_INITDIALOG, set the layered bit of the window's extended style and call SetLayeredWindowAttributes with the desired alpha value. The code might look like this: // Set WS_EX_LAYERED on this window SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED); // Make this window 70% alpha SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA); Note that the third parameter of SetLayeredWindowAttributes is a value that ranges from 0 to 255, with 0 making the window completely transparent and 255 making it completely opaque. This parameter mimics the more versatile BLENDFUNCTION of the AlphaBlend function. To make this window completely opaque again, remove the WS_EX_LAYERED bit by calling SetWindowLong and then ask the window to repaint. Removing the bit is desired to let the system know that it can free up some memory associated with layering and redirection. The code might look like this: // Remove WS_EX_LAYERED from this window styles SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED); // Ask the window and its children to repaint RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); Nursey

      A 1 Reply Last reply
      0
      • C cnurse

        Andrew, I sincerely hope this example, which I found for you, helps out... Using Layered Windows To have a dialog box come up as a translucent window, first create the dialog as usual. Then, on WM_INITDIALOG, set the layered bit of the window's extended style and call SetLayeredWindowAttributes with the desired alpha value. The code might look like this: // Set WS_EX_LAYERED on this window SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED); // Make this window 70% alpha SetLayeredWindowAttributes(hwnd, 0, (255 * 70) / 100, LWA_ALPHA); Note that the third parameter of SetLayeredWindowAttributes is a value that ranges from 0 to 255, with 0 making the window completely transparent and 255 making it completely opaque. This parameter mimics the more versatile BLENDFUNCTION of the AlphaBlend function. To make this window completely opaque again, remove the WS_EX_LAYERED bit by calling SetWindowLong and then ask the window to repaint. Removing the bit is desired to let the system know that it can free up some memory associated with layering and redirection. The code might look like this: // Remove WS_EX_LAYERED from this window styles SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED); // Ask the window and its children to repaint RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); Nursey

        A Offline
        A Offline
        andrew
        wrote on last edited by
        #3

        Nursey, thanks for trying address the issue at hand here. My main objective is to draw to an offscreen DC and use UpdateLayeredWindow to render the form. Being VB.NET Winforms, I am not quite sure how one goes about handling the WM_CREATE, or WM_INITDIALOG messages. If this were VB6 or below I would have no problem with this. The reason why I thought it would be worthwhile to pose the question is because I had checked the winform with spy++ and the WS_EX_LAYERED exstyle was not listed for the window. If there is a setting or a way to assert the WX_EX_LAYERED exstyle to a winform without calling SetWindowLong (Which has no effect on the winform at all) please do let me know! Thanks!

        D 1 Reply Last reply
        0
        • A andrew

          Nursey, thanks for trying address the issue at hand here. My main objective is to draw to an offscreen DC and use UpdateLayeredWindow to render the form. Being VB.NET Winforms, I am not quite sure how one goes about handling the WM_CREATE, or WM_INITDIALOG messages. If this were VB6 or below I would have no problem with this. The reason why I thought it would be worthwhile to pose the question is because I had checked the winform with spy++ and the WS_EX_LAYERED exstyle was not listed for the window. If there is a setting or a way to assert the WX_EX_LAYERED exstyle to a winform without calling SetWindowLong (Which has no effect on the winform at all) please do let me know! Thanks!

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Just for learning purposes ... What are you trying to do with the layered window? What is the goal of rendering the window offscreen? SetWindowLong is the only way your going to set WS_EX_LAYERED, BUT what I noticed in your code is that your ANDing the value in. You must OR the value in to get it to work, at least in the examples I've found (see here[^]) RageInTheMachine9532

          A 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Just for learning purposes ... What are you trying to do with the layered window? What is the goal of rendering the window offscreen? SetWindowLong is the only way your going to set WS_EX_LAYERED, BUT what I noticed in your code is that your ANDing the value in. You must OR the value in to get it to work, at least in the examples I've found (see here[^]) RageInTheMachine9532

            A Offline
            A Offline
            andrew
            wrote on last edited by
            #5

            *headsmack* Youre absolutely right. As for your question of why... If you draw to an offscreen DC, maintain a background color (which in this case is argb(0,0,0,0)) of the DC, pass it to UpdateLayeredWindow with the right params, it will render the window for you and continue to handle the wm_paint messages that come in, until you remove the ws_ex_layered style and re-add it (in which case you need to call your fuction which draws offscreen again...). Its a great lil bit o' magic, and it allows you to have a window, for example, based on a PNG with alpha channel support. Thanks for bringing that ANDing to my attention though! UPDATE: I stumbled upon an article which stated that Integers in vb.net are Longs in vb6. I had to modify the declares for SetWindowLong and GetWindowLong so that the params used Integers and returned Integers instead of Longs. Once I changed this, and used the ORing instead of the ANDing, everything worked as it should have. Thanks again for the help guys!

            C 1 Reply Last reply
            0
            • A andrew

              *headsmack* Youre absolutely right. As for your question of why... If you draw to an offscreen DC, maintain a background color (which in this case is argb(0,0,0,0)) of the DC, pass it to UpdateLayeredWindow with the right params, it will render the window for you and continue to handle the wm_paint messages that come in, until you remove the ws_ex_layered style and re-add it (in which case you need to call your fuction which draws offscreen again...). Its a great lil bit o' magic, and it allows you to have a window, for example, based on a PNG with alpha channel support. Thanks for bringing that ANDing to my attention though! UPDATE: I stumbled upon an article which stated that Integers in vb.net are Longs in vb6. I had to modify the declares for SetWindowLong and GetWindowLong so that the params used Integers and returned Integers instead of Longs. Once I changed this, and used the ORing instead of the ANDing, everything worked as it should have. Thanks again for the help guys!

              C Offline
              C Offline
              cnurse
              wrote on last edited by
              #6

              Andrew...this is a comment to you, because we all get code blindness, but the rest of us have learned a very valuable lesson from Rage, read the code brothers, be the code. I just had the urge to assume I could grab you a quick example and let you go from there. If I had read the code properly then I too would have learned somthing 8-) say la vee as people who talk foreign languages phonetically would say. Chris. Nursey

              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