Hàm SystemParametersInfo cho phép bạn truy cập và thay đổi hầu hết các thiết lập giao diện người sử dụng ở mức thấp (màn hình Display Properties của Windows).
Bạn có thể đọc chi tiết trên MSDN, ở đây tôi chỉ đưa một số ví dụ cơ bản:
Và một số thiết lập trong Control Panel như:
Sau đây, tôi sẽ viết một đoạn chương trình demo để thay đổi thiết lập font chữ của MessageBox trên Windows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | public class WindowsUISettings { // Khai báo sử dụng API SystemParametersInfo [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool SystemParametersInfo( int Action, int uiParam, [In, Out] NONCLIENTMETRICS pvParam, int WinIni); [StructLayout(LayoutKind.Sequential)] private class NONCLIENTMETRICS { public int cbSize = Marshal.SizeOf(typeof(NONCLIENTMETRICS)); public int iBorderWidth; public int iScrollWidth; public int iScrollHeight; public int iCaptionWidth; public int iCaptionHeight; [MarshalAs(UnmanagedType.Struct)] public LOGFONT lfCaptionFont; public int iSmCaptionWidth; public int iSmCaptionHeight; [MarshalAs(UnmanagedType.Struct)] public LOGFONT lfSmCaptionFont; public int iMenuWidth; public int iMenuHeight; [MarshalAs(UnmanagedType.Struct)] public LOGFONT lfMenuFont; [MarshalAs(UnmanagedType.Struct)] public LOGFONT lfStatusFont; [MarshalAs(UnmanagedType.Struct)] public LOGFONT lfMessageFont; } private const int LF_FACESIZE = 32; // A "logical font" used by old-school windows [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private class LOGFONT { public int lfHeight; public int lfWidth; public int lfEscapement; public int lfOrientation; public int lfWeight; public byte lfItalic; public byte lfUnderline; public byte lfStrikeOut; public byte lfCharSet; public byte lfOutPrecision; public byte lfClipPrecision; public byte lfQuality; public byte lfPitchAndFamily; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)] public string lfFaceName; // to shut it up about the warnings public LOGFONT(string lfFaceName) { this.lfFaceName = lfFaceName; lfHeight = lfWidth = lfEscapement = lfOrientation = lfWeight = 0; lfItalic = lfUnderline = lfStrikeOut = lfCharSet = lfOutPrecision = lfClipPrecision = lfQuality = lfPitchAndFamily = 0; } } //Khai báo SPI lấy và thay đổi thiết lập về //caption, menu, status bar, scroll bar và message box private const int SPI_GETNONCLIENTMETRICS = 0x0029; private const int SPI_SETNONCLIENTMETRICS = 0x002A; private NONCLIENTMETRICS currentMetrics; /// <summary> /// Lưu giữ lại các giá trị ngầm định /// </summary> private static NONCLIENTMETRICS defaultMetrics; private WindowsUISettings() { } public static WindowsUISettings CurrentMetrics() { //Lưu giữ lại các giá trị ngầm định defaultMetrics = new NONCLIENTMETRICS(); SystemParametersInfo(SPI_GETNONCLIENTMETRICS, defaultMetrics.cbSize, defaultMetrics, 0); NONCLIENTMETRICS pvParam = new NONCLIENTMETRICS(); SystemParametersInfo(SPI_GETNONCLIENTMETRICS, pvParam.cbSize, pvParam, 0); WindowsUISettings instance = new WindowsUISettings(); instance.currentMetrics = pvParam; return instance; } /// <summary> /// Lấy/Thiết lập font chữ cho MessageBox /// </summary> public Font MessageFont { get { return Font.FromLogFont(currentMetrics.lfMessageFont); } set { value.ToLogFont(currentMetrics.lfMessageFont); } } /// <summary> /// Thiết lập các thay đổi /// </summary> public void Apply() { SystemParametersInfo(SPI_SETNONCLIENTMETRICS, currentMetrics.cbSize, currentMetrics, 1); } /// <summary> /// Đặt lại các giá trị ngầm định /// </summary> public void RestoreDefault() { SystemParametersInfo(SPI_SETNONCLIENTMETRICS, defaultMetrics.cbSize, defaultMetrics, 1); } } |
Và đây là ví dụ chạy thử:
Hãy tạo 1 form mới, trên đó có 2 button bao gồm, button1 và button2. Double-click vào button1 và sau đó button2, ví dụ xử lý sự kiện Click trên 2 button sẽ như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public partial class Form1 : Form { WindowsUISettings metrics = WindowsUISettings.CurrentMetrics(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Font messageFont = new Font( metrics.MessageFont.FontFamily, 14); metrics.MessageFont = messageFont; metrics.Apply(); MessageBox.Show( "MessageBox đã được thay đổi font size thành 14."); } private void button2_Click(object sender, EventArgs e) { metrics.RestoreDefault(); MessageBox.Show( "MessageBox đã được trở về font size 8."); } } |
Qua ví dụ này, chắc các bạn đã hiểu cách để lấy thông tin và thay đổi các thông số khác.
Theo Ho Hai Thanh’s blog