深入浅出CChart 每日一课——快乐高四第三十九课 再见江湖,GNU环境之MingW窗口程序绘图

下面继续GNU环境编程。

这里,演示一下用MingW编制一个Win32窗口程序,并在窗口里用CChart绘制等高线图。

窗口程序相对于上一节课的控制台程序,相对要复杂一些,为了方便编程,特别是为了能生成一个程序模板以供我们进一步修改,这里使用了Code::Blocks这个IDE。

第一步,打开上一节课二次包装CChart库的Dll工程文件,在ChartWrapper.cpp里面,输入下面的代码,然后编译出CChartWrapperU.dll待用。

#pragma comment(linker, "/export:_CreateChart=CChartu.?CreateChart@Classless@@YAXXZ")
#pragma comment(linker, "/export:_DestroyChart=CChartu.?DestroyChart@Classless@@YAXXZ")
#pragma comment(linker, "/export:_Attach=CChartu.?Attach@Classless@@YAHPAUHWND__@@@Z")
#pragma comment(linker, "/export:_SetType=CChartu.?SetType@Classless@@YA_NH@Z")
#pragma comment(linker, "/export:_SetFieldFcn=CChartu.?SetFieldFcn@Classless@@YAXP6ANNN@Z@Z")
#pragma comment(linker, "/export:_SetPlotRange=CChartu.?SetPlotRange@Classless@@YAXNNNN@Z")
#pragma comment(linker, "/export:_SetTitle=CChartu.?SetTitle@Classless@@YAXPB_W@Z")
#pragma comment(linker, "/export:_ReDraw=CChartu.?ReDraw@Classless@@YAXXZ")

这里转发了八个需要用到的函数。

第二步,打开Code::Blocks,点击菜单File->New->Project…,在弹出的对话框里选择Win32 GUI Project,然后继续,建立一个名为GccWnd的项目。

第三步,修改编译目标。点击菜单Project->Properties…,进入Build Targets选项卡,把Type由Console application改变为GUI application。

这时系统自动产生的代码如下。

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

这些代码比上节课复杂多了!如果手工写,太费劲了,所以才需要使用IDE。

直接编译,效果如图。

第四步,拷贝CChartWrapperU.dll,CChartWrapperU.lib和CChartu.dll到GccWnd目录下面。

第五步,设置链接到CChartWrapperU.lib。点击菜单Project->Build options…,在弹出的对话框里,左边选择Debug,右边在Linker Settings选项卡里,点击Add按钮。如下。在相对路径选择的对话框里,选择“否”,不使用相对路径。

第六步,声明要用到的函数。

extern "C" void		CreateChart();
extern "C" void		DestroyChart();
extern "C" int		Attach(HWND hWnd);
extern "C" void     SetType(int nType);
extern "C" void		SetFieldFcn(double (*pFieldFcn)(double x, double y));
extern "C" void     SetPlotRange(double x1, double x2, double y1, double y2);
extern "C" void		SetTitle(const wchar_t* title);
extern "C" void		ReDraw();

第七步,添加等高线图的场函数。

double field(double x, double y)
{
    return 1.0/(x*x+y*y+1.0);
}

第八步,增加WM_CREATE、WM_SIZE消息响应,并修改WM_DESTROY消息的响应。

        case WM_CREATE:
            CreateChart();
            Attach(hwnd);
            SetType(6);
            SetFieldFcn(field);
            SetPlotRange(-2, 2, -3, 3);
            SetTitle(L"GCC标题");
            break;
        case WM_SIZE:
            ReDraw();
            break;
        case WM_DESTROY:
            DestroyChart();
            PostQuitMessage (0);
            break;

第八步,点击菜单Project->Build options…,在Other compiler options选项卡里,添加:-finput-charset=GBK。添加这个选项的目的是支持中文字符串,和上节课命令行中的选项一样。

编译并运行,如图所示。

好了,结束!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值