zoukankan      html  css  js  c++  java
  • 在 VC6 中使用 GdiPlus-使用

    下面用 VC6 来写一个 GdiPlus 的 Demo 工程

    Demo_GdiPlus 运行效果图

    Step1:新建一个名为 Demo_GdiPlus 的 MFC AppWizard(exe) 工程

    操作步骤:
    (1)主菜单File->New...,选择 Projects 选项卡;
    (2)在工程类型列表中选中 MFC AppWizard(exe);
    (3)Project name 填入 Demo_GdiPlus,按 OK 进入下一页;
    (4)选择单文档(Single document)类型的程序框架,按 Finish 完成工程创建工作。


    Step2:添加头文件声明

    在 StdAfx.h 中添加以下代码:

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    typedef unsigned long ULONG_PTR, *PULONG_PTR;
    #include <gdiplus.h>
    using namespace Gdiplus;
    #pragma comment (lib, "GdiPlus.lib")

    Step3:在 CDemo_GdiPlusApp 中增加成员变量 m_gdiplusToken,并在构造函数中进行初始化

    class CDemo_GdiPlusApp : public CWinApp
    {
    private:
     ULONG_PTR m_gdiplusToken;
     // …… ……
    };

    CDemo_GdiPlusApp::CDemo_GdiPlusApp()
    {
     // TODO: add construction code here,
     // Place all significant initialization in InitInstance
     m_gdiplusToken = NULL;
    }

    Step4:添加安装和卸载 GdiPlus 的代码
    通过 ClassWizard 在 CDemo_GdiPlusApp 中增加成员函数

    // .h 中的声明
    virtual BOOL InitInstance();
    virtual int ExitInstance();

    // .cpp 中的实现
    BOOL CDemo_GdiPlusApp::InitInstance()
    {
     // 加载 GdiPlus
     Gdiplus::GdiplusStartupInput gdiplusStartupInput;
     Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
     // …… ……
    }

     
    int CDemo_GdiPlusApp::ExitInstance() 
    {
     // TODO: Add your specialized code here and/or call the base class
     // 卸载 GdiPlus
     if (m_gdiplusToken)
      Gdiplus::GdiplusShutdown(m_gdiplusToken);  
     return CWinApp::ExitInstance();
    }

    Step5:找到 CDemo_GdiPlusView::OnDraw() 函数,在里面添加一段 GdiPlus 的绘图代码

    void CDemo_GdiPlusView::OnDraw(CDC* pDC)
    {
     CDemo_GdiPlusDoc* pDoc = GetDocument();
     ASSERT_VALID(pDoc);
     // TODO: add draw code for native data here
     
     Graphics graphics(pDC->GetSafeHdc());
     
     // Pen can also be constructed using a brush or another pen.  There is a second parameter - a width which defaults to 1.0f
     Pen blue (Color(255, 0, 0, 255));
     Pen red  (Color(255, 255, 0, 0));
     
     int y = 256;
     for (int x = 0; x < 256; x += 5)
     {
      graphics.DrawLine(&blue, 0, y, x, 0);
      graphics.DrawLine(&red, 256, x, y, 256);  
      y -= 5;
     }  
    }


    编译运行,Demo 程序完成。留住这个 Demo 程序,今后我们会利用它进行更加深入的 GdiPlus 学习。

  • 相关阅读:
    java语言中public、private、protected三个关键字的用法,重写和重载的区别。
    MVC模式中M,V,C每个代表意义,并简述在Struts中MVC的表现方式。
    abstract class和interface有什么区别?
    简单说明什么是递归?什么情况会使用?并使用java实现一个简单的递归程序。
    在项目中用过Spring的哪些方面?及用过哪些Ajax框架?
    简述基于Struts框架Web应用的工作流程
    List、Map、Set三个接口存储元素时各有什么特点?
    mybatis查询时间段sql语句
    Maven pom.xml中添加指定的中央仓库
    为什么需要RPC,而不是简单的HTTP接口
  • 原文地址:https://www.cnblogs.com/liangxiaofeng/p/4843203.html
Copyright © 2011-2022 走看看