[중요1] Graphics 개체를 사용한다.
[중요2] 다음의 헤더선언을 꼭 해야된다
#include <gdiplus.h> using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib") // <- 이부분은 한번반 선언하면 됨. 따라서 stdafx.cpp같은데 두면 좋음
[중요3] ULONG_PTR m_gdiplusToken; // Global로 선언
// 초기화를 위해 최소 한번 실행 // 이부분은 GDI 개체, 즉 DC로 만들어지는 개체가 생기기 전에 생성되면 된다. // 예를 들어 다이얼로그 선언부 전에 선언하면 좋다. Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup( &m_gdiplusToken, &gdiplusStartupInput, NULL);
// Exit Instance에 실행 GdiplusShutdown(m_gdiplusToken);
1. Graphics 개체 만드는 방법
(1) 폼 또는 컨트롤의 Paint 이벤트에서 PaintEventArgs의 일부로서 Graphics 개체에 대한 참조를 받는다. 이 방법은 대개 컨트롤을 그리는 코드를 작성할 때 Graphics 개체에 대한 참조를 가져오는 데 사용된다.
private: System::Void Form1_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * pe) { // Declares the Graphics object and sets it to the Graphics object // supplied in the PaintEventArgs. Graphics * g = pe->Graphics; // Insert code to paint the form here. }
(2) 폼 또는 컨트롤의 CreateGraphics 메서드를 호출하여 해당 폼이나 컨트롤의 그리기 화면을 나타내는 Graphics 개체에 대한 참조를 가져온다. 이미 있는 폼이나 컨트롤에서 그리려는 경우에 이 방법을 사용한다.
방법1)
Graphics * g; g = this->CreateGraphics();
방법2)
// OnPaint() 이벤트에서 CPaintDC dc(this); Graphics g(dc.m_hDC);
(3) Image에서 상속된 개체에서 Graphics 개체를 만든다. (가장 간단) 이 방법은 기존 이미지를 변경하려는 경우에 유용.
Bitmap * myBitmap = new Bitmap(S"D:Documents and SettingsJoePicsmyPic.bmp"); Graphics * g = Graphics::FromImage(myBitmap);
예제) ::OnPaint() { CPaintDC dc(this); Graphics g(dc.m_hDC);
CRect rect; GetClientRect( &rect); Image image(L"image1.jpg"); Rect rct( 0, 0, rect.right, rect.bottom); g.DrawImage( &image, rct.X, rct.Y, rct.Width, rct.Height); } |