CChart daily lesson - Happy senior four lesson 51 toddler, easy Win32++

WIN32 + + is a C + + project that encapsulates the WIN32 API. Project address: . Although the name is Win32, both 32-bit and 64 bit are supported.

Win32 + + is actually a light package of Win32 API. The package imitates MFC, but it is much lighter.

A friend in the group asked CChart if it could be used in Win32 + + a few days ago. In fact, it is stupid and a little speechless. It can be used in more complex environments. Of course, there is no problem here. Of course, in a strange environment, you may encounter new problems, so clumsy tried.

To use this library, you must first install Win32 + + correctly. Download the latest version from the official website. The downloaded file is Unzip the file to a suitable place, and then add the include and lib folders to the compilation environment in the IDE of Visual Studio.

The first step is to create an empty project LessonA51 of Win32 Application.

Step 2: find the Tutorials directory of Win32xx, directly copy the main.cpp file of the second example and add it to LessonA51.

The content of this file is not long. It is all as follows.

// main.cpp
// Note:
//  * Add the Win32++\include  directory to project's additional include directories
#include "wxx_wincore.h"
// CView is the application's main window.
class CView : public CWnd
    CView() {}
    virtual void OnDestroy() { PostQuitMessage(0); } // Ends the program
    virtual ~CView() {}

// A class that inherits from CWinApp.
// It is used to run the application's message loop.
class CSimpleApp : public CWinApp
    CSimpleApp() {}
    virtual ~CSimpleApp() {}
    virtual BOOL InitInstance();
    CView m_view;
// Called when the application starts.
BOOL CSimpleApp::InitInstance()
    // Create the Window
    return TRUE;
// WinMain is the program's entry point. The program starts here.
    // Start Win32++
    CSimpleApp theApp;
    // Run the application
    return theApp.Run();

Third, direct compilation will lead to compilation and link errors, which need to be modified.

To solve compilation errors, you need to modify the runtime to Multithreaded in the properties of C + +, such as Multithreaded or Debug Multithreaded.

To solve the link error, add a line after the #include "wxx_wincore.h".

#pragma comment(lib, "comctl32.lib")

Now you can compile, and the running effect is as follows.


It's an empty window!

Step 4, add the reference of CChart and the reference of math.h.

#include "Chart.h"
#if defined(_UNICODE) || defined(UNICODE)
#	pragma comment(lib,"CChartu.lib")
#	pragma comment(lib,"CChart.lib")
using namespace NsCChart;
#include <math.h>

Step 5: add a variable and a function to the CView class.

	CChartWnd chartWnd;
	virtual int OnCreate(CREATESTRUCT& cs);

OnCreate here is a built-in virtual function in Win32 + +. Let's reload it. This function is the same as OnCreate in MFC, but the parameter is a reference to CREATESTRUCT, not a pointer.

Step 6: implement OnCreate function.

int	CView::OnCreate(CREATESTRUCT& cs)
	double pi = 4.0*atan(1.0);
	for(int i=0; i<360; ++i)
		chartWnd.GetChart()->AddPoint2D(i, 1.3*sin(i*2.0*pi/360.0));
	chartWnd.GetChart()->SetTitle(_T("CChart and Win32++"));
	return 0;

OK, OK, the effect is shown in the figure.

However, there are still two problems. First, the window cannot respond to the message of double clicking the mouse. Second, the image cannot be updated in real time when the window is resized. Let's solve it one by one.

Step 7: overload the PreRegisterClass function in the CView class to solve the problem of double clicking the mouse.

virtual void PreRegisterClass(WNDCLASS& wc){ |= CS_DBLCLKS;}

Running it seems to have no effect. What's the reason?

In fact, this is the Bug of the Win32 + + library itself. After all, this library is a personal work. It can't be compared with the mature commercial products like MFC. The requirements are not too high WOW!

Open wxx in the include folder of Win32 + + library_ In the wincore. H file, find the CWnd::CreateEx function, find the line if (RegisterClass(wc) == 0), and add such a sentence before it.


After modifying the Win32 + + library in this way, there is no problem with double clicking the mouse.

Step 8: solve the problem of window resizing and redrawing.

In MFC, this can be handled directly in OnSize. After checking the source code of Win32 + +, it is found that it does not provide OnSize function. What shall I do?

Don't panic, we have the ultimate trick! Win32 + + provides overloading of window functions.

Add a member function in CView class.

virtual LRESULT WndProc(UINT msg, WPARAM wparam, LPARAM lparam);

Implementing this member function is simple.

LRESULT CView::WndProc(UINT msg, WPARAM wparam, LPARAM lparam)
	if(msg == WM_SIZE)
	return CWnd::WndProc(msg, wparam, lparam);

Now the function is completely normal!!!

Tags: data visualization

Posted on Sat, 23 Oct 2021 05:39:55 -0400 by onegative