VB.Net WS_EX_LAYERED Forms
-
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
-
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
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
-
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
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!
-
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!
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
-
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
*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!
-
*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!
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