积极答复者
VB.NET 怎样使界面自适应WINDOWS的不同系统和不同分辨率的屏幕?

问题
答案
-
#Region " Public Sub AutoCtlSize(ByVal inObj As Control) '自动调整控件大小" Public Sub AutoCtlSize(ByVal inObj As Control) '自动调整控件大小 If inObj Is Nothing Then Exit Sub End If '显示分辨率与窗体工作区的大小的关系:分辨率width*height--工作区(没有工具栏)width*(height-46) '即分辨率为*600时,子窗体只能为*554 '上述情况还要windows状态栏自动隐藏,如果不隐藏,则height还要减少,结果为:*524 '检测桌面显示分辨率(Visual Basic)请参见 '此示例以像素为单位确定桌面的宽度和高度。 Dim DeskTopSize As Size = System.Windows.Forms.SystemInformation.PrimaryMonitorSize Dim FontSize As Single FontSize = 9 * DeskTopSize.Height / 600 '控件本身**** '控件大小 inObj.Size = New Size(Int(inObj.Size.Width * DeskTopSize.Width / 800), Int(inObj.Size.Height * DeskTopSize.Height / 600)) '控件位置 inObj.Location = New Point(Int(inObj.Location.X * DeskTopSize.Width / 800), Int(inObj.Location.Y * DeskTopSize.Height / 600)) '如果控件为Form,则设置固定边框 'Dim mType As Type 'Dim mProperty As System.Reflection.PropertyInfo 'mType = inObj.GetType 'mProperty = mType.GetProperty("FormBorderStyle") 'If Not mProperty Is Nothing Then 'MessageBox.Show(mType.ToString) ' mProperty.SetValue(inObj, FormBorderStyle.FixedSingle, Nothing) 'End If '子控件===== Dim n As Integer For n = 0 To inObj.Controls.Count - 1 '只调整子控件的字体。(如果调整窗体的字体,再调用窗体的show方法时,会引发resize从而导致控件的大小和布局改变) inObj.Controls.Item(n).Font = New Font(inObj.Controls.Item(n).Font.FontFamily, FontSize) '递归调用(穷举所有子控件) AutoCtlSize(inObj.Controls.Item(n)) Next End Sub #End Region
我早年写的自动调整控件大小的程序,在要show form时,通过调用AutoCtlSize(form),可以起到调整form及form中控件大小、位置的作用,你可以参考下。- 已标记为答案 Leo (Apple) YangModerator 2013年10月19日 7:24
全部回复
-
您好iChanse:
就我知道,要实现分辨率适应不是很容易的。我建议你可以在最小的屏幕上开发,或者使用一些特殊的控件(比如TableLayout,Anchor等属性)。让控件自适应。
Click For donating:Free Rice For the poor
For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
For more things to talk about? StackOverFlow is your choice.- 已建议为答案 Leo (Apple) YangModerator 2013年10月14日 1:46
-
#Region " Public Sub AutoCtlSize(ByVal inObj As Control) '自动调整控件大小" Public Sub AutoCtlSize(ByVal inObj As Control) '自动调整控件大小 If inObj Is Nothing Then Exit Sub End If '显示分辨率与窗体工作区的大小的关系:分辨率width*height--工作区(没有工具栏)width*(height-46) '即分辨率为*600时,子窗体只能为*554 '上述情况还要windows状态栏自动隐藏,如果不隐藏,则height还要减少,结果为:*524 '检测桌面显示分辨率(Visual Basic)请参见 '此示例以像素为单位确定桌面的宽度和高度。 Dim DeskTopSize As Size = System.Windows.Forms.SystemInformation.PrimaryMonitorSize Dim FontSize As Single FontSize = 9 * DeskTopSize.Height / 600 '控件本身**** '控件大小 inObj.Size = New Size(Int(inObj.Size.Width * DeskTopSize.Width / 800), Int(inObj.Size.Height * DeskTopSize.Height / 600)) '控件位置 inObj.Location = New Point(Int(inObj.Location.X * DeskTopSize.Width / 800), Int(inObj.Location.Y * DeskTopSize.Height / 600)) '如果控件为Form,则设置固定边框 'Dim mType As Type 'Dim mProperty As System.Reflection.PropertyInfo 'mType = inObj.GetType 'mProperty = mType.GetProperty("FormBorderStyle") 'If Not mProperty Is Nothing Then 'MessageBox.Show(mType.ToString) ' mProperty.SetValue(inObj, FormBorderStyle.FixedSingle, Nothing) 'End If '子控件===== Dim n As Integer For n = 0 To inObj.Controls.Count - 1 '只调整子控件的字体。(如果调整窗体的字体,再调用窗体的show方法时,会引发resize从而导致控件的大小和布局改变) inObj.Controls.Item(n).Font = New Font(inObj.Controls.Item(n).Font.FontFamily, FontSize) '递归调用(穷举所有子控件) AutoCtlSize(inObj.Controls.Item(n)) Next End Sub #End Region
我早年写的自动调整控件大小的程序,在要show form时,通过调用AutoCtlSize(form),可以起到调整form及form中控件大小、位置的作用,你可以参考下。- 已标记为答案 Leo (Apple) YangModerator 2013年10月19日 7:24