zoukankan      html  css  js  c++  java
  • How to host win32 in wpf?

    1.       Create win32 Project

    2.       Modify project properties

    Configuration TypeàDynamic Library(.dll)
    Common Language Runtime Support
    àCommon Language Runtime Support(/clr)
    Output Directory
    àDebug\
    Intermidiate Directory->Debug\

     

    3.       Add clrMust Finish previous step!
    PresentationCore
    PresentationFramework
    WindowsBase
    System

     

    4.       If vs2008,may be need to add lib
    Error :  fatal error LNK1104: cannot open file 'MSCOREE.lib'        
    Solution:Tools->Options->Projects and Solutions->VC++ Directories->Win32/Libraray files
    à
    add [C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib]

     

    5.       Insert Dialog,and modify dialog properties.
    Contol->True
    Style->Child
    if no ,will error:host hwnd must by child window


    Open Win32Project.cpp ,and add code:
    namespace ManagedCpp

    {

     using namespace System;

        using namespace System::Windows::Interop;

        using namespace System::Runtime::InteropServices;

        public ref class MyHwndHost : public HwndHost {

        private:

            HWND dialog;

        protected:

            virtual HandleRef BuildWindowCore(HandleRef hwndParent) override {

                InitializeGlobals();          

                dialog = CreateDialog(hInstance,

                    MAKEINTRESOURCE(IDD_DIALOG1),

                    (HWND) hwndParent.Handle.ToPointer(),

                    (DLGPROC) About);

                   return HandleRef(this, IntPtr(dialog));

            }

     

            virtual void DestroyWindowCore(HandleRef hwnd) override {

                // hwnd will be disposed for us

            }

        };

    }

     

    6.       Modify Code:
    **replace   BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

    With  void InitializeGlobals()

    Add:

    bool initialized = false;

        void InitializeGlobals() {

            if (initialized) return;

            initialized = true;

     

            // TODO: Place code here.

            MSG msg;

            HACCEL hAccelTable;

     

            // Initialize global strings

            LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

            LoadString(hInstance, IDC_TYPICALWIN32DIALOG, szWindowClass, MAX_LOADSTRING);

            MyRegisterClass(hInstance);

    }
    Remove:

    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

    {

       HWND hWnd;

     

       hInst = hInstance; // Store instance handle in our global variable

     

       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

     

       if (!hWnd)

       {

          return FALSE;

       }

     

       ShowWindow(hWnd, nCmdShow);

       UpdateWindow(hWnd);

     

       return TRUE;

    }

     

    Replace _tWinMain with dllMain

    Add:
                      
    #ifdef _MANAGED

    #pragma managed(push, off)

    #endif

     

    HINSTANCE hInstance;                                                        // current instance

     

    BOOL APIENTRY DllMain( HINSTANCE hInst,

                           DWORD  ul_reason_for_call,

                           LPVOID lpReserved

                                  )

    {

        hInstance = hInst;

        return TRUE;

    }

     

    #ifdef _MANAGED

    #pragma managed(pop)

    #endif

    Remove:
    int APIENTRY _tWinMain(HINSTANCE hInstance,

                         HINSTANCE hPrevInstance,

                         LPTSTR    lpCmdLine,

                         int       nCmdShow)

    {

     UNREFERENCED_PARAMETER(hPrevInstance);

     UNREFERENCED_PARAMETER(lpCmdLine);

     

           // TODO: Place code here.

     MSG msg;

     HACCEL hAccelTable;

     

     // Initialize global strings

     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

     LoadString(hInstance, IDC_WIN32PROJECT, szWindowClass, MAX_LOADSTRING);

     MyRegisterClass(hInstance);

     

     // Perform application initialization:

     if (!InitInstance (hInstance, nCmdShow))

     {

            return FALSE;

     }

     

     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT));

     

     // Main message loop:

     while (GetMessage(&msg, NULL, 0, 0))

     {

            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

            {

                   TranslateMessage(&msg);

                   DispatchMessage(&msg);

            }

     }

     

     return (int) msg.wParam;

    }

    Mayby have other error,please check it by youre self

    Build successed ,continue!

    7.       Host in wpf

    Create wpf application ,and add Reference

     

     

    8.       XAML, care namespace
    <Window x:Class="WpfApplication1.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:my="clr-namespace:ManagedCpp;assembly=Win32Project"

    Title="Window1" Height="310" Width="510">

    <Grid>

    <my:MyHwndHost Width="200" Height="200" />

    </Grid>

    </Window>

    9.       View:

     

    Finish!

  • 相关阅读:
    (III)AOP:第六节:通知
    (III)AOP:第八节:多切面运行顺序
    (IV)JdbcTemplate
    (III)AOP:第三节:AOP配置与使用
    (III)AOP:第十节:AOP 总结
    (III)AOP:第二节:AOP概述
    (III)AOP:第四节:切入点表达式
    (III)AOP:第九节:XML配置切面
    (III)AOP:第五节:当前连接点
    7月3号
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/1942674.html
Copyright © 2011-2022 走看看