MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.28k stars 2.45k forks source link

HwndHost in MetroWindow #496

Closed spiovesan closed 11 years ago

spiovesan commented 11 years ago

I have a MFC dialog showed in a WPF application using a HwndHost class. The basic code is quite simple

public ref class MyConsoleHost : public HwndHost
{
    HandleRef BuildWindowCore(HandleRef hwndParent)
    {
    HINSTANCE hInstance = fInitializeGlobals();            
    dialog = CreateDialog(hInstance, 
        MAKEINTRESOURCE(IDD_CONSOLE), 
        (HWND) hwndParent.Handle.ToPointer(),
        (DLGPROC) DialogProc); 

    return HandleRef(this, IntPtr(dialog));
    }
}

This works is a standard WPF application. If the WPF window becomes a MetroWindow class, the dialog area (where MFC buttons where displayed) is completely white. I noticed that commenting the <Behaviours:BorderlessWindowBehavior/> in the <Style TargetType="{x:Type Controls:MetroWindow}"> in MetroWindow.xaml make it working (but with an extra old style title bar).

A sample is here https://github.com/spiovesan/hwndwpfmetro (change StartupUri="WindowMetro.xaml" too see metro style) Please help! Ciao, Stefano

bitterskittles commented 11 years ago

good to hear it's working as intended :) http://blogs.msdn.com/b/dwayneneed/archive/2013/02/26/mitigating-airspace-issues-in-wpf-applications.aspx see: https://github.com/MahApps/MahApps.Metro/issues/488

spiritdead commented 11 years ago

the idea is try find another solution and no use the other DLL ...

bitterskittles commented 11 years ago

Well after peeking through the code I think I found the culprit.

This should fix it for .NET 4.5 without referencing additional dll. MA.M for .NET 4 client profile can use the source code from here http://archive.msdn.microsoft.com/WPFShell/Release/ProjectReleases.aspx?ReleaseId=4332, or ILMerge the Microsoft.Windows.Shell.dll

spiovesan commented 11 years ago

Hi, thank you for your help! I was able to make the HwndHost working using DwayneNeed and 4.0 with the AirspaceDecorator. I am not able to make the HwndHost working with latest Mahapps and 4.5 without additional dlls. I have some random errors with the VS2010 designer using DwayneNeed and now I am looking how to make the HwndHost working with Microsoft.Windows.Shell. May I have some quick suggestion how to use it?

bitterskittles commented 11 years ago

Have you tried your project with MA.M make-windowchrome-not-suck branch?

Also, a fix for AirspaceDecorator borking the XAML designer issue: Set AirspaceDecorator.AirspaceMode to "None" in XAML, and change it to "Clip" or "Redirect" in runtime.

spiovesan commented 11 years ago

Thanks for the suggestions. I tried the make-windowchrome-not-suck, but unfortunately the WindowsFormsHost does not work with a derived HwndHost element. Probably I should encapsulate my dialog in a WindowsForms Control to make it working. I have a question about the solution I should go: my real application is a vision system application, showing images in real time display too (the MFC part is a third part library with an CWnd ‘console’, where you can show images and graphics; this component is using DirectDraw). I have some concern using the AirspaceDecorator, that would decrease the performances requiring additional CPU work. Instead hosting the CWnd console in a WindowsForms Control should not require any additional CPU work. What do you think?

bitterskittles commented 11 years ago

@spiovesan: It is because BorderlessWindowBehavior sets Window.AllowsTransparency = true This is how your "hwndinwpf" demo works without AllowsTransparency

hwndinwpf

spiovesan commented 11 years ago

Here again.. I used the AirspaceDecorator on my desktop PC (a HP 8100 i5) with no problems. Then I installed the same application on a production PC (Windows7 embedded with a i7) and the application started sucking (idle) 50% of the CPU and memory (up to 1 GB). The issue is the AirspaceDecorator, but I have no idea why... Then I am trying another way, embedding my MFC classes in a WindowsFormsControl. As before, this works with a plain wpf but the control is invisible with the Metro style (even without AllowsTransparency). The sample application is here https://github.com/spiovesan/GithubSamples/tree/master/MyWindowsFormControlProj. Thank you for any help!

bitterskittles commented 11 years ago

@spiovesan lets see: You downloaded MA.M code. removed AllowTransparency assignment statement from BorderlessWindowBehavior (what I said in my previous post), recompiled, and didn't see this:

image

(https://github.com/MahApps/MahApps.Metro/pull/503#issuecomment-19036221)

spiovesan commented 11 years ago

Sorry my fault… When removing the AirspaceDecorator I forgot to add the behaviours:BorderlessWindowBehavior/ section. Now it works with both the solutions (HwndHost and WidowsFormControl) and with no CPU load. Thank you for yor help.

spiovesan commented 10 years ago

We would like to update the MahApps.Metro-make-windowchrome-not-suck we are using but the branch is disappeared and the latest MahApps 0.99 does not work. Is this branch still maintained, in a new branch or merged?

punker76 commented 10 years ago

@spiovesan have you tried the latest alpha?

spiovesan commented 10 years ago

I tried this one @ https://github.com/MahApps/MahApps.Metro/archive/master.zip downloaded yesterday. Is this correct?

AzureKitsune commented 10 years ago

@spiovesan Yes. That's more or less the same code the alpha uses.

spiovesan commented 10 years ago

The sample @ https://github.com/spiovesan/GithubSamples/tree/master/MyWindowsFormControlProj. is working with the branch. Referencing the latest MahApps.Metro build, I got 'System.Windows.Interactivity.BehaviorCollection' error when loading the xaml page. The application starts removing the EnableDWMDropShadow="True" attribute but the MFC is not shown anymore

spiovesan commented 10 years ago

Any idea how to align the current release with the MahApps.Metro-make-windowchrome-not-suck branch? Thanks.

AzureKitsune commented 10 years ago

If I remember correctly, that branch was merged into the main code months ago.

EDIT: If you /need/ that specific branch /that/ badly, I can bring the branch back but it won't be updated should you find a bug in it since it's been merged. If we were to fix a bug, we would create a new branch.

spiovesan commented 10 years ago

I posted here a sample @ https://github.com/spiovesan/GithubSamples/tree/master/MyWindowsFormControlProj. that was working with that branch. Just referencing the latest MahApps.Metro build the sample does not work anymore. I get 'System.Windows.Interactivity.BehaviorCollection' error when loading the xaml page. I would like to use latest mahapps. It is not possible making the branch behavior working within the main code?

AzureKitsune commented 10 years ago

@spiovesan That issue is because of:

<behaviours:BorderlessWindowBehavior ResizeWithGrip="True" EnableDWMDropShadow="True" />

In order for DropShadow to be enabled, AllowsTransparency must be False.

<behaviours:BorderlessWindowBehavior ResizeWithGrip="True" EnableDWMDropShadow="True" AllowsTransparency="False" />

EDIT: Also, recent changes have renamed "Colours.xaml" to "Colors.xaml".

EDIT2: Setting AllowsTransparency to false allows your control to render. This is maybe related to the airspace issue: #488 capture2

spiovesan commented 10 years ago

Great! Thank you for your help.