Develop WPF using C ++

Source: Internet
Author: User

C ++ is used to develop WPF, mainly to host the page of WPF in the window of MFC (win32. The following is a detailed introduction.

1. Create a project. Because the MFC Wizard will generate a lot of codes that are not usable, I am going to create an MFC project from an empty project.

A) Open vs2005, choose File> New> projects ..., Select visual c ++-> Win32 on the left and Win32 console application on the right. Enter the project name cplusplus_wpf and click OK to go to the next step. See figure 1.

Figure 1 Create Project 1

B) Basic project configuration. In application setting, select console application and MFC .. Finish to go to the next step. See figure 2.

Figure 2 Application setting

C) modify the project to make it an MFC windows program.

I. Delete the cplusplus_wpf.cpp and cplusplus_wpf.h files.


Figure 3 Remove No used files

Ii. Add a cwinapp derived class, right-click the project, and add => Class...


Figure 4 Add New Class Menu

In the displayed dialog box, select "MFC" on the left, "MFC class" on the right, and click "add" to go to the next step.


Figure 5 create new class Dialog

In the displayed dialog box, enter the class name ccplusplus_wpfapp and select cwinapp as the base class.


Figure 6 Add cwinapp derived class

 

I. Add the cwnd derived class using the same method as above. The class name is ccplusplus_wpfmainwnd, and the base class is cwnd.

Ii. Modify project properties. Change System-> subsystem in the attribute from concole to Windows. See figure 7.


 

Figure 7 change property

In this step, the two classes cwinapp and cwnd Derived classes required by a basic MFC program are added. Our program can be compiled smoothly, but it cannot be run. Please continue to the next step.

D). Add code for ccplusplus_wpfmainwnd;
L add a window creation function. The function is as follows:

Bool ccplusplus_wpfmainwnd: createmainwnd (const crect & rect, DWORD dwstyle, DWORD dwstyleex)
{
Wndclass;
Memset (& wndclass, 0, sizeof (wndclass ));
Wndclass. Style = cs_hredraw | cs_vredraw | cs_dblclks;
Wndclass. lpfnwndproc =: defwindowproc;
Wndclass. hinstance = AfxGetInstanceHandle ();
Wndclass. hicon = NULL;
Wndclass. hcursor =: loadcursor (null, idc_arrow );
Wndclass. hbrbackground = (hbrush) (color_window );
Wndclass. lpszmenuname = NULL;
Wndclass. lpszclassname = _ T ("_ cplusplus_wpf_mainwnd __");

If (! Afxregisterclass (& wndclass ))
{
Return false;
}

Return cwnd: createex (dwstyleex, wndclass. lpszclassname, _ T ("C ++ & WPF"), dwstyle, 0, 0, rect. width (), rect. height (), null, null );
}

L inherits the postncdestroy function of the cwnd class. This function is a virtual function in the cwnd class. It is the last called function after the window exits. We also delete ourselves in this function.

Void ccplusplus_wpfmainwnd: postncdestroy ()
{
Delete this;
}

E) Add code for ccplusplus_wpfapp

L change the constructor from protect to public.

L define theapp, that is, ccplusplus_wpfap theapp

L implement initinstance ()

 

Bool ccplusplus_wpfapp: initinstance ()
{
Cwinapp: initinstance ();

Ccplusplus_wpfmainwnd * pmainwnd = new ccplusplus_wpfmainwnd ();
If (! Pmainwnd-> createmainwnd (crect (0, 0,800,600), ws_overlappedwindow, 0 ))
Return false;

M_pmainwnd = pmainwnd;

Pmainwnd-> centerwindow ();
Pmainwnd-> showwindow (sw_show );
Pmainwnd-> updatewindow ();

Return true;
}

Compile and run the program. You can see the window. At this point, a basic MFC program has been created. You can add the desired style to the window. I will not elaborate on it here.

2. Modify the Project to Support WPF

1. modify the project properties and select/CLR in the general common language runtime support of the project properties. This option allows the Project to support the common language runtime. in this way, we can write c ++/CLI code in our project to operate WPF.

Figure 8 support CLR
2. Add references and select Common Properties> references on the tree on the left of the Project Properties (C ++ programmers seem to have little chance to use it ).

Figure 9 Add references

Click Add new reference. The following dialog box is displayed.

 

 

Add the following. Net DLLs in sequence. to the current position, this project has fully supported WPF.

 
3. Add the display area of WPF
A) create a XAML file. I have uploaded one, clock. XAML, which can be found in the Windows SDK.
B) dynamically read The XAML File
C) Upload the page host of WPF to the window of MFC. The complete code is as follows:

Using namespace system;
Using namespace system: IO;
Using namespace system: windows;
Using namespace system: Windows: controls;
Using namespace system: Windows: markup;
Using namespace system: Windows: InterOP;

Void ccplusplus_wpfmainwnd: createwpfwnd ()
{
Filestream ^ pstream = file: openread ("clock. XAML ");
If (pstream = nullptr)
Return;

Page ^ pmainpage = (page ^) xamlreader: load (pstream );

Pstream-> close ();

Crect rcclient;
Getclientrect (& rcclient );

Rect ^ rect = gcnew rect (0, 0, rcclient. Width (), rcclient. Height ());

Hwndsourceparameters ^ sourceparams = gcnew hwndsourceparameters (gcnew string ("wpfwnd "));
Sourceparams-> positionx = int32 (rect-> X );
Sourceparams-> positiony = int32 (rect-> Y );
Sourceparams-> Height = int32 (rect-> height );
Sourceparams-> width = int32 (rect-> width );
Sourceparams-> parentwindow = system: intptr (m_hwnd );
Sourceparams-> windowstyle = ws_visible | ws_child | ws_clipchildren;

Hwndsource ^ source = gcnew hwndsource (* sourceparams );
If (Source = nullptr)
Return;

Source-> rootvisual = pmainpage;
}

D) Call the above function in ccplusplus_wpfmainwnd: oncreate.
E) Compile and run. Everything is OK.

Of course, this is only a basic step. On this basis, you can use C ++ and WPF to develop a complete software.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.