• 0

[C#] WPF Window with transparency makes WindowsFormsHost disappear


Question

5 answers to this question

Recommended Posts

  • 0
  Nazgulled said:
Hi,

I have a WPF window without border and with Allow Transparency set to true but this makes any WindowsFormsHost component inside the WPF window disappear.

Is there any workaround to solve this?

You'll have to go old school.

I've seen in a few spots on the web where people are asking about using the WebBrowser control with a borderless window. I wondered the same thing. Using straight WPF you can not. However, being the old man that I am, I remember how to use the Win API. Below is the code to not only do it but I threw in some dragging code for good measure.

#region The Classic Window API

//The SendMessage function sends a message to a window or windows.

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

//ReleaseCapture releases a mouse capture

[DllImportAttribute("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

public static extern bool ReleaseCapture();

//SetWindowLong lets you set a window style

[system.Runtime.InteropServices.DllImport("user32.dll")]

static extern int SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);

#endregion

/* Borderless - The window which contains the WebBrowser control* /

Window win = new Window();

const int GWL_STYLE = -16;

const long WS_POPUP = 2147483648;

IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(win).Handle;

SetWindowLong(hWnd, GWL_STYLE, WS_POPUP);

/* Drag Window - Capture this on the mouse down of a control in the window*/

ReleaseCapture();

SendMessage(hWnd, 0xa1, 0x2, 0);

  • 0
  SimpleJack said:
You'll have to go old school.

I've seen in a few spots on the web where people are asking about using the WebBrowser control with a borderless window. I wondered the same thing. Using straight WPF you can not. However, being the old man that I am, I remember how to use the Win API. Below is the code to not only do it but I threw in some dragging code for good measure.

#region The Classic Window API

//The SendMessage function sends a message to a window or windows.

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

//ReleaseCapture releases a mouse capture

[DllImportAttribute("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]

public static extern bool ReleaseCapture();

//SetWindowLong lets you set a window style

[system.Runtime.InteropServices.DllImport("user32.dll")]

static extern int SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);

#endregion

/* Borderless - The window which contains the WebBrowser control* /

Window win = new Window();

const int GWL_STYLE = -16;

const long WS_POPUP = 2147483648;

IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(win).Handle;

SetWindowLong(hWnd, GWL_STYLE, WS_POPUP);

/* Drag Window - Capture this on the mouse down of a control in the window*/

ReleaseCapture();

SendMessage(hWnd, 0xa1, 0x2, 0);

Are we sure this works?

  • 0
  dotnetukguru said:
Are we sure this works?

Works like a champ. Also, lets say you have the WebBrowser control in a grid and you want to set margins to provide transparency around the browser. Just extend the glass to the entire window and set your win.Background to Transparent and you are good to go.

In Summary:

IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(win).Handle;

const int GWL_STYLE = -16;

int style = GetWindowLong(hWnd, GWL_STYLE)

SetWindowLong(hWnd, GWL_STYLE, style );

Extend the glass - Adam Nathan - Extend Glass

Off you go

  • 0
  SimpleJack said:
Also, I had a WS_POPUP constant in the original code. I found it wasn't needed.

Man, I feel like a moron! Yes, you do need the WS_POPUP constant. Sorry about that!

This is the only code you need once you extend the glass and make the background transparent

IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(win).Handle;

SetWindowLong(hWnd, GWL_STYLE, WS_POPUP);

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • PicView 3.1.4 by Razvan Serea PicView is a fast, free and fully customizable image viewer for Windows 10 and 11. It supports a vast range of image file types, including WEBP, GIF, SVG, PNG, JXL, HEIC, PSD and many others. Additional features includes viewing EXIF metadata, image compression, batch resizing, viewing images within archives and comic books, image effects, image galleries, and more. Available in portable and installable versions. PicView 3.1.4 changelog: What's new Mouse Side Buttons Customization: You can now change how the mouse side buttons work. Choose to navigate file history or switch between directories. Find this setting under the Mouse tab in the settings window (#199). Improvements Directory Navigation: Improved navigation between directories. If Search subdirectories is enabled, PicView moves to the next directory in the list; if off (or if there are no directories in the list), it navigates as before. PicView now remembers your startup directory (including subdirectories) and restores it on the next launch. Performance: Preloader has been fine-tuned for better performance and lower memory usage. Bug Fixes Fixed incorrect saved settings path when saving in portable mode (#213). Corrected cases where keybindings and file history were not saved when there was no write permission. Restoring the window from a maximized state now correctly keeps its position when auto-fit is enabled. Fixed start-up menu text alignment and missing text issues. Translations Hebrew translation by @Y-PLONI (#212) Hungarian translation by @JohnFowler58 (#209) Japanese update by @coolvitto (#214) Miscellaneous Updated to Avalonia 11.3.1. Now using ZLinq and ZLinq.FileSystem to reduce memory allocation and possibly improve performance. Future versions managed via Scoop will now preserve configuration files (#15555). Experimental settings (not yet available in the UI—edit UserSettings.json directly): Disable the file watcher (not recommended, untested). Choose preload amount and direction (forward/backward). Download: PicView 3.1.4 | Portable ~50.0 MB (Open Source) Download: PicView ARM64 | Portable ARM64 Links: PicView Home Page | Github Project Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • The design looks good on their phones, but I can't say that I like the look of these.
    • AMD preps Ryzen 9600X3D for those who want a taste of the fastest gaming CPU for cheap by Sayan Sen AMD desktop processors are available across two platforms: socket AM4 and socket AM5. Both of these sockets are still going pretty strong, and the company is offering deals and discounts for both as it continues to release new SKUs for its older platform. For example, the company launched an affordable X3D processor for AM4 this month with the new 5500X3D. These gaming CPUs from AMD have been a massive success, as both AM4 and AM5 X3D chips are quite popular among gamers, and for good reason, too. They are fast, very fast. The massive chunk of vertical 3D stacked last-level cache (LLC) Level 3 cache helps these processors a ton in scenarios such as gaming. In our Ryzen 9 9950X3D review, we saw that 300+ frames per second are possible depending on the game title. While we did not review the 8-core 9800X3D, it has been found to be even slightly faster than the 9950X3D and is currently the most powerful gaming CPU on the planet. The Ryzen 9 9800X3D is selling these days for around $440-$500, and that may still be out of budget for many users. As such, similar to the 5600X3D, which is a 6-core 5000X3D processor cut down from the 8-core 5800X3D, AMD is planning to launch a hexacore Ryzen 5 9600X3D that will have two cores disabled compared to the 9800X3D. The information has been uncovered from the recent system integrator (SI) graphics driver for the recently launched Radeon AI PRO R9700 discrete GPU. The driver (Windows SI Driver for Radeon AI PRO R9700, Version 25.10.13.01) confirms the existence of 9600X3D as well as a 9600 non-X SKU and several other Ryzen PRO 9000 CPUs. The full list of upcoming CPUs is given below: Ryzen 5 9600X3D Ryzen 5 9600 AMD Ryzen 9 PRO 9945 Ryzen 7 PRO 9745 Ryzen 5 PRO 9645 Ryzen 5 PRO 9400 In terms of specs, the core configuration of the 9600X3D will be identical to the Ryzen 9600X and 9600, barring things like clock speeds. However, the six-core X3D will have a lot more L3 cache at 96 MB vs 32 MB on the 9600X and 9600. There is no information on pricing at the moment. Source: AMD (spotted by Mellodic Warrior on X)
    • With the dwindling number of TV license payers the BBC has to look for alternative avenues for income.
  • Recent Achievements

    • Week One Done
      Crunchy6 earned a badge
      Week One Done
    • One Month Later
      KynanSEIT earned a badge
      One Month Later
    • One Month Later
      gowtham07 earned a badge
      One Month Later
    • Collaborator
      lethalman went up a rank
      Collaborator
    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      681
    2. 2
      ATLien_0
      275
    3. 3
      Michael Scrip
      221
    4. 4
      +FloatingFatMan
      170
    5. 5
      Steven P.
      164
  • Tell a friend

    Love Neowin? Tell a friend!