Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

So, I'm trying to create a win32 application with a nice UI, and after some research decided that WPF is probably the way to go (as I used to work with MFC based UIs in the past when doing win32 apps). I'm a C/C++ developer, so I prefer developing in Visual Studio 2012, as the 'BLEND' tool with VS2012 is quite easy to do WPF xaml style development for the UI.

Before doing anything creative, I wanted to make a quick 'test' app to see if what I wanted to do is even possible....

I started off with a super simple setup, just made a generic win32 application right from VS2012, then went into BLEND and made a real simple WPF control library, just a plain background with one single 'button'. I created the action for the 'button click', then compiled the C# code into a DLL (for .NET 3.5).

From following the Microsoft examples for including WPF into win32 apps, I was able to reference in the WPF dll fine, and doing the hwndsource setup, load the WPF window within the dialog box of the main win32 app.


My questions are these:

1) Is it even possible to hook an event handler in my win32 app, to the WPF event for that button 'click' action, when using the WPF DLL in this
particular setup? (where I'm importing the compiled DLL as a reference in my win32 app)

2) Or Do I need to setup message pumps between the two, to pass msgs back and forth?

3) Or, is my only option to write this WPF app in c code, and compile it directly into the win32app, so I can setup the event handlers that way?
(as Microsoft does in their 'win32 with WPF control example' (address book example) ???





My WPF xaml for the layout is this simple:

HTML
[code]

<UserControl x:Class="WpfControlLibrary1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="516" Width="616">
	<UserControl.Background>
		<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
			<GradientStop Color="Black" Offset="0"/>
			<GradientStop Color="#FF7FB2FB" Offset="1"/>
		</LinearGradientBrush>
	</UserControl.Background>
    <Grid>
    	<Button x:Name="IDC_BUTTON" Content="Button" HorizontalAlignment="Left" Height="79" Margin="222,188,0,0" VerticalAlignment="Top" Width="185" Click="ButtonClick"/>
            
    </Grid>
</UserControl>

[/code]



and the C# code is this simple:

C#
[code]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary1
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void ButtonClick(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
        }
    }
}

[/code]



Thanks so much everyone in advance, for any help or advice you can provide!!

- Mike
Posted
Comments
Sergey Alexandrovich Kryukov 4-Jul-14 21:52pm    
The question is quite clear, but first, my question: why using raw Windows API at all? What is that so special effect that you cannot done in WPF? If the only reason is your better familiarity with Windows than WPF, the conglomerate you are thinking of will never pay off.

Don't get me wrong: I'm the one who really used Windows API with WPF, but it was pretty exotic case of functionality which is really not available in WPF.

If you are still interested: a quick hint: the only Window in WPF is the top window. Everything inside it is never an object which handles any Windows messages. From the standpoint of Windows API, a WPF window is one monolithic piece.

—SA
MikeS_BelAirMd 4-Jul-14 22:33pm    
Hi Sergey, Thanks for the response....

So, reason I'm using a win32app as the 'main' app, is that the real app I want to put together, involves a whole bunch of C code I already have, which is quite heavy in real raw binary data parsing, down at the bit level, plus on top of that I'll be also doing some interaction with some tcl scripts (through tcl library/boost)....

So it looked to me that WPF could provide a nice looking/spiffy UI (as the apps I made in the past like this, I did the UI with MFC and boring/bland dialog boxes/buttons).

Yeah, so I understand the part of WPF being one monolothic window...but you can still add control pieces on it, it buttons, list boxes, etc....I'm just trying to figure out how to actually interact with those pieces....

microsoft does have a nice example that does exactly this, but they wrote the WPF part in c code, and compiled it all at once with their win32app code....I can take this route, but it means having to write my WPF design by hand back into c code...

(ie this page: http://msdn.microsoft.com/en-us/library/ms744829%28v=vs.110%29.aspx )
this page has more snippets than the whole project, but there is another link by searching that has the whole 'example' to download, just can't find it at the moment)

I was hoping I could use BLEND (or something similar) to design a nice UI, and go from there, but it sounds that approach will not be possible...
Sergey Alexandrovich Kryukov 4-Jul-14 23:08pm    
Controls you add to WPF window do not participate in message pumping at all. From the standpoint of Windows, they don't exist as objects. I think you know that. You see, you should thoroughly isolate UI from non-UI and make UI strictly WPF. Everything else will give you a lot more trouble than gain. Are you trying to reuse Windows UI? Almost all UI reuse these days is a marketing myth; it is only reused on the base of the same framework...
—SA

1 solution

The more I looked at your situation, the more I see something which looks pointless to me. No, I'm not talking about using your C/C++ code; you explained something I expected to hear from you. So, what is that? Ah, of course. Using C#. Why would you even do that, if you essential code is C++.

You should not use C#; you should not use C++. You should use C++/CLI, which allows to freely combine managed and unmanaged code in one executable module (using so called "mixed-mode" project). The obtained module can be a regular DLL, but, from the .NET standpoint, it can also be seen as a main executable module of quite a regular .NET assembly, which can be referenced in any other .NET assembly, or otherwise used on CLI way. (To export to .NET, you will need to use managed "ref" classes/structures which can wrap unmanaged code.)

Unfamiliar with C++/CLI? If so, you will learn it, based on your knowledge of C# and C++, much faster then development of even the minimal project requiring the use of unmanaged modules in managed assemblies. You will be able to reuse all your C/C++ code and use WPF, as well as any other .NET libraries. The language is standardized, under the ECMA 372 standard.

How about that?

See also:
http://en.wikipedia.org/wiki/C++/CLI[^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^].

—SA
 
Share this answer
 
Comments
MikeS_BelAirMd 7-Jul-14 22:06pm    
Hi Sergey,

I was actually doing a setup that is pretty much what you are describing, the 'win32app' I setup, I did setup as a 'mixed-mode' app, with CLI enabled. The only reason I showed the "C#" and "xaml" code, is the microsoft 'BLEND' tool (I was using to create the simple WPF window w/button) develops what you create in C# and XAML... I just pasted the code in my question to show the simple setup of what I was creating in the BLEND WPF tool... but the C# code I was not actually coding myself, the BLEND tool does all that for you...

but I compiled the final WPF solution into a .NET (version 3.5) DLL, which I did import into the win32 C++/CLI app as a 'reference' DLL. So I think what I was doing is pretty much what you are describing above. The windows examples I was following describe that exact situation, including 'managed' code into your 'unmanaged' app....

I chose to use WPF, because it seemed that the UI capabilities of WPF are much more elegant and creative than what you can do with straight up .NET stuff...?

So right now I'm testing with the microsoft 'win32hostingwpf' example, which is a fully function 'address book' sample, that is a win32app w/CLI, but instead of importing a WPF managed code DLL, they have the WPF code all written up in C, so not exactly what I wanted to do, but somewhat close...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900