深入浅出CChart 每日一课——快乐高四第五十一课 蹒跚学步,轻松自在之Win32++

WIN32++是封装了Win32API的C++项目。项目地址:http://sourceforge.net/projects/win32-framework/files/?source=navbar。虽然名字是Win32,实际上32位和64位都是支持的。

Win32++实际上是Win32API的一个轻度封装,封装有点模仿MFC,但要轻量级很多。

前些日子群里有个朋友问CChart能不能在Win32++下使用,其实笨笨有点无语,在更复杂的环境下都可以,这里当然没有任何问题啊。当然,在陌生环境中,有可能遇到新的问题,因此笨笨就尝试了一下。

要使用这个库,首先当然是要正确安装Win32++了。到官网上下载最新的版本,笨笨下载的文件是Win32xx891.zip。把这个文件解压到合适的地方,再在Visual Studio的IDE中把其中的include和lib文件夹加入到编译环境就可以了。

第一步,建立一个Win32 Application的空工程LessonA51。

第二步,找到Win32xx的Tutorials目录,直接把里面第二个示例的main.cpp文件拷贝过来,并加入到LessonA51。

这个文件的内容不长,全部如下。

///
// 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
{
public:
    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
{
public:
    CSimpleApp() {}
    virtual ~CSimpleApp() {}
    virtual BOOL InitInstance();
 
private:
    CView m_view;
};
 
// Called when the application starts.
BOOL CSimpleApp::InitInstance()
{
    // Create the Window
    m_view.Create();
 
    return TRUE;
}
 
 
// WinMain is the program's entry point. The program starts here.
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    // Start Win32++
    CSimpleApp theApp;
 
    // Run the application
    return theApp.Run();
}

第三步,直接编译会出编译和链接错误,需要做一些修改。

解决编译错误,需要在C++的属性里面把运行时修改为多线程的,如Multithreaded,或Debug Multithreaded,都可以。

解决链接错误,需要在#include "wxx_wincore.h"这一句话后面,增加一行。

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

现在可以编译了,运行效果如下。

 

就是一个空窗口!

第四步,加入CChart的引用,并加入math.h的引用。

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

第五步,在CView类里面增加一个变量和一个函数。

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

这里OnCreate是Win32++已经内置的一个虚拟函数,我们重载一下就好了。这个函数的作用和MFC的OnCreate一样,但参数是CREATESTRUCT的引用,而不是指针。

第六步,实现OnCreate函数。

int	CView::OnCreate(CREATESTRUCT& cs)
{
	chartWnd.Attach(GetHwnd());
	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和Win32++"));
	return 0;
}

OK,好了,效果如图。

不过呢,目前还有两个问题。一是窗口不能响应鼠标双击消息,二是窗口大小调整时图像不能实时更新。下面就来逐一解决。

第七步,在CView类中重载PreRegisterClass函数,解决鼠标双击问题。

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

运行一下,似乎没有效果,什么原因呢?

其实,这是Win32++库本身的Bug,毕竟这个库就是个人作品,不能和MFC那样成熟的商业产品相比,要求不要太高了哇!

打开打开Win32++库include文件夹下wxx_wincore.h文件,找到CWnd::CreateEx函数,并找到if (RegisterClass(wc) == 0)这一行,在其前面添加这样一句话。

PreRegisterClass(wc);

这样修改Win32++库以后,鼠标双击完全没有问题了。

第八步,解决窗口大小调整重绘的问题。

在MFC中,这直接可以在OnSize中处理,检查了一遍Win32++的源码,发现其并没有提供OnSize函数。怎么办呢?

别慌,我们还有终极大招!Win32++提供了窗口函数的重载。

在CView类中增加一个成员函数。

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

实现这个成员函数就很简单了。

LRESULT CView::WndProc(UINT msg, WPARAM wparam, LPARAM lparam)
{
	if(msg == WM_SIZE)
	{
		chartWnd.ReDraw();
	}
	return CWnd::WndProc(msg, wparam, lparam);
}

现在功能完全正常!!!

使用C++代码封装的win32操作类, 与MFC相似,对于学习SDK与C++是巨好的参考 Tutorials Menu of tutorials Tutorial 1: The Simplest Window Tutorial 2: Using Classes and Inheritance Tutorial 3: Using Messages to Create a Scribble Window Tutorial 4: Repainting the Window Tutorial 5: Wrapping a Frame around our Scribble Window Tutorial 6: Customising Window Creation Tutorial 7: Customising the Toolbar Tutorial 8: Loading and Saving Files Tutorial 9: Printing Tutorial 10: Finishing Touches Tutorial 1: The Simplest Window The following code uses Win32++ to create a window. This is all the code you need (in combination with Win32++) to create and display a simple window. Note that in order to add the Win32++ code to our program, we use an #include statement as shown below. #include "../Win32++/Wincore.h" INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { //Start Win32++ CWinApp MyApp; //Create a CWnd object CWnd MyWindow; //Create (and display) the window MyWindow.Create(); //Run the application return MyApp.Run(); } This program has four key steps: Start Win32++. We do this here by creating a CWinApp object called MyApp. Create a CWnd object called MyWindow. Create a default window by calling the Create function. Start the message loop, by calling the Run function. If you compile and run this program, you'll find that the application doesn't end when the window is closed. This is behaviour is normal. An illustration of how to use messages to control the windows behaviour (including closing the application) will be left until tutorial 3.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值