MFC 之 GDI

MFC程序,使用GDI绘图

//矢量图和位图的区别


//介绍DC、了解DC
//CBrush brush(RGB(255,0,0));
//CClientDC dc(this->GetParent()->GetParent());
//CWindowDC dc(this);
//dc.FillRect(CRect(0,0,500,500),&brush);

//基本图形的绘制
//线段
pDC->MoveTo(20,30);
pDC->LineTo(300,30);
//矩形
pDC->Rectangle(20,50,300,200);
//椭圆
pDC->Ellipse(20,50,300,200);


CPen pen, *pOldPen;
CBrush brush, *pOldBrush;
//1.使用画笔
pen.CreatePen(PS_SOLID,6,RGB(255,0,0));
pOldPen=pDC->SelectObject(&pen);
pDC->MoveTo(20,250);
pDC->LineTo(300,250);
pDC->SelectObject(pOldPen);

//2.使用画刷(填充)
//创建一个蓝色的画刷
brush.CreateSolidBrush(RGB(0,0,255));
pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(20,300,300,400);
pDC->SelectObject(pOldBrush);

//创建一个蓝色的网格画刷
brush.DeleteObject();
brush.CreateHatchBrush(HS_CROSS,RGB(0,0,255));
pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(20,420,300,520);
pDC->SelectObject(pOldBrush);

//创建一个位图画刷
brush.DeleteObject();
CBitmap bitmap_brush;
bitmap_brush.LoadBitmap(IDB_BITMAP1);
brush.CreatePatternBrush(&bitmap_brush);
pOldBrush=pDC->SelectObject(&brush);
pDC->Rectangle(20,540,300,640);
pDC->SelectObject(pOldBrush);

//空画刷
brush.DeleteObject();
brush.CreateStockObject(NULL_BRUSH);
pDC->Rectangle(20,660,300,760);
pDC->SelectObject(pOldBrush);

//3.使用位图
//定义位图
CBitmap bitmap;

//保存位图信息;
BITMAP bmp;

//建立兼容设备
CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);

//从资源中装入位图
bitmap.LoadBitmap(IDB_BITMAP1);

//将位图选入设备
dcCompatible.SelectObject(&bitmap);

//获取位图信息
bitmap.GetBitmap(&bmp);

//拷贝
pDC->BitBlt(600,0,bmp.bmWidth,bmp.bmHeight,&dcCompatible,0,0,SRCCOPY);


//从文件中获取位图
HBITMAP hBitmap;
hBitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),
"res\\ball.bmp", //实际位图文件的路径
IMAGE_BITMAP,0, 0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);//从文件中装入位图
bitmap.Detach();
bitmap.Attach(hBitmap);
bitmap.GetBitmap(&bmp);

dcCompatible.SelectObject(&bitmap);

//绘制非透明位图(将dcCompatible上的内容拷贝到pDC中)
pDC->BitBlt(600,40,bmp.bmWidth,bmp.bmHeight,&dcCompatible,0,0,SRCCOPY);
pDC->StretchBlt(600,100,bmp.bmWidth*2,bmp.bmHeight*2,&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);

//绘制透明位图(将dcCompatible上的内容拷贝到pDC中,同时去掉最后一个参数指定的颜色)
pDC->TransparentBlt(600,200,bmp.bmWidth,bmp.bmHeight,&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,RGB(255,0,0));


//4.输出文字
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkMode(TRANSPARENT);

CFont font;//当前字体
font.CreateFont(32,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH | FF_SWISS,"宋体");

CFont *pOldFont=pDC->SelectObject(&font);

pDC->TextOut(50,320,"输出文字");
CSize cs;
cs=pDC->GetTextExtent("输出文字");
pDC->TextOut(50+cs.cx,320,"abcdefg");
pDC->SelectObject(pOldFont);

//复杂图形
int step=30;
int center_x=700;
int center_y=500;
float x,y;
x=center_x-300;
y=sin(x/600*6*3.14)*100;
pDC->MoveTo(x,center_y+y);
while (x<center_x+300)
{
y=sin(x/600*6*3.14)*100;
pDC->LineTo(x,center_y+y);
x+=step;
}

SDK 程序

#include <windows.h>
#include <stdio.h>

//声明窗口过程函数
LRESULT CALLBACK WinProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

//入口函数
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
//设计窗口类
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinProc;
wndcls.lpszClassName="win32";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;

//注册窗口类
RegisterClass(&wndcls);

//创建窗口
HWND hwnd;
hwnd=CreateWindow("win32","wind32程序",WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);

//显示和更新窗口
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

//处理消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

//定义窗口过程函数
LRESULT CALLBACK WinProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,szChar,"win32",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","win32",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,"LbutonDown",strlen("LbutonDown"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0,"win32程序",strlen("win32程序"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","win32程序",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

 

 

GDI+配置步骤:

(1)、在stdafx.h文件中加入下面3行代码
#pragma comment( lib, "gdiplus.lib" )
#include "gdiplus.h"
using namespace Gdiplus;

(2)、在App类中,添加一个成员变量,如下列代码:
ULONG_PTR m_gdiplusToken;
其中,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。

(3)、在应用类的InitInstance函数中添加GDI+的初始化代码:
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
(4)、在应用类中添加ExitInstance的重载,并添加下列代码用来关闭GDI+:
  Gdiplus::GdiplusShutdown(m_gdiplusToken);

GDI+基本绘图方法

//建立GDI+绘图对象
Graphics g( pDC->GetSafeHdc() );
//建立画笔
Pen pen_black(Color::Black);
Pen pen_white(Color::White);
//使用画笔绘制图形
pen_black.SetWidth(6); //设置画笔宽度
pen_black.SetStartCap(LineCapRoundAnchor); //设置开始笔帽
pen_black.SetEndCap(LineCapArrowAnchor); //设置结束笔帽
g.DrawLine(&pen_black, 10, 10, 100, 10);
Rect rect1(0,0, 150, 80);
Rect rect2(10 + 170, 200, 150, 80);
Rect rect3(10 + 170*2, 200, 150, 80);
Rect rect4(10 + 170*3, 200, 150, 80);
g.DrawRectangle(&pen_black, rect1);
g.DrawRectangle(&pen_black, rect2);
g.DrawRectangle(&pen_black, rect3);
g.DrawRectangle(&pen_black, rect4);
//建立画刷
//实色画刷
SolidBrush brush_black(Color::Black);
SolidBrush brush_white(Color::White);
SolidBrush brush_blue(Color::Blue);
g.FillRectangle(&brush_blue, rect1);
//网格画刷
HatchBrush brush_hatch( HatchStyleDiagonalBrick, Color(255, 255, 0, 0), Color(30, 0, 255, 0));
g.FillRectangle(&brush_hatch, rect2);
//贴图画刷
Image image(L"res\\qq.gif");
TextureBrush brush_texture(&image);
g.FillRectangle(&brush_texture, rect3);
//渐变画刷(线形渐变)
LinearGradientBrush brush_gradient_line( Point(0, 0), Point(10, 10),Color(255, 255, 0, 0), Color(255, 0, 0, 255));
g.FillRectangle(&brush_gradient_line, rect4);
//渐变画刷(路径渐变)
//PathGradientBrush......................
//贴图画笔
Pen pen_texture(&brush_texture, 30);
g.DrawEllipse(&pen_texture, 600, 10, 150 ,150);
//启动抗锯齿功能
pen_black.SetWidth(1);
g.SetSmoothingMode(SmoothingModeAntiAlias);
g.DrawLine(&pen_black, 150, 5, 350 , 20);
//绘制图像
// 不进行缩放
g.DrawImage(&image, 10,50);
// 使用低质量的插补算法
g.SetInterpolationMode(InterpolationModeNearestNeighbor);
g.DrawImage( &image, Rect(100,50, 100, 100));
// 使用中等质量的插补算法
g.SetInterpolationMode(InterpolationModeHighQualityBilinear);
g.DrawImage( &image, Rect(250,50, 100, 100));
// 使用高质量的插补算法
g.SetInterpolationMode(InterpolationModeHighQualityBicubic);
g.DrawImage( &image, Rect(400,50, 100, 100));
//路径
GraphicsPath path1;
path1.AddLine(300, 350, 500 ,350);
path1.AddArc(300, 300, 200, 100, 0, -180);
g.DrawPath(&pen_black, &path1);
g.FillPath(&brush_black, &path1);
//区域
GraphicsPath pathOuter;
pathOuter.AddRectangle(Rect(100, 320, 150 ,150));
GraphicsPath pathInner;
pathInner.AddEllipse(Rect(150, 360, 90 ,80));
Region rgn(&pathOuter);
rgn.Exclude(&pathInner);
g.FillRegion(&brush_blue, &rgn);

 

 

FROM:http://blog.sina.com.cn/s/blog_493309600100cvmd.html

 

 

你可能感兴趣的:(image,null,mfc,Path,callback,GDI+)