vb.net窗体自适应的简单介绍

VB 窗体控件分辨率自适应问题Public Type CONTROLRECTLeft As SingleTop As SingleWidth As SingleHeight As SingleEnd Type Public Const HORZRES = 8Public Const VERTRES = 10Public Declare Function GetDesktopWindow Lib "user32" () As LongPublic Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As LongPublic Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As LongPublic Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long '取得界面原始控件的位置及大小 , 并保存到数组里Public Sub GetSourcePos(this As Object, rc() As CONTROLRECT, Optional bigFont As Boolean = True)
'On Error Resume Next
【vb.net窗体自适应的简单介绍】 Dim tempX As Integer, tempY As IntegertempX = this.ScaleWidth'1024tempY = this.ScaleHeight'768 '此处原来如果在1024*768分辨率下显示正常的话 , 就可以直接赋值1024和768Dim temp As Control Dim nSum As Integer nSum = 0 For Each temp In this'此处要注意,有些控件没有width,height等属性,在此要做出判断If TypeOf temp Is ComboBox ThenWith rc(nSum).Left = temp.Left / tempX.Width = temp.Width / tempX.Top = temp.Top / tempYEnd WithElseIf TypeOf temp Is Timer Then''none'ElseIf TypeOf temp Is StatusBar Then''noneElseWith rc(nSum).Left = temp.Left / tempX.Width = temp.Width / tempX.Top = temp.Top / tempY.Height = temp.Height / tempYEnd WithEnd IfnSum = nSum + 1 NextEnd Sub '根据比例调整控件的大小Public Sub SetNewPos(this As Object, rc() As CONTROLRECT)
'On Error Resume NextDim tempX As Integer, tempY As IntegertempX = this.ScaleWidth'1024tempY = this.ScaleHeight'768''如果初始界面显示始终是以最大化的方式显示的话 , 此处就可以调用系统分辨率进行设置tempx,tempy'hwnd = GetDesktopWindow()'' Get the device context for the desktop'hdc = GetWindowDC(hwnd)'If hdc Then'Dim a As Long, b As Long'a = GetDeviceCaps(hdc, HORZRES)'b = GetDeviceCaps(hdc, VERTRES)'tempX = a'tempY = b'End If'ReleaseDC hwnd, hdcDim temp As Control'//用于取各种控件Dim nSum As IntegernSum = 0For Each temp In this'此处要注意,有些控件没有width,height等属性 , 在此要做出判断If TypeOf temp Is ComboBox Thentemp.Left = rc(nSum).Left * tempXtemp.Width = rc(nSum).Width * tempXtemp.Top = rc(nSum).Top * tempYElseIf TypeOf temp Is Timer Then'none'ElseIf TypeOf temp Is StatusBar Then'noneElsetemp.Left = rc(nSum).Left * tempXtemp.Width = rc(nSum).Width * tempXtemp.Top = rc(nSum).Top * tempYtemp.Height = rc(nSum).Height * tempYEnd IfnSum = nSum + 1Next End Sub
VB窗体控件自动适应窗体大小Dim f_size(1) As Long, fist_re As Boolean'用来存放窗体默认大小 以及 是否第一次初始化,全局变量,可在 模块中public声明
Private Sub Form_Resize()
If Me.WindowState1 Then '必须排除最小化vb.net窗体自适应的状态
If fist_re = False Then '窗体初始化只记录窗体大小
f_size(0) = Me.Height: f_size(1) = Me.Width
fist_re = True
Else '否则开始适应屏幕变化
For Each a In Form1.Controls
On Error Resume Next
a.Width = a.Width * (Me.Width / f_size(1))
a.Height = a.Height * (Me.Height / f_size(0))
a.Top = a.Top * (Me.Height / f_size(0))
a.Left = a.Left * (Me.Width / f_size(1))
Next
f_size(0) = Me.Height: f_size(1) = Me.Width '重新记录窗口大小,用于下次运算
End If
End If
End Sub
''对于一些控件如listboxvb.net窗体自适应的高度不适宜 呵呵
VB如何设置窗体背景图片随窗体大小自动调整?Private Sub Form_Load();
Me.Picture = LoadPicture("E:\Users\xjn\Pictures\Lotus.jpg")'自己改图片路径;
Me.AutoRedraw = True;
End Sub;
Private Sub Form_Resize();
Me.PaintPicture Me.Picture, 0, 0, Me.Width, Me.Height;
End Sub 。
延展回答:
VB表格打印控件:自动识别并支持ADO和DAO两种数据库链接,也可以不链接数据库.(可以链接数据控件,也可以链接数据对象dao/ado).支持合并单元格、对齐、支持单元格斜线、支持多种单元格格式,可自动设置行高列宽.支持表格导出到word 。

推荐阅读