[英]How to change the font of multiple sizes in richtextbox in C#?
我有一個富文本框,我想讓它像寫字板一樣工作。 我的問題是,例如,如果我使用 Calibri 字體輸入“123”,然后使用 Arial 字體輸入“456”並且我想更改 2345 的大小,它不會讓我這樣做,因為他們有兩個不同的字體類型。 這是我遇到問題的地方:
private void combo_sizes_TextChanged(object sender, EventArgs e)
{
if (rtb.SelectionFont == null)
{
rtb.SelectionFont = new Font(combo_fonts.Text, Convert.ToInt16(combo_sizes.Text));
}
rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily, Convert.ToInt16(combo_sizes.Text));
}
我知道當 rtb 中的選定文本包含多種字體時,SelectionFont 等於 null,因此在這種情況下,我已使其從兩個組合框中獲取選定文本的大小和字體,但我想更改大小而不丟失其原始字體。 有解決辦法嗎?
謝謝
將您selected text
分解為char
s。 獲取每個char
的字體,並更改其大小。
RichTextBox (RTB) 中有兩種內置字體:
Font
是用於任何輸入的Font
。 所以如果你想切換到另一種字體,這是你應該設置的SelectionFont
是當前選擇的字體。 這將隨着選擇而改變,但它也用於設置已經輸入的部分文本的字體。但在同一時間和地點只能有其中之一。 如果您想切換回標准 Front,您需要在某處保留標准字體。
或者您可以將您使用的所有字體存儲在一個列表中,然后您可以在一個組合框中提供它們。
另請注意:
你的代碼可能是這樣工作的:
public Form1()
{
InitializeComponent();
lastSelectionFont = rtb.SelectionFont;
lastFont = rtb.Font;
//..
}
Font lastSelectionFont;
Font lastFont;
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (rtb.SelectionLength > 0)
{
lastSelectionFont = rtb.SelectionFont;
rtb.SelectionFont = new Font(rtb.SelectionFont.FontFamily,
Convert.ToInt16(combo_sizes.Text));
}
else
{
lastFont = rtb.Font;
rtb.Font = new Font(rtb.Font.FontFamily,
Convert.ToInt16(combo_sizes.Text));
}
}
請注意,除非您將它設置為 null,否則 SelectionFont 不會為 null。 當沒有選擇時,您可能遇到了問題。
但同樣,我不確定你對“拯救”以前的 Forn 的想法。 想想寫字板:它也不像那樣做任何事情。 將您使用的所有字體添加到字體列表中,甚至可以使用這些顏色,並且字體和樣式的好名字聽起來很有吸引力。
我在這里也需要同樣的東西。 我沒有找到最好的解決方案......所以,這是丑陋的。
private void UglyChangeFontSize(RichTextBox rtb, float newSize = -1f, FontFamily fontFamily = null) {
if (rtb.SelectionFont != null) {
if (newSize < 0) newSize = rtb.SelectionFont.Size;
if (fontFamily == null) fontFamily = rtb.SelectionFont.FontFamily;
rtb.SelectionFont = new Font(fontFamily, newSize, rtb.SelectionFont.Style);
}
else {
// Backup Selection
var sel = rtb.SelectionStart;
var selLen = rtb.SelectionLength;
// Change, char by char
for (int k = 0; k < selLen; k++) {
rtb.Select(sel + k, 1);
if (newSize < 0) newSize = rtb.SelectionFont.Size;
var ff = fontFamily ?? rtb.SelectionFont.FontFamily;
rtb.SelectionFont = new Font(fontFamily, newSize, rtb.SelectionFont.Style);
}
// Restore Selection
rtb.SelectionStart = sel;
rtb.SelectionLength = selLen;
}
}
改良版
這是一個更好的版本,它包括:
要求
編碼
RichTextBox myRichTextBox = new RichTextBox();
RichOLE mRichOle = new RichOLE(myRichTextBox);
...
private void UglyChangeFontSize(RichTextBox rtb, float newSize = -1f, FontFamily fontFamily = null) {
if (rtb.SelectionFont != null) {
if (newSize < 0) newSize = rtb.SelectionFont.Size;
if (fontFamily == null) fontFamily = rtb.SelectionFont.FontFamily;
rtb.SelectionFont = new Font(fontFamily, newSize, rtb.SelectionFont.Style);
}
else {
Cursor = Cursors.WaitCursor;
// Hide the modifications from the user
External.LockWindowAndKeepScrollPosition(rtb, () =>
{
// Backup Selection
var sel = rtb.SelectionStart;
var selLen = rtb.SelectionLength;
// Disable UNDO for this "in pieces modifications" [START]
rtb.SelectedRtf = rtb.SelectedRtf; // Required to allow Undo
//mFontLockEvents = true; // RicherTextBox
mRichOle.EnableUndo(false);
// Disable UNDO for this "in pieces modifications" [END]
// Change, char by char
for (int k = 0; k < selLen; k++) {
rtb.Select(sel + k, 1);
// again, ugly... buuut we have Branch Prediction (google it)
if (newSize < 0) newSize = rtb.SelectionFont.Size;
var ff = fontFamily ?? rtb.SelectionFont.FontFamily;
rtb.SelectionFont = new Font(fontFamily, newSize, rtb.SelectionFont.Style);
}
// Disable UNDO for this "in pieces modifications" [START]
//mFontLockEvents = false; // RicherTextBox
mRichOle.EnableUndo(true);
// Disable UNDO for this "in pieces modifications" [END]
// Restore Selection
rtb.SelectionStart = sel;
rtb.SelectionLength = selLen;
});
Cursor = Cursors.Default;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.