C语言版GDI+应用例子 -- 文字

本文是使用C语言版GDI+仿C#文字绘制的例子代码:

#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; PGpFont titleFont; PGpFont textFont; PGpFont thisFont; 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); } //---------------------------------------------------------------------------

例子代码使用的窗口框架代码和GDI+ C语言版本下载地址见《在C语言Windows应用程序中使用GDI+》。

下面是运行效果图:

C语言版GDI+应用例子 -- 文字

指导和建议请来信:[email protected][email protected]