RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
518抽奖软件源码之:GDI+上下左右中对齐-创新互联

518抽奖软件源码之:GDI+上下左右中对齐

在开发“518抽奖软件”的时候,需要用到GDI+的写文字功能。 GDI+在对齐方面比原来的GDI复杂些,主要是 StringFormat 的参数设置和行高的测定问题,详情参考下面的源码。

创新互联是由多位在大型网络公司、广告设计公司的优秀设计人员和策划人员组成的一个具有丰富经验的团队,其中包括网站策划、网页美工、网站程序员、网页设计师、平面广告设计师、网络营销人员及形象策划。承接:成都网站设计、成都网站建设、网站改版、网页设计制作、网站建设与维护、网络推广、数据库开发,以高性价比制作企业网站、行业门户平台等全方位的服务。

GDIPLUS(gdi+)对齐处理的函数源码

//单行文本,rc内对齐,align是水平对齐,valign是垂直对齐
//
void Tapp::paint_str(HDC hDc, const WCHAR* str, LOGFONT& lf, COLORREF color, RECT& rc, WORD align, WORD valign)
{
	if (!str) return;
	if (rc.right - rc.left <= 1 || rc.bottom - rc.top <= 1) return;

	StringFormat fmt;
	fmt.SetFormatFlags(StringFormatFlagsNoWrap | StringFormatFlagsNoClip | StringFormatFlagsMeasureTrailingSpaces);

	if (align == ALIGN_NEAR) fmt.SetAlignment(StringAlignmentNear);
	else if (align == ALIGN_CENTER) fmt.SetAlignment(StringAlignmentCenter);
	else fmt.SetAlignment(StringAlignmentFar);
	fmt.SetLineAlignment(StringAlignmentNear);

	RectF rcF;
	rcF.X = rc.left;
	rcF.Y = rc.top;
	rcF.Width = rc.right - rc.left;
	rcF.Height = rc.bottom - rc.top;

	FontFamily ff(lf.lfFaceName);
	Font gfont(hDc, &lf);
	SolidBrush  bru(Color(GetRValue(color), GetGValue(color), GetBValue(color)));
	Graphics g(hDc);

	int style = FontStyleRegular;
	if (lf.lfItalic) style = FontStyleItalic;
	if (lf.lfWeight == FW_BOLD) style = FontStyleBold;
	if (lf.lfItalic && lf.lfWeight == FW_BOLD) style = FontStyleBoldItalic;
	double r = (double)(ff.GetCellAscent(style) + ff.GetCellDescent(style)) / (double)ff.GetLineSpacing(style);

	if (align == ALIGN_NEAR) rcF.Width += 500.0;
	else if (align == ALIGN_FAR) { rcF.X -= 500.0; rcF.Width += 500.0; }
	else { rcF.X -= 500.0; rcF.Width += 1000.0; }

	if (valign == ALIGN_NEAR) rcF.Height += 500.0;
	else if (valign == ALIGN_CENTER) {
		rcF.Y -= 500.0;
		rcF.Height += 1000.0;
		rcF.Y += ((rcF.Height - gfont.GetHeight(&g)*r) / 2.0);
	}
	else {
		rcF.Y -= 500.0;
		rcF.Height += 500.0;
		rcF.Y += (rcF.Height - gfont.GetHeight(&g)*r);
	}

	SetTextRenderingHint(TextRenderingHintAntiAlias);
	g.DrawString(str, -1, &gfont, rcF, &fmt, &bru);
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:518抽奖软件源码之:GDI+上下左右中对齐-创新互联
链接地址:http://sczitong.cn/article/godgj.html