Below use VC6 to write a GdiPlus of the Demo project
STEP1: Create a new MFC AppWizard (EXE) project named Demo_gdiplus
Operation Steps:
(1) Main Menu file->new ..., select the Projects tab;
(2) In the Project type list, select MFC AppWizard (EXE);
(3) Project name fills in the Demo_gdiplus, press OK to go to the next page;
(4) Select the single document type of program framework and complete the project creation work by finish.
STEP2: Add header File Declaration
Add the following code in StdAfx.h:
{{Afx_insert_location}}
Microsoft Visual C + + would 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: Add member Variable M_gdiplustoken in Cdemo_gdiplusapp and initialize in constructor
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: Add code to install and uninstall GdiPlus
Adding member functions to Cdemo_gdiplusapp by ClassWizard
declarations in. h
Virtual BOOL InitInstance ();
virtual int exitinstance ();
Implementations in the. cpp
BOOL cdemo_gdiplusapp::initinstance ()
{
Load 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
Uninstalling GdiPlus
if (M_gdiplustoken)
Gdiplus::gdiplusshutdown (M_gdiplustoken);
return CWinApp::ExitInstance ();
}
STEP5: Find the Cdemo_gdiplusview::ondraw () function and add a GdiPlus drawing code inside
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 < n; x + = 5)
{
Graphics. DrawLine (&blue, 0, y, x, 0);
Graphics. DrawLine (&red, n, x, y, 256);
Y-= 5;
}
}
Compile and run, Demo program completed. Keep this Demo program, we will use it for more in-depth GdiPlus learning in the future.
Using gdiplus-in VC6