⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graview.cpp

📁 VC.net 的一些有用的技巧
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// graView.cpp : CgraView 类的实现
//
#include "gdipluscolormatrix.h"
#include "stdafx.h"
#include "gra.h"

#include "graDoc.h"
#include "graView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CgraView

IMPLEMENT_DYNCREATE(CgraView, CView)

BEGIN_MESSAGE_MAP(CgraView, CView)
END_MESSAGE_MAP()

// CgraView 构造/销毁

CgraView::CgraView()
{
	// TODO: 在此处添加构造代码

}

CgraView::~CgraView()
{
}

BOOL CgraView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
	// 样式

	return CView::PreCreateWindow(cs);
}

// CgraView 绘制

void CgraView::OnDraw(CDC* pDC)
{
	CgraDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: 在此处为本机数据添加绘制代码
	using namespace Gdiplus;
	Graphics myGraphics(pDC->m_hDC);
	Pen myPen(Color(255,0,0),3);
	
	//myGraphics.TranslateTransform(100, 50);
	//myGraphics.DrawLine(&myPen, 0, 0, 160, 80);

	///////////////////////////////////////////////////
	/*Matrix myMatrix;
	myMatrix.Rotate(30);
	myMatrix.Scale(1, 2, MatrixOrderAppend);
	myMatrix.Translate(5, 0, MatrixOrderAppend);

	myGraphics.DrawEllipse(&myPen, 0, 0, 100, 50);
	myGraphics.ScaleTransform(1.0f, 0.5f);
	myGraphics.TranslateTransform(50.0f, 0.0f, MatrixOrderAppend);
	myGraphics.RotateTransform(30.0f, MatrixOrderAppend);
	myGraphics.DrawEllipse(&myPen, 0, 0, 100, 50);*/
	//////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////
	/*Matrix myMatrix (1, 0, 0, -1, 0, 0);
	myGraphics.SetTransform(&myMatrix);
	myGraphics.TranslateTransform(200, 150, MatrixOrderAppend);
	// Create the path.
	SolidBrush mySolidBrush1(Color(255,0,0));
	SolidBrush mySolidBrush2(Color(0,255,0));
	GraphicsPath myGraphicsPath;
	Rect myRectangle(0, 0, 60, 60);
	myGraphicsPath.AddRectangle(myRectangle);

	// Fill the path on the new coordinate system.
	// No local transformation
	myGraphics.FillPath(&mySolidBrush1, &myGraphicsPath);

	// Set the local transformation of the GraphicsPath object.
	Matrix myPathMatrix;
	myPathMatrix.Scale(2, 1);
	myPathMatrix.Rotate(30, MatrixOrderAppend);
	myGraphicsPath.Transform(&myPathMatrix);

	// Fill the transformed path on the new coordinate system.
	myGraphics.FillPath(&mySolidBrush2, &myGraphicsPath);*/

	//画笔
	//画笔的预定义风格
	/*Pen pen(Color(255,0,0,255),15);
	pen.SetDashStyle(DashStyleSolid);
	myGraphics.DrawLine(&pen, 20, 20, 400, 20);
	pen.SetDashStyle(DashStyleDash);
	myGraphics.DrawLine(&pen, 20, 40, 400, 40);
	pen.SetDashStyle(DashStyleDot);
	myGraphics.DrawLine(&pen, 20, 60, 400, 60);
	pen.SetDashStyle(DashStyleDashDot);
	myGraphics.DrawLine(&pen, 20, 80, 400, 80);
	pen.SetDashStyle(DashStyleDashDotDot);
	myGraphics.DrawLine(&pen, 20, 100, 400, 100);*/

	//自定义画笔风格
	/*float dashValues[] = {5, 2, 15, 4};
	Pen bluePen(Color::Blue, 4);
	bluePen.SetDashPattern(dashValues,4);
	myGraphics.DrawLine(&bluePen, Point(5, 10), Point(405, 10));*/

	
	//绘制不透明和半透明的线条
	/*Bitmap bitmap(L"Texture.bmp");
	myGraphics.DrawImage(&bitmap, 10, 10, bitmap.GetWidth(), bitmap.GetHeight());
	Pen opaquePen(Color(255, 0, 0, 255), 15);
	Pen semiTransPen1(Color(128, 0, 0, 255), 15);
	Pen semiTransPen2(Color(64, 0, 0, 255), 15);
	myGraphics.DrawLine(&opaquePen, 0, 20, 100, 20);
	myGraphics.DrawLine(&semiTransPen1, 0, 40, 100, 40);
	myGraphics.DrawLine(&semiTransPen2, 0, 60, 100, 60);*/

	//设置画笔的对齐方式
	/*Pen blackPen(Color(255, 0, 0, 0), 1);
	Pen greenPen(Color(255, 0, 255, 0), 10);
	
	greenPen.SetAlignment(PenAlignmentCenter);
	myGraphics.DrawRectangle(&greenPen, 10, 100, 50, 50);
	myGraphics.DrawRectangle(&blackPen, 10, 100, 50, 50);

	greenPen.SetAlignment(PenAlignmentInset);
	myGraphics.DrawRectangle(&greenPen, 90, 100, 50, 50);
	myGraphics.DrawRectangle(&blackPen, 90, 100, 50, 50);*/

	//绘制具有线帽的线条
	/*Pen pen(Color(255, 0, 0, 255), 8);
	pen.SetStartCap(LineCapArrowAnchor);
	pen.SetEndCap(LineCapRoundAnchor);
   	myGraphics.DrawLine(&pen, 20, 175, 300, 175);*/

	//连接线条
	/*GraphicsPath path1;
	GraphicsPath path2;
	GraphicsPath path3;
	Pen penJoin(Color(255, 0, 0, 255), 20);
	//45度拼接
	path1.StartFigure();
	path1.AddLine(Point(50, 200), Point(100, 200));
	path1.AddLine(Point(100, 200), Point(100, 250));
	penJoin.SetLineJoin(LineJoinMiter);
	myGraphics.DrawPath(&penJoin, &path1);
	//斜角拼接
	path2.AddLine(Point(150, 200), Point(200, 200));
	path2.AddLine(Point(200, 200), Point(200, 250));
	penJoin.SetLineJoin(LineJoinBevel);
	myGraphics.DrawPath(&penJoin, &path2);
	//圆角拼接
	path3.AddLine(Point(250, 200), Point(300, 200));
	path3.AddLine(Point(300, 200), Point(300, 250));
	penJoin.SetLineJoin(LineJoinRound);
	myGraphics.DrawPath(&penJoin, &path3);*/

	//绘制用纹理填充的线条
	/*Bitmap bitmap(L"Texture.bmp");
	TextureBrush tBrush(&bitmap);
	Pen texturedPen(&tBrush, 30);
	myGraphics.DrawImage(&bitmap, 0, 0, bitmap.GetWidth(), bitmap.GetHeight());
	myGraphics.DrawEllipse(&texturedPen, 100, 20, 200, 100);*/

	//画刷
	//用纯色填充形状
	/*SolidBrush solidBrush(Color(255, 255, 0, 0));
	myGraphics.FillEllipse(&solidBrush, 0, 0, 100, 60);*/

	//用阴影图案填充形状
	/*HatchBrush hBrush(
		HatchStyleHorizontal,
		Color::Red,
		Color(255, 128, 255, 255));
	myGraphics.FillEllipse(&hBrush, 0, 0, 100, 60);*/

	//用图像平铺形状
	/*Image image(L"HouseAndTree.bmp");
	TextureBrush tBrush(&image);
	Pen blackPen(Color::Black);
	//平铺图像
	myGraphics.FillRectangle(&tBrush, Rect(0, 0, 200, 200));
	myGraphics.DrawRectangle(&blackPen, Rect(0, 0, 200, 200));
	//平铺时水平翻转图像
	tBrush.SetWrapMode(WrapModeTileFlipX);
	myGraphics.FillRectangle(&tBrush, Rect(250, 0, 200, 200));
	myGraphics.DrawRectangle(&blackPen, Rect(250, 0, 200, 200));
	//平铺时垂直翻转图像
	tBrush.SetWrapMode(WrapModeTileFlipY);
	myGraphics.FillRectangle(&tBrush, Rect(0, 250, 200, 200));
	myGraphics.DrawRectangle(&blackPen, Rect(0,250, 200, 200));
	//平铺时在水平和垂直方向翻转图像
	tBrush.SetWrapMode(WrapModeTileFlipXY);
	myGraphics.FillRectangle(&tBrush, Rect(250, 250, 200, 200));
	myGraphics.DrawRectangle(&blackPen, Rect(250, 250, 200, 200));*/

	//水平线性梯度
	/*LinearGradientBrush linGrBrush(
				Point(0, 10),
				Point(200, 10),
				Color(255, 255, 0, 0),   // 不透明红
				Color(255, 0, 0, 255));  // 不透明蓝
	Pen pen(&linGrBrush);
	myGraphics.DrawLine(&pen, 0, 10, 200, 10);
	myGraphics.FillEllipse(&linGrBrush, 0, 30, 200, 100);
	myGraphics.FillRectangle(&linGrBrush, 0, 155, 500, 30);*/

	//对角线线性梯度
	/*LinearGradientBrush linGrBrush(
		Point(0, 0),
		Point(200, 100),
		Color(255, 0, 0, 255),   // 不透明蓝
		Color(255, 0, 255, 0));  // 不透明绿
	Pen pen(&linGrBrush, 10);
	myGraphics.DrawLine(&pen, 0, 0, 600, 300);
	myGraphics.FillEllipse(&linGrBrush, 10, 100, 200, 100);*/

	//自定义线性梯度
	/*LinearGradientBrush linGrBrush(
		Point(0, 10),
		Point(200, 10),
		Color(255, 0, 0, 0),     // 不透明黑
		Color(255, 255, 0, 0));  // 不透明红
	float blendFactors[] = {0.0f, 0.5f, 1.0f};
	float blendPositions[] = {0.0f, 0.2f, 1.0f};
	linGrBrush.SetBlend(blendFactors, blendPositions, 3);
	myGraphics.FillEllipse(&linGrBrush, 0, 30, 200, 100);
	myGraphics.FillRectangle(&linGrBrush, 0, 155, 500, 30);*/

	//
	// 创建单个椭圆路径
	/*GraphicsPath path;
	path.AddEllipse(0, 0, 200, 100);
	// 创建基于椭圆路径的梯度画刷
	PathGradientBrush pthGrBrush(&path);
	// 设定边界颜色为蓝色
	Color colors[] = {Color::Blue};
	INT count = 1;
	pthGrBrush.SetSurroundColors(colors, &count);
	// 设定中心点颜色为浅绿色
	pthGrBrush.SetCenterColor(Color::Aqua);
	// 使用路径梯度画刷填充椭圆
	myGraphics.FillPath(&pthGrBrush, &path);
	// 设定聚焦缩放
	pthGrBrush.SetFocusScales(0.3f, 0.8f);
	// 再次填充椭圆
	myGraphics.TranslateTransform(220.0f, 0.0f);
	myGraphics.FillPath(&pthGrBrush, &path);*/
	
	// 指定边界上的点
	// 多边形顶点数组
	/*Point points[] = {
			Point(75, 0),
			Point(100, 50),
			Point(150, 50),
			Point(112, 75),
			Point(150, 150),
			Point(75, 100),
			Point(0, 150),
			Point(37, 75),
			Point(0, 50),
			Point(50, 50)};
	// 使用顶点数组创建路径
	GraphicsPath path;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -