2016-05-29 13 views
0

是否有一種簡單/快速的方法來更改VB.net應用程序中所有控件的字體? (目前使用VB 2015)VB.net VS2015更改整個項目的字體

我的意思是所有的文本框,按鈕等。它應該是獨立的系統 默認字體。

在整個項目(30-40表格+)中更改每個控件將極其耗費人力。

+1

顯然編輯窗體設計器中的文件是可能的,但印象中的IM你應該做的:) – Josh

+0

寫一個應用程序來做到這一點,你可以通過設計文件,並替換所有字體屬性... – Codexer

+0

沒有什麼內置VS來做到這一點。你可以寫一個VS擴展,然後這將是你想要的快速和簡單的操作。 – jmcilhinney

回答

0

vb.net自定義字體

即使我猜測它不建議更改默認字體的家庭;有時需要它,特別是對於可訪問性,定製和個性化。這也可以爲其他應用程序中的最終用戶提供很好的功能。

感謝許多帖子和答案,並將它們結合起來;你可以嘗試 在運行時更改整個項目的字體系列。

  • 這是在VS 2017 CE,VB.Net 4.6.1;對於2015年VS不能肯定
  • 這個答案也包括在同一時間應用谷歌的字體不進行安裝到最終用戶的操作系統

步驟自定義字體添加到VB.NET和WinForms :

  1. 葡萄從谷歌字體的字體(下載爲TTF)。
  2. 將字體添加到您的解決方案中。
  3. 引用新的自定義字體並將其作爲對象加載。
  4. 引用表單中的所有控件。
  5. 檢查控件是否接受/需要字體定製。
  6. 應用新的字體系列。
  7. 運行和測試。

Imports System.Drawing.Text 
Public Class Form1 
    Dim pfc As New PrivateFontCollection() 
    Private Function FindALLControlRecursive(ByVal list As List(Of Control), ByVal parent As Control) As List(Of Control) 
     ' function that returns all control in a form, parent or child regardless of control's type 
     If parent Is Nothing Then 
      Return list 
     Else 
      list.Add(parent) 
     End If 
     For Each child As Control In parent.Controls 
      FindALLControlRecursive(list, child) 
     Next 
     Return list 
     End Functio 
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown 
     ' On Form1 shown, start applying font 
     Dim CFontPath As String = Application.StartupPath 
     pfc.AddFontFile(CFontPath & "\Resources\Fonts\Roboto.ttf") 
     Dim allCtrl As New List(Of Control) 
     For Each ctrl As Control In FindALLControlRecursive(allCtrl, Me) 
      ' You need to define which control type to change it's font family; not recommendd to just change all controls' fonts, it will create a missy shape 
      If TypeOf ctrl Is Label Or TypeOf ctrl Is TextBox Or TypeOf ctrl Is Button Or TypeOf ctrl Is CheckBox Or TypeOf ctrl Is RadioButton Or TypeOf ctrl Is ProgressBar Or TypeOf ctrl Is GroupBox Or TypeOf ctrl Is Chart Then 
       Dim CurrentCtrlFontSize = ctrl.Font.Size ' get current object's font size before applying new font family 
       ctrl.Font = New Font(pfc.Families(0), CurrentCtrlFontSize, FontStyle.Regular) 
      End If 
     Next 
     allCtrl.Clear() 
    End Sub 
End Class 

我不能肯定儘管這是最優的代碼或最好的方法,但它做的工作,並希望一些合作。

我知道它的一箇舊的日期問題,但希望這個答案有幫助和它非常有趣的問題。

相關問題