- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[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
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 là 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:
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