iT邦幫忙

0

C#要怎麼用程式碼改變FontStyle?

c#

無論是Bold、Italic、Strikeout還是Underline全部都是唯獨
如果我希望有一個又是粗體又斜體的字形該怎麼做啊?
是不是只能用建構子來達成啊?

謝謝各位看到這裡

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

3
glj8989332
iT邦研究生 4 級 ‧ 2020-09-17 23:15:37
最佳解答

這樣子, 用 | 串接多個FontStyle

label1.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold | FontStyle.Italic);

原理是FontStyle的值是bit manipulation, 背後會再解析是否有多個選項
Bold 1
Italic 2
Regular 0
Strikeout 8
Underline 4

看更多先前的回應...收起先前的回應...
a31916909 iT邦新手 5 級 ‧ 2020-09-17 23:32:24 檢舉

如果我的字原本有什麼特效不知道,然後要增加一個斜體要怎麼做?

glj8989332 iT邦研究生 4 級 ‧ 2020-09-17 23:35:27 檢舉

取得原本的, 仍用 | 繼續串

FontStyle curFontStyle = label1.Font.Style;
label1.Font = new Font(FontFamily.GenericSansSerif, 12.0F, curFontStyle | FontStyle.Italic);
HoiDam iT邦新手 5 級 ‧ 2020-09-18 10:35:24 檢舉

神羅天征

glj8989332 iT邦研究生 4 級 ‧ 2020-09-18 12:13:27 檢舉

一起扛米 /images/emoticon/emoticon37.gif

0
小魚
iT邦大師 1 級 ‧ 2020-09-17 23:44:17

你說的是WinForm吧,
可以這樣子,
不一定要寫在建構子,
只是建構子會在進入畫面馬上看到.

Font font = new Font("新細明體", 12, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
label1.Font = font;

結果如下:
https://ithelp.ithome.com.tw/upload/images/20200917/20105694Xvrf0eHejm.png

我要發表回答

立即登入回答