gdi c语言教程,C语言版GDI+应用例子 -- 文字

#include "../../SampleCode/comcode/Application.h"

#pragma hdrstop

WCHAR flowedText1[] =

L"I went down to the St James Infirmary,/n /

Saw my baby there,/n /

Stretched out on a long white table,/n /

So sweet,so cold,so fair,/n /

Let her go,let her go,God bless her,/n /

Wherever she may be,/n /

She can look this wide world over,/n /

But she'll never find a sweet man like me,/n /

When I die want you to dress me in straight lace shoes,/n /

I wanna a boxback coat and a Stetson hat,/n /

Put a twenty dollar gold piece on my watch chain,/n /

So the boys'll know that I died standing up.";

WCHAR flowedText2[] =

L"the sky seems full when you're in the cradle/n /

the rain will fall and wash your dreams/n /

stars are stars and they shine so hard.../n /

now spit out the sky because its empty/n /

and hollow and all your dreams are hanging out to dry/n /

stars are stars and they shine so cold.../n /

once i like crying twice i like laughter/n /

come tell me what i'm after.";

WCHAR japaneseText[] = {

31169, 12398, 21517, 21069, 12399, 12463,

12522, 12473, 12391, 12377, 12290, 0};

PGpBrush backgroundBrush;

PGpBrush textTextureBrush;

PGpBrush titleShadowBrush;

PGpBrush linearGradBrush;

PGpFonttitleFont;

PGpFonttextFont;

PGpFontthisFont;

PGpFont japaneseFont;

void OnCreate(void)

{

Point ps[] = {{0, 0}, {0, 45}};

PGpFontFamily serifFontFamily = FontFamilyGenericSerif();

//Load the image to be used for the background from the exe's resource fork

PGpImage image = ImageCreate(L"..//..//Media//colorbars.jpg");

//Now create the brush we are going to use to paint the background

backgroundBrush = TextureBrushCreate(image);

ImageDelete(image);

//Load the image to be used for the textured text from the exe's resource fork

image = ImageCreate(L"..//..//Media//marble.jpg");

textTextureBrush = TextureBrushCreate(image);

ImageDelete(image);

//Load the fonts we want to use

thisFont = FontFromFamily(serifFontFamily, 20, 0, UnitPoint);

titleFont = FontFromFamily(serifFontFamily, 60, 0, UnitPoint);

textFont = FontFromFamily(serifFontFamily, 11, 0, UnitPoint);

//Set up shadow brush - make it translucent

titleShadowBrush = SolidBrushCreate(SetAlpha(Black, 70));

//Set up fonts and brushes for printing japanese text

japaneseFont = FontCreate(L"MS Mincho", 36, 0);

linearGradBrush = LineBrushFromPoint(&ps[0], &ps[1], Blue, Red);

}

void OnDestroy(void)

{

BrushDelete(textTextureBrush);

BrushDelete(backgroundBrush);

BrushDelete(titleShadowBrush);

BrushDelete(linearGradBrush);

FontDelete(titleFont);

FontDelete(textFont);

FontDelete(thisFont);

FontDelete(japaneseFont);

}

void OnPaint(HDC DC)

{

WCHAR s[40] = L"Hello Symetrical World";

RECT clientRect;

RectF r;

REAL center;

INT characters, lines;

PGpStrFormat format = StrFormatCreate();

PGpSolidBrush sb = SolidBrushCreate(SetAlpha(White, 180));

PGpGraphics g = GraphicsCreate(DC);

GraphicsSetTextRenderingHint(g, TextRenderingHintAntiAlias);

//Fill the background use the texture brush

//and then apply a white wash

GetClientRect(Handle, &clientRect);

GraphicsFillRectangle(g, backgroundBrush, clientRect.left, clientRect.top, clientRect.right, clientRect.bottom);

GraphicsFillRectangle(g, sb, clientRect.left, clientRect.top, clientRect.right, clientRect.bottom);

//Simple draw hello world

SolidBrushSetColor(sb, Black);

GraphicsDrawStringXY(g, L"Hello World", thisFont, sb, 10, 10);

//Draw a textured string

GraphicsDrawStringXY(g, L"Graphics Samples", titleFont, titleShadowBrush, 15, 25);

GraphicsDrawStringXY(g, L"Graphics Samples", titleFont, textTextureBrush, 10, 20);

//Use Measure string to display a string at the center of the window

SolidBrushSetColor(sb, Red);

center = clientRect.right / 2.0f;

GraphicsMeasureString(g, s, textFont, &r);

GraphicsDrawStringXY(g, s, textFont, sb, center - r.Width / 2.0f, 10.0f);

//Now draw a string flowed into a rectangle

r = MakeRectF(20.0f, 150.0f, 250.0f, 300.0f);

SolidBrushSetColor(sb, Gainsboro);

GraphicsFillRectangleF(g, sb, r.X, r.Y, r.Width, r.Height);

SolidBrushSetColor(sb, Blue);

GraphicsDrawString(g, flowedText1, textFont, sb, &r, NULL);

//Draw more flowed text but this time center it

r = MakeRectF(420.0f, 150.0f, 250.0f, 300.0f);

SolidBrushSetColor(sb, Gainsboro);

GraphicsFillRectangleF(g, sb, r.X, r.Y, r.Width, r.Height);

StrFormatSetAlignment(format, StringAlignmentCenter);

SolidBrushSetColor(sb, Blue);

GraphicsDrawString(g, flowedText2, textFont, sb, &r, format);

//Work out how many lines and characters we printed just now

characters = lines = 0;

GraphicsMeasureStringSize(g, flowedText2, textFont,

250.0f, 300.0f, format, &r, &characters, &lines);

wsprintfW(s, L"We printed %d characters and %d lines", characters, lines);

SolidBrushSetColor(sb, Black);

GraphicsDrawStringXY(g, s, textFont, sb, 390.0f, 440.0f);

//If we have the Japanese language pack draw some text in Japanese

//Rotate it to make life truly exciting

GraphicsRotate(g, -30, MatrixOrderPrepend);

GraphicsTranslate(g, -180, 300, MatrixOrderPrepend);

GraphicsDrawStringXY(g, japaneseText, japaneseFont, linearGradBrush, 175, 125);

GraphicsResetTransform(g);

StrFormatDelete(format);

BrushDelete(sb);

GraphicsDelete(g);

}

WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

{

/* 这里使用了双缓冲画窗口,可将第3个参数改为FALSE比较一下效果 */

InitApplication(hInstance, nCmdShow, TRUE);

CreateProc = OnCreate;

DestroyProc = OnDestroy;

PaintProc = OnPaint;

return RunApplication(TEXT("C语言Gdiplus演示例子 -- 文字(仿c#例子)"), 700, 500);

}

//---------------------------------------------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值