首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

VC++.NET 使用 GDI+ 建立特效字體:漸層/網格畫刷繪製文字完整教學

     編輯:VC 教學

前言

C++/CLI (Managed C++) 結合 **GDI+** (System.Drawing) 提供強大的向量繪圖能力。本文示範在 Windows Forms 控件 Paint 事件中,使用 **線性漸層畫刷** 、**網格畫刷** 繪製特效文字,涵蓋 Graphics 取得、字體建立、文字測量、畫刷實例化、背景填充、DrawString 繪製完整流程。

核心步驟總覽

  1. 在控件 Paint 事件處理函數中繪製 (確保重繪正確)。
  2. 從 PaintEventArgs.Graphics 取得 **永久有效** 的 Graphics 物件 (勿用 CreateGraphics)。
  3. 建立 Font 物件 (字體名、大小、樣式)。
  4. Graphics.MeasureString 測量文字尺寸 (SizeF)。
  5. 建立畫刷:HatchBrush (網格) 或 LinearGradientBrush (漸層)。
  6. (選擇性) 填充背景:Graphics.Clear 或 FillRectangle。
  7. Graphics.DrawString 繪製文字 (指定畫刷、字體、位置)。

完整程式碼 (C++/CLI)


#pragma once
using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Drawing2D;
using namespace System::Windows::Forms;

namespace GDIPlusTextDemo {
    public ref class TextDisplayControl : public PictureBox
    {
    public:
        property String^ TextToDisplay {
            String^ get() { return textToDisplay; }
            void set(String^ value) { textToDisplay = value; Invalidate(); }
        }

        property Font^ DisplayFont {
            Font^ get() { return displayFont; }
            void set(Font^ value) { displayFont = value; Invalidate(); }
        }

        property BrushType BrushType { // 自訂列舉
            BrushType get() { return brushType; }
            void set(BrushType value) { brushType = value; Invalidate(); }
        }

    protected:
        virtual void OnPaint(PaintEventArgs^ e) override
        {
            PictureBox::OnPaint(e);
            Graphics^ g = e->Graphics; // 關鍵:使用事件提供的 Graphics

            if (String::IsNullOrEmpty(textToDisplay) || displayFont == nullptr) return;

            // 1. 測量文字尺寸
            SizeF textSize = g->MeasureString(textToDisplay, displayFont);

            // 2. 建立畫刷
            Brush^ brush;
            if (brushType == BrushType::Hatch) {
                brush = gcnew HatchBrush(HatchStyle::Cross, Color::Black, Color::Blue);
            } else { // LinearGradient
                RectangleF rect(0, 0, textSize.Width, textSize.Height);
                brush = gcnew LinearGradientBrush(rect, Color::Black, Color::Blue,
                                                  LinearGradientMode::ForwardDiagonal);
            }

            // 3. 填充背景 (示範兩種方式)
            // g->Clear(Color::White); // 簡單全域填充
            g->FillRectangle(SystemBrushes::Control, 0, 0, this->Width, this->Height); // 系統控件色

            // 4. 置中繪製文字
            float x = (this->Width - textSize.Width) / 2;
            float y = (this->Height - textSize.Height) / 2;
            g->DrawString(textToDisplay, displayFont, brush, x, y);
        }

    private:
        String^ textToDisplay = "GDI+ Special Text";
        Font^ displayFont = gcnew Font("Times New Roman", 20, FontStyle::Regular);
        enum class BrushType { Hatch, LinearGradient };
        BrushType brushType = BrushType::LinearGradient;
    };
}

關鍵類別與方法速查

類別/方法命名空間用途
GraphicsSystem::Drawing繪圖介面,對應 HDC
FontSystem::Drawing字體 (名稱、大小、樣式)
MeasureStringGraphics回傳 SizeF 文字繪製範圍
HatchBrushSystem::Drawing::Drawing2D網格/斜線/十字等圖案填充
LinearGradientBrushSystem::Drawing::Drawing2D線性漸層 (雙色、多色、路徑漸層)
SolidBrushSystem::Drawing純色填充
TextureBrushSystem::Drawing圖片紋理填充
DrawStringGraphics繪製文字 (支援畫刷、字體、格式)
FillRectangleGraphics填充矩形 (支援任意畫刷)
SystemBrushesSystem::Drawing系統預設畫刷 (Control, Window, Highlight 等)

進階技巧

  • 文字格式控制:StringFormat 設定對齊、換行、省略號、數位替換等。g->DrawString(text, font, brush, rect, format)。
  • 抗鋸齒:g->TextRenderingHint = TextRenderingHint::AntiAliasGridFit (或 ClearTypeGridFit)。
  • 路徑漸層:PathGradientBrush 依 GraphicsPath 形狀產生放射/自定漸層。
  • 高 DPI:GDI+ 內建支援裝置獨立像素,配合 SetProcessDPIAware 即可正確縮放。

非 Managed C++ (原生 MFC) 使用 GDI+


#include 
#pragma comment(lib, "gdiplus.lib")

// 初始化 (App 啟動)
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartupInput input;
Gdiplus::GdiplusStartup(&gdiplusToken, &input, NULL);

// 繪製 (OnPaint)
void CMyView::OnPaint()
{
    CPaintDC dc(this);
    Gdiplus::Graphics g(dc.m_hDC);
    Gdiplus::Font font(L"Times New Roman", 20);
    Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 0, 255)); // ARGB
    g.DrawString(L"GDI+ Text", -1, &font, Gdiplus::PointF(10,10), &brush);
}

// 關閉 (App 結束)
Gdiplus::GdiplusShutdown(gdiplusToken);

總結

GDI+ 提供豐富的**畫刷體系** (漸層、網格、紋理) 讓文字繪製極具表現力。關鍵在於:**在 Paint 事件中使用 e->Graphics**、**正確測量尺寸**、**選擇適合的畫刷**。Managed C++ (C++/CLI) 程式碼極簡潔;原生 MFC 亦可透過 Gdiplus.h 直接呼叫,僅需手動管理啟動/關閉與 Graphics 建構。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved