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

📄 regions.cpp

📁 VC源代码大全(精华版)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		point[i].x = rcClient.right / 2 + (int) (cos (fAngle) * nArm);
		point[i].y = rcClient.bottom / 2 - (int) (sin (fAngle) * nArm);
	}
	if (bStar)
	{
	POINT temp, temp1;
	HRGN	hLeft, hRight;

		temp = point[1];
		point[1] = point[3];
		temp1 = point[2];
		point[2] = temp;
		temp = point[4];
		point[4] = temp1;
		point[3] = temp;
		hLeft = CreatePolygonRgn (point, 5, ALTERNATE);
		OffsetRgn (hLeft, -rcClient.right / 4, 0);
		hRight = CreatePolygonRgn (point, 5, WINDING);
		OffsetRgn (hRight, rcClient.right / 4, 0);
		HRGN hRgn = CreateRectRgn (0, 0, 1, 1);
		CombineRgn (hRgn, hRight, hLeft, RGN_OR);
		DeleteObject (hLeft);
		DeleteObject (hRight);
		DrawRadials (hdc, hRgn, rcClient);
	}
	else
	{
		hRgn = CreatePolygonRgn (point, 5, ALTERNATE);
		DrawRadials (hdc, hRgn, rcClient);
	}
	if (bInvert)
		InvertRgn (hdc, hRgn);
	DeleteObject (hRgn);
}

void DoCloverRegion (HWND hWnd, HDC hdc, bool bInvert)
{
HRGN hRgn, hTemp[6];
RECT	rcClient;

	GetClientRect (hWnd, &rcClient);
	hTemp[0] = CreateEllipticRgn (0, rcClient.bottom / 3,
									rcClient.right /  2, 2 * rcClient.bottom / 3); 
	hTemp[1] = CreateEllipticRgn (rcClient.right / 2, rcClient.bottom / 3,
									rcClient.right, 2 * rcClient.bottom / 3); 
	hTemp[2] = CreateEllipticRgn (rcClient.right / 3, 0,
									2 * rcClient.right /  3, rcClient.bottom / 2); 
	hTemp[3] = CreateEllipticRgn (rcClient.right / 3, rcClient.bottom / 2,
									2 * rcClient.right /  3, rcClient.bottom); 
	hTemp[4] = CreateRectRgn (0, 0, 1, 1);
	hTemp[5] = CreateRectRgn (0, 0, 1, 1);

	hRgn = CreateRectRgn (0, 0, 1, 1);
	CombineRgn (hTemp[4], hTemp[0], hTemp[1], RGN_OR);
	CombineRgn (hTemp[5], hTemp[2], hTemp[3], RGN_OR);
	CombineRgn (hRgn, hTemp[4], hTemp[5], RGN_XOR);

	for (int i = 0; i < 6; ++i)
		DeleteObject (hTemp[i]);
	DrawRadials (hdc, hRgn, rcClient, 0);
	if (bInvert)
		InvertRgn (hdc, hRgn);
	DeleteObject (hRgn);
}

void DoMappingModes (HWND hWnd, HDC hdc)
{
int nOldMapMode;
int nMapMode;
RECT	rcClient, rcText;
HPEN	hPen;
LOGFONT	lf;
HFONT	hFont;
int nHorzSide, nVertSide, nOrigin;
int nModes[] =  {MM_TEXT, MM_HIMETRIC, MM_LOMETRIC,
				 MM_HIENGLISH, MM_LOENGLISH, MM_TWIPS};
char *szLabels[] = 
	{
		"MM_TEXT",
		"MM_HIMETRIC",
		"MM_LOMETRIC",
		"MM_HIENGLISH",
		"MM_LOENGLISH",
		"MM_TWIPS"
	};

	GetClientRect (hWnd, &rcClient);
	nVertSide = rcClient.bottom / 3;
	nHorzSide = nVertSide;

//	int nOldROP = SetROP2 (hdc, R2_MASKPEN);
//	int nOldROP = SetROP2 (hdc, R2_NOTXORPEN);
//	int nOldROP = SetROP2 (hdc, R2_NOTMASKPEN);

	CreateLogFont (hdc, lf, 14);
	hFont = CreateFontIndirect (&lf);

	rcText.right = rcClient.right;
	rcText.left = 3 * nHorzSide + 20;

	nOldMapMode = GetMapMode (hdc);
	for (int i = 0; i < 6; ++i)
	{
		nVertSide = rcClient.bottom - 10;
		nHorzSide = nVertSide;
		nMapMode = SetMapMode (hdc, nModes[i]);
		rcText.top = rcClient.top + 20 * (i + 1) + lf.lfHeight;
		rcText.bottom = rcText.top + lf.lfHeight;
		switch (nModes[i])
		{
			case MM_TEXT:
				hPen = CreatePen (PS_SOLID, 2, 0);
				SelectObject (hdc, hPen);
				nOrigin = 0;
				SetTextColor (hdc, 0);
				break;
			case MM_HIMETRIC:
				hPen = CreatePen (PS_SOLID, 2, 0x00ff0000);
				SelectObject (hdc, hPen);
				nVertSide *= -1;
				nOrigin = 100;
				SetTextColor (hdc, 0x00ff0000);
				break;
			case MM_LOMETRIC:
				hPen = CreatePen (PS_SOLID, 2, 0x0000ff00);
				SelectObject (hdc, hPen);
				nVertSide *= -1;
				nOrigin = 10;
				SetTextColor (hdc, 0x0000ff00);
				break;
			case MM_HIENGLISH:
				hPen = CreatePen (PS_SOLID, 2, 0x000000ff);
				SelectObject (hdc, hPen);
				nVertSide *= -1;
				nHorzSide *= 1;
				nOrigin = 30;
				SetTextColor (hdc, 0x000000ff);
				break;
			case MM_LOENGLISH:
				hPen = CreatePen (PS_SOLID, 2, 0x00ff00ff);
				SelectObject (hdc, hPen);
				nVertSide *= -1;
				nHorzSide *= 1;
				nOrigin = 100;
				SetTextColor (hdc, 0x00ff00ff);
				break;
			case MM_TWIPS:
				hPen = CreatePen (PS_SOLID, 2, 0x00ffff00);
				SelectObject (hdc, hPen);
				nVertSide *= -1;
				nHorzSide *= 1;
				nOrigin = 50;
				SetTextColor (hdc, 0x00ffff00);
				break;
		}
		DrawRect (hdc, nOrigin, -nOrigin, nHorzSide, nVertSide);
		SetMapMode (hdc, MM_TEXT);
		DrawText (hdc, szLabels[i], strlen (szLabels[i]), &rcText, TEXT_FORMAT & ~DT_CENTER);
		DeleteObject (hPen);
	}
	SetMapMode (hdc, nOldMapMode);
//	SetROP2 (hdc, nOldROP);
}

void DrawRect (HDC hdc, int x, int y, int width, int depth)
{
	Rectangle (hdc, x, y, x + width, y + depth);
}

void DoRectanglePath (HWND hWnd, HDC hdc)
{
RECT	rcClient;
POINT	pt;
LOGBRUSH	lb;
HBRUSH hGrayBrush, hWhiteBrush;

	GetClientRect (hWnd, &rcClient);
	lb.lbStyle = BS_SOLID;
	lb.lbColor = RGB(0x77, 0x77, 0x77);
	lb.lbHatch = 0;
	hGrayBrush = CreateBrushIndirect (&lb);
	lb.lbColor = RGB(0xff, 0xff, 0xff);
	hWhiteBrush = CreateBrushIndirect (&lb);

	BeginPath (hdc);
	Rectangle (hdc, rcClient.bottom / 4, rcClient.bottom / 4, 
                    3 * rcClient.bottom / 4, 3 * rcClient.bottom / 4);
	CloseFigure (hdc);
	EndPath (hdc);
	HPEN hPen = CreatePen (PS_SOLID, 1, 0);
	SelectObject (hdc, hPen);
	SelectObject (hdc, hGrayBrush);
	StrokeAndFillPath (hdc);
	BOOL bResult = SetViewportOrgEx (hdc, 20, 20, &pt);
	SelectObject (hdc, hWhiteBrush);
	StrokeAndFillPath (hdc);
	SetViewportOrgEx (hdc, pt.x, pt.y, NULL);

	DeleteObject (hPen);
	DeleteObject (hGrayBrush);
	DeleteObject (hWhiteBrush);

}

void DoTextPath (HWND hWnd, HDC hdc)
{
RECT	rcClient;
LOGFONT	lf;
HRGN	hRgn;
LOGBRUSH	lb;
HBRUSH hGrayBrush, hBlackBrush;

	GetClientRect (hWnd, &rcClient);
	lb.lbStyle = BS_SOLID;
	lb.lbColor = RGB(0x77, 0x77, 0x77);
	lb.lbHatch = 0;
	hGrayBrush = CreateBrushIndirect (&lb);
	lb.lbColor = RGB(0x0, 0x0, 0x0);
	hBlackBrush = CreateBrushIndirect (&lb);

	CreateLogFont (hdc, lf, 120);
	lf.lfHeight = -lf.lfHeight;
	HFONT hFont = CreateFontIndirect(&lf);
	SelectObject (hdc, hFont);
	BeginPath (hdc);
	DrawText (hdc, "HEY!", 4, &rcClient, TEXT_FORMAT);
	EndPath (hdc);

	HRGN hTemp1, hTemp2;
	RECT rcText;
	hTemp1 = PathToRegion (hdc);
	GetRgnBox (hTemp1, &rcText);
	hTemp2 = CreateRectRgnIndirect (&rcText);
	hRgn = CreateRectRgn (0, 0, 1, 1);
	CombineRgn (hRgn, hTemp1, hTemp2, RGN_XOR);
	DeleteObject (hTemp1);
	DeleteObject (hTemp2);

	OffsetRgn (hRgn, 20, 20);
	FillRgn (hdc, hRgn, hGrayBrush);
	OffsetRgn (hRgn, -20, -20);
	FillRgn (hdc, hRgn, hBlackBrush);
	DeleteObject (hRgn);
	DeleteObject (hGrayBrush);
	DeleteObject (hBlackBrush);
	DeleteObject (hFont);
}

void DrawRadials (HDC hdc, HRGN hRgn, RECT & rc, int nShadow, int nStep)
{
double	fAngle, fRadius;
HPEN	hPen, hOldPen;
HBRUSH	hbrGray, hbrBlack, hbrWhite;

	hbrGray = CreateSolidBrush (RGB(0x77, 0x77, 0x77));
	hbrBlack = CreateSolidBrush (RGB(0x0, 0x0, 0x0));
	hbrWhite = CreateSolidBrush (RGB(0xff, 0xff, 0xff));

	if (nShadow)
	{
		OffsetRgn (hRgn, nShadow, nShadow);
//
//	To create our shadow, you may use one of two methods. In the first
//	you select the brush into the device context, then call PaintRegion().
//	In the second method, you call FillRgn with the brush you want to
//	fill the region with.
		SelectObject (hdc, hbrGray);
		PaintRgn (hdc, hRgn);
//	FillRgn (hdc, hRgn, hbrGray);

//	Draw a black frame around the shadow.
		FrameRgn (hdc, hRgn, hbrBlack, 1, 1);

		OffsetRgn (hRgn, -nShadow, -nShadow);
//
//	Regions are transparent, so we want to erase the portion of the
//	shadow below our image. See the note above abouit filling and painting.
		FillRgn (hdc, hRgn, hbrWhite);
	}
//	Draw a black frame around the image.
	FrameRgn (hdc, hRgn, hbrBlack, 1, 1);

//	We'll draw long lines and let the region clip them
	fRadius = _hypot (rc.right / 2.0, rc.bottom / 2.0);

//	Set the viewport origin to the center of the rectangle
	POINT pOldOrigin;
	SetViewportOrgEx (hdc, rc.right / 2, rc.bottom / 2, &pOldOrigin);
//	Select the region as our clipping area
	SelectClipRgn (hdc, hRgn);
//	Create and select our pen
	hPen = CreatePen (PS_SOLID, 1, RGB(0x00, 0x00, 0x00));
	hOldPen = (HPEN) SelectObject (hdc, hPen);

//	Draw the radials from the viewport origin.
	for (fAngle = 0; fAngle < 2 * PI; fAngle += nStep * 2 * PI / 360)
	{
		MoveToEx (hdc, 0, 0, NULL);
		LineTo (hdc, (int) (fRadius * cos (fAngle) + 0.5),
					 (int) (-fRadius * sin (fAngle) + 0.5));
	}
//	Reselect the old pen and delete the one we created.
	SelectObject (hdc, hOldPen);
	DeleteObject (hPen);
//	Get rid of the brushes we created.
	DeleteObject (hbrBlack);
	DeleteObject (hbrGray);
	DeleteObject (hbrWhite);
	SetViewportOrgEx (hdc, pOldOrigin.x, pOldOrigin.y, NULL);
}

void CreateLogFont (HDC hdc, LOGFONT & lf, int nPoints, char *szFace)
{
	memset (&lf, '\0', sizeof (LOGFONT));
	lf.lfCharSet = DEFAULT_CHARSET;
	lf.lfClipPrecision = OUT_TT_PRECIS;
	lf.lfQuality = DEFAULT_QUALITY;
	lf.lfPitchAndFamily = DEFAULT_PITCH | FF_ROMAN;
	lf.lfHeight = MulDiv(nPoints, GetDeviceCaps(hdc, LOGPIXELSY), 72);
	strcpy (lf.lfFaceName, szFace);
}

void DoMouseEvent (HWND hWnd, UINT message, UINT nFlags, POINT & point)
{
static bool	bTracking = false;
static POINT	ptLineStart = {0, 0};
static POINT	ptLineEnd = {0, 0};

	switch (message)
	{
		case WM_LBUTTONDOWN:
			ptLineStart = point;
			break;
		case WM_LBUTTONUP:
			bTracking = false;
			ptLineStart.x = 0;
			ptLineStart.y = 0;
			break;
		case WM_MOUSEMOVE:
			if (!(nFlags & MK_LBUTTON))
				return;
			if (bTracking)
			{
			HDC hdc;
				POINT pt;
				pt = point;
				hdc = GetDC (hWnd);
				DoMouseLine (hdc, ptLineStart, ptLineEnd);
				DoMouseLine (hdc, ptLineStart, pt);
				ReleaseDC (hWnd, hdc);
			}
			ptLineEnd = point;
			bTracking = true;
			break;
	}
}

void DoMouseLine (HDC hdc, POINT & ptLineStart, POINT & ptLineEnd)
{
	HPEN hPen = CreatePen (PS_SOLID, 2, 0);
	int nOldMode = SetROP2 (hdc, R2_NOT);
	MoveToEx (hdc, ptLineStart.x, ptLineStart.y, NULL);
	SelectObject (hdc, hPen);
	LineTo (hdc, ptLineEnd.x, ptLineEnd.y);
	SetROP2 (hdc, nOldMode);
	DeleteObject (hPen);
}

⌨️ 快捷键说明

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