Posted by: Zeeshan Amjad | September 1, 2009

Call unmanaged Dialog box from WPF


We can call the unmanaged code from the WPF. There is a namespace System.Windows.Interop define all the necessary classes and structure for this purpose.

If we are working in VC++ then we can easily mix managed and unmanaged code. First we have to create an object of WindowInteropHelper class and then get the handle of window i.e. HWND. Once we get the HWND then we can use it with unmanaged code. Here is a code to get the HWND within WPF.

  1: WindowInteropHelper^ helper = gcnew WindowInteropHelper(this);
  2: HWND hwnd = (HWND)helper->Handle.ToPointer();
  3: 

Here is a sample code to demonstrate this. Here we create a simple WPF window and create one button inside it. When user press that button then it will popup one unmanaged dialog from it.

  1: #include <windows.h>
  2: #include "resource.h"
  3: using namespace System;
  4: using namespace System::Windows;
  5: using namespace System::Windows::Media;
  6: using namespace System::Windows::Controls;
  7: using namespace System::Windows::Interop;
  8: 
  9: HINSTANCE gInst;
 10: LRESULT CALLBACK MyDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 11: 
 12: public ref class MyWindow : public Window
 13: {
 14: public:
 15: 	MyWindow()
 16: 	{
 17: 		Title = "Unmanaged DialogBox Demo";
 18: 		Width = 400;
 19: 		Height = 300;
 20: 
 21: 		Grid^ grid = gcnew Grid();
 22: 
 23: 		Windows::Controls::RowDefinition^ row = gcnew Windows::Controls::RowDefinition();
 24: 		Windows::Controls::ColumnDefinition^ col = gcnew Windows::Controls::ColumnDefinition();
 25: 
 26: 		Button^ btn = gcnew Button();
 27: 		btn->Content = "Call Dialog";
 28: 		btn->Margin = Thickness(10);
 29: 		btn->Click += gcnew RoutedEventHandler(this, &MyWindow::OnButton);
 30: 
 31: 		grid->SetRow(btn, 0);
 32: 		grid->SetColumn(btn, 0);
 33: 
 34: 		grid->RowDefinitions->Add(row);
 35: 		grid->ColumnDefinitions->Add(col);
 36: 
 37: 		Content = grid;
 38: 
 39: 		grid->Children->Add(btn);
 40: 	}
 41: 
 42: 	void OnButton(Object^ sender, RoutedEventArgs^ e)
 43: 	{
 44: 		WindowInteropHelper^ helper = gcnew WindowInteropHelper(this);
 45: 		HWND hwnd = (HWND)helper->Handle.ToPointer();
 46: 		DialogBox(gInst, MAKEINTRESOURCE(IDC_DIALOG), hwnd, (DLGPROC)MyDialogProc);
 47: 	}
 48: };
 49: 
 50: LRESULT CALLBACK MyDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 51: {
 52: 	switch(uMsg)
 53: 	{
 54: 	case WM_COMMAND:
 55: 		switch(LOWORD(wParam)) 
 56: 		{
 57: 			case IDC_CLOSE:
 58: 				EndDialog(hWnd, LOWORD(lParam));
 59: 				break;
 60: 		}
 61: 	}
 62: 
 63: 	return FALSE;
 64: }
 65: 
 66: [STAThread]
 67: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
 68: 				   LPSTR lpCmd, int nCmd)
 69: {
 70: 	gInst = hInstance;
 71: 
 72: 	MyWindow^ win = gcnew MyWindow();
 73: 
 74: 	Application^ app = gcnew Application();
 75: 	app->Run(win);
 76: }
 77: 

Here is the output of the program.

vcwpfUnmanaged_01

When user press this button then one dialog box will popup. Here is the output of it.

vcwpfUnmanaged_02

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: