NEWS

[C#] Hướng dẫn cách tạo drop cap giống trình soạn thảo văn bản Microsoft Word

[C#] Hướng dẫn cách tạo drop cap giống trình soạn thảo văn bản Microsoft Word
Đăng bởi: Thảo Meo - Lượt xem: 7077 08:32:01, 28/08/2018C#   In bài viết

Bài viết hôm nay, mình sẽ chia sẽ các bạn source code cách tạo Drop Cap giống chương trình soạn thảo Microsoft Word trong lập trình C#.

Vậy Drop cap là gì?

Drop Cap là thuật ngữ chỉ việc tạo chữ đầu tiên của đoạn văn lớn hơn bình thường như cách trình bày bạn vẫn thấy ở các trang báo in. Đây  kỹ năng khá cơ bản mà khi đi học tin học văn phòng các bạn đều được hướng dẫn, tuy nhiên có lẽ do Microsoft Office 2010 còn chưa phổ biến nên khá nhiều bạn còn thắc mắc vấn đề này.

Giống đây là giao diện Demo ứng dụng:

sử dụng drop cap trong lập trình c#

Full source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace howto_illuminated_writing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private string[] paragraphs =
        {
            "RSA là một trong những hệ thống mã hoá bất đối xứng được sử dụng rộng rãi. Nó được đặt theo tên của 3 nhà khoa học MIT thiết kế ra nó là: Ron Rivest, Adi Shamir, và Leonard Adleman. Ý tưởng then chốt để đảm bảo tính an toàn của RSA là dựa trên sự khó khăn trong việc phân tích nhân tử của 2 số nguyên tố lớn. (a x b = c, tìm ngược lại a, b từ c là phân tích nhân tử).",
            "SQLite không thích hợp với những hệ thống lớn nhưng ở quy mô vừa tầm thì SQLite phát huy uy lực và không hề yếu kém về mặt chức năng hay tốc độ. Với các đặc điểm trên SQLite được sử dụng nhiều trong việc phát triển, thử nghiệm … và là sự lưa chọn phù hợp cho những người bắt đầu học Database.",

        };

        private void picWriting_Paint(object sender, PaintEventArgs e)
        {

            const int paragraph_spacing = 10;
            const int margin = 5;
            RectangleF rect = new RectangleF(margin, margin,
                picWriting.ClientSize.Width - 2 * margin,
                picWriting.ClientSize.Height - 2 * margin);


            using (Font lead_font = new Font("Times New Roman", 30, FontStyle.Bold))
            {
                using (Font body_font = new Font("Times New Roman", 12))
                {

                    foreach (string paragraph in paragraphs)

                        DrawIlluminatedText(e.Graphics, 50, 55,
                            lead_font, Brushes.Green, Pens.Green,
                            body_font, Brushes.Black,
                            ref rect, paragraph_spacing, paragraph);
                }
            }
        }


        private void picWriting_Resize(object sender, EventArgs e)
        {
            picWriting.Refresh();
        }


        private void DrawIlluminatedText(Graphics gr,
            float min_lead_width, float min_lead_height,
            Font lead_font, Brush lead_brush, Pen lead_pen,
            Font body_font, Brush body_brush,
            ref RectangleF rect, int paragraph_spacing, string paragraph)
        {

            string ch = paragraph.Substring(0, 1);
            paragraph = paragraph.Substring(1);


            SizeF lead_size = gr.MeasureString(ch, lead_font);
            if (lead_size.Width < min_lead_width)
                lead_size.Width = min_lead_width;
            if (lead_size.Height < min_lead_height)
                lead_size.Height = min_lead_height;


            using (StringFormat string_format = new StringFormat())
            {

                string_format.Trimming = StringTrimming.Word;


                SizeF side_size = new SizeF(
                    rect.Width - lead_size.Width,
                    lead_size.Height);


                int chars_fitted, lines_filled;
                side_size = gr.MeasureString(paragraph, body_font,
                    side_size, string_format,
                    out chars_fitted, out lines_filled);


                string side_text = paragraph.Substring(0, chars_fitted);
                paragraph = paragraph.Substring(chars_fitted);


                string_format.FormatFlags = StringFormatFlags.LineLimit;


                side_size.Height += 1000;
                side_size = gr.MeasureString(side_text, body_font,
                    side_size, string_format,
                    out chars_fitted, out lines_filled);
                if (side_size.Height < min_lead_height)
                    side_size.Height = min_lead_height;


                if (lead_size.Height < side_size.Height)
                    lead_size.Height = side_size.Height;


                gr.DrawRectangle(lead_pen, rect.X, rect.Y,
                    lead_size.Width, lead_size.Height);


                RectangleF lead_rect = new RectangleF(
                    rect.X, rect.Y, lead_size.Width, lead_size.Height);
                string_format.Alignment = StringAlignment.Center;
                string_format.LineAlignment = StringAlignment.Center;
                gr.DrawString(ch, lead_font, lead_brush, lead_rect, string_format);
                string_format.Alignment = StringAlignment.Near;
                string_format.LineAlignment = StringAlignment.Near;


                RectangleF side_rect = new RectangleF(
                    rect.X + lead_size.Width,
                    rect.Y,
                    side_size.Width,
                    side_size.Height);


                gr.DrawString(side_text, body_font, body_brush,
                    side_rect, string_format);


                rect.Y += lead_size.Height;

                rect.Height -= lead_size.Height;


                gr.DrawString(paragraph, body_font, body_brush, rect, string_format);


                SizeF rect_size = new SizeF(rect.Width, rect.Height);
                SizeF size = gr.MeasureString(paragraph, body_font, rect_size);

                rect.Y += size.Height + paragraph_spacing;
                rect.Height -= size.Height + paragraph_spacing;
            }
        }
    }
}

HAPPY CODING heart

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn cách tạo drop cap giống trình soạn thảo văn bản Microsoft Word
Đăng bởi: Thảo Meo - Lượt xem: 7077 08:32:01, 28/08/2018C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.