Solved

Change System Font in WinXP

Posted on 2004-11-01
558 Views
Last Modified: 2013-12-03
Hi Experts,

Now I'm using WinXP, .Net Framework 1.1. I want to read and change current system font in my System. Can you help me?

Thanks for all help.

VINHNL
0
Question by:vinhnl
    9 Comments
     
    LVL 18

    Expert Comment

    by:armoghan
    Question is unclear..
    Do you mean the font of your application window only. That is simple. Set the font of the form from designer

    or

    Do you mean the Font of Windows. i.e. which you chnage by going to
    Control Panel -> Display -> Appearnce

    see this VB code and convert it into C#
    http://www.quantumcloud.com/forum/viewtopic.php?p=3110
    0
     
    LVL 8

    Author Comment

    by:vinhnl
    Hi  armoghan,

    I want change the Fonts of Windows appearence  (Message, Menu, Caption Fonts ).

    Your url maybe change Fonts of Windows appearence  in VB6. I can't convert it into C#.

    Can someone help me?

    VINHNL
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    I am sorry, i cannot convert it for you right now..
    See the following link , This might help you in converting it to VB.NET

    http://www.codeguru.com/forum/showthread.php?t=308842&goto=nextnewest



    I have converted the Types into structures
    see this .. You have to convertthe rest of it .. Sorry



    Private Structure LOGFONT
            Dim lfHeight As Long
            Dim lfWidth As Long
            Dim lfEscapement As Long
            Dim lfOrientation As Long
            Dim lfWeight As Long
            Dim lfItalic As Byte
            Dim lfUnderline As Byte
            Dim lfStrikeOut As Byte
            Dim lfCharSet As Byte
            Dim lfOutPrecision As Byte
            Dim lfClipPrecision As Byte
            Dim lfQuality As Byte
            Dim lfPitchAndFamily As Byte
            Dim lfFaceName As Byte()
            Sub New(ByVal ArraySize As Integer)
                ReDim lfFaceName(ArraySize)
            End Sub
        End Structure
        Private Structure NONCLIENTMETRICS
            Dim cbSize As Long
            Dim iBorderWidth As Long
            Dim iScrollWidth As Long
            Dim iScrollHeight As Long
            Dim iCaptionWidth As Long
            Dim iCaptionHeight As Long
            Dim lfCaptionFont As LOGFONT
            Dim iSMCaptionWidth As Long
            Dim iSMCaptionHeight As Long
            Dim lfSMCaptionFont As LOGFONT
            Dim iMenuWidth As Long
            Dim iMenuHeight As Long
            Dim lfMenuFont As LOGFONT
            Dim lfStatusFont As LOGFONT
            Dim lfMessageFont As LOGFONT
        End Structure

    0
     
    LVL 8

    Author Comment

    by:vinhnl
    No no, I can convert VB Struct to C# Struct. But Screen Object in C# have no Screen.Fonts Property.

    VINHNL
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    Ok, that problem I can solve
    you need to use System.Drawing.Text.InstalledFontCollection to get the fonts instead of Screen.Fonts
    see this link
    http://www.c-sharpcorner.com/winforms/DisplayFontsJOD.asp
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    Please do post the final code here if you can.. It would be a help to the community
    Thanx
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    All the help is provided. A working VB example which can be easily converted to .NET  
    So would like to claim points for the question.
    According to EE rules I do not want to provide the full working code. but if it is necessary. It can be done :)
    0
     
    LVL 8

    Author Comment

    by:vinhnl
    Would you write this method (Change Fonts of Windows appearence) for me? If you can do that, I will accept you!

    VINHNL
    0
     
    LVL 18

    Accepted Solution

    by:
    Ok I tried to convert it to VB.NET. most of it is converted. It may have few problems and may not run "as is".

    Imports System.Drawing
    Imports System.Drawing.Text

    Public Class Form1
        Inherits System.Windows.Forms.Form
        '<!--StartFragment-->'**************************************
        'Windows API/Global Declarations for :Change System (Message, Menu, Caption) Fonts
        Private Structure LOGFONT
            Dim lfHeight As Long
            Dim lfWidth As Long
            Dim lfEscapement As Long
            Dim lfOrientation As Long
            Dim lfWeight As Long
            Dim lfItalic As Byte
            Dim lfUnderline As Byte
            Dim lfStrikeOut As Byte
            Dim lfCharSet As Byte
            Dim lfOutPrecision As Byte
            Dim lfClipPrecision As Byte
            Dim lfQuality As Byte
            Dim lfPitchAndFamily As Byte
            Dim lfFaceName As Byte()
            Sub New(ByVal ArraySize As Integer)
                ReDim lfFaceName(ArraySize)
            End Sub
        End Structure
        Private Structure NONCLIENTMETRICS
            Public cbSize As Long
            Public iBorderWidth As Long
            Public iScrollWidth As Long
            Public iScrollHeight As Long
            Public iCaptionWidth As Long
            Public iCaptionHeight As Long
            Public lfCaptionFont As LOGFONT
            Public iSMCaptionWidth As Long
            Public iSMCaptionHeight As Long
            Public lfSMCaptionFont As LOGFONT
            Public iMenuWidth As Long
            Public iMenuHeight As Long
            Public lfMenuFont As LOGFONT
            Public lfStatusFont As LOGFONT
            Public lfMessageFont As LOGFONT

            Sub New(ByVal ArraySize As Integer)
                lfSMCaptionFont = New LOGFONT(ArraySize)
                lfCaptionFont = New LOGFONT(ArraySize)
                lfMenuFont = New LOGFONT(ArraySize)
                lfStatusFont = New LOGFONT(ArraySize)
                lfMessageFont = New LOGFONT(ArraySize)
            End Sub
        End Structure

        Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByRef NONCLIENTMETRICS As Object, ByVal fuWinIni As Integer) As Integer
        '**************************************

        ' Inputs:ADD A COMBO BOX
        '
        ' Returns:Changes the Message box font a
        '    nd Windows Caption Font (Title Font).
        '
        'Assumes:Add a Combo box.
        '
        '**************************************


    #Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Public WithEvents ComboBox1 As System.Windows.Forms.ComboBox
        Friend WithEvents Button1 As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.ComboBox1 = New System.Windows.Forms.ComboBox
            Me.Button1 = New System.Windows.Forms.Button
            Me.SuspendLayout()
            '
            'ComboBox1
            '
            Me.ComboBox1.Location = New System.Drawing.Point(64, 80)
            Me.ComboBox1.Name = "ComboBox1"
            Me.ComboBox1.Size = New System.Drawing.Size(448, 21)
            Me.ComboBox1.TabIndex = 0
            Me.ComboBox1.Text = "ComboBox1"
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(72, 136)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Button1"
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(536, 266)
            Me.Controls.Add(Me.Button1)
            Me.Controls.Add(Me.ComboBox1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)

        End Sub

    #End Region

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer
            Show()
            Dim ifc As New InstalledFontCollection
            Dim ffs As FontFamily() = ifc.Families
            Dim f As Font


            For i = 0 To ffs.Length - 1 ' or use For i = 1 To Screen.FontCount to flood all the fonts in your pc
                Dim ff As FontFamily
                ff = ffs(i)
                If ff.IsStyleAvailable(System.Drawing.FontStyle.Regular) Then
                    f = New Font(ff.GetName(1), 12, System.Drawing.FontStyle.Regular)
                ElseIf ff.IsStyleAvailable(System.Drawing.FontStyle.Bold) Then
                    f = New Font(ff.GetName(1), 12, System.Drawing.FontStyle.Bold)
                ElseIf (ff.IsStyleAvailable(System.Drawing.FontStyle.Italic)) Then
                    f = New Font(ff.GetName(1), 12, System.Drawing.FontStyle.Italic)
                Else
                    f = New Font(ff.GetName(1), 12, System.Drawing.FontStyle.Underline)
                End If

                ComboBox1.Items.Add(f.Name)
            Next i
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ncm As New NONCLIENTMETRICS(32)  'NONCLIENTMETRICS to change

            Dim Orincm As New NONCLIENTMETRICS(32) 'NONCLIENTMETRICS to replace original
            Dim Returned As Long
            'Dim xa As Integer = Len(ncm)
            'ncm.cbSize = xa
            Try
                Returned = SystemParametersInfo(41, 0, ncm, 0)   'get the system NONCLIENTMETRICS
            Catch ex As Exception
                Console.WriteLine(ex.StackTrace)
            End Try

            Orincm = ncm 'store the value of system NONCLIENTMETRICS to use later now to change the font name
            Console.WriteLine(Me.ComboBox1.Text)
            ncm.lfMessageFont.lfFaceName = System.Text.Encoding.ASCII.GetBytes(ComboBox1.Text)
            ncm.lfCaptionFont.lfFaceName = System.Text.Encoding.ASCII.GetBytes(ComboBox1.Text)

            'ncm.lfMessageFont.lfFaceName(i) = 0 'add null at the end of font name
            'ncm.lfCaptionFont.lfFaceName(i) = 0
            Returned = SystemParametersInfo(42, 0, ncm, &H1 Or &H2) 'remove &H2 if you don't want to affect all the open windows
            MsgBox("Message & Caption Font Changed to " & ComboBox1.Text, vbOKOnly)
            Returned = SystemParametersInfo(42, 0, Orincm, &H1 Or &H2) 'replace original font
            'MsgBox("Message & Caption Font Replaced to " & StrConv(System.Text.Encoding.ASCII.GetString(Orincm.lfCaptionFont.lfFaceName, 0, Orincm.lfCaptionFont.lfFaceName.Length), VbStrConv.None), vbOKOnly, "NILESH")
        End Sub

        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        End Sub
    End Class
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now