在 VC6 中使用 GdiPlus-使用

來源:互聯網
上載者:User

標籤:

下面用 VC6 來寫一個 GdiPlus 的 Demo 工程

 

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 學習。

在 VC6 中使用 GdiPlus-使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.