VB窗体的设计技巧 孙春美 2001年 52期 在Visual Basic编程中,每个应用程序都是从窗体(Form)开始的,窗体是构成用户接口的所有控件的容器。灵活运用窗体的各项属性和操作,就可以为用户设计出赏心悦目的操作界面。下面就是几个与窗体设计有关的示例:   #1一、创建不规则窗口   要创建不规则窗口看上去似乎很难。但如果我们说用几行代码就可以实现,这似乎不可思议。可事实就是如此!Win32 API有很多让你意想不到的功能,大家不妨试一试:   #21.菱形窗口   Private Declare Function CreatePolygonRgn Lib “gdi32” (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long   Private Declare Function SetWindowRgn Lib “user32” (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long   Private Type POINTAPI   x As Long   y As Long   End Type   Private Sub Form_Load   Dim ap(4) As POINTAPI   Dim hx As Integer   Dim hy As Integer   Dim reRgn As Integer   hy = Me.Height \(2 * Screen.TwipsPerPixelY)   hx = Me.Width \(2 * Screen.TwipsPerPixelX)   '设置Polygon为一个以Form1为依据的菱形   ap(0).x = 0: ap(0).y = hy   ap(1).x = hx: ap(1).y = 0   ap(2).x = Me.Width \ Screen.TwipsPerPixelX: ap(2).y = hy   ap(3).x = hx: ap(3).y = Me.Height \ Screen.TwipsPerPixelY   ap(4) = ap(0)   reRgn = CreatePolygonRgn(ap(0), 5, 1)   '设置窗口为菱形   SetWindowRgn Me.hWnd, reRgn, True   End Sub   #22.椭圆形窗口   Private Declare Function CreateEllipticRgn Lib “gdi32” (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long,ByVal Y2 As Long) As Long   Private Declare Function SetWindowRgn Lib “user32” (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long   Private Sub Form_Load()   Show   SetWindowRgn hWnd,CreateEllipticRgn(0,0,300,200),true   End Sub   #1二、创建字型窗体   Private Declare Function BeginPath Lib “gdi32” (ByVal hdc As Long) As Long   Private Declare Function TextOut Lib “gdi32” Alias “TextOutA” (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal lpString As String, ByVal nCount As Long) As Long   Private Declare Function EndPath Lib “gdi32” (ByVal hdc As Long) As Long   Private Declare Function PathToRegion Lib “gdi32” (ByVal hdc As Long) As Long   Private Declare Function DeleteObject Lib “gdi32” (ByVal hObject As Long) As Long   Private Declare Function SetWindowRgn Lib “user32” (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long   Private Sub Form_Load()   Dim hRgn As Long   Me.Height = 5265   Me.Left = 0   Me.Top = 0   Me.Width = 10200   With Me   .BackColor = QBColor(3)   .Font.Name = “隶书”   .Font.Size = 250   End With   '在窗体上产生“窗体”字路径   BeginPath Form1.hdc   TextOut Form1.hdc, 0, 0, “窗体”, 6   EndPath Form1.hdc   '将所产生的路径转换为区域   hRgn = PathToRegion(hdc)   '设置窗体形状为转换成的区域   SetWindowRgn hwnd, hRgn. 1   '删除对象,释放系统资源   DeleteObject hRgn   End Sub   #1三、创建一个背景具有渐变效果的窗体   许多Windows应用程序的安装界面都是一个颜色由蓝至黑的渐变背景,下面的示例就是在VB中实现窗体背景的渐变效果。   Private Sub Gradient(TheObject As Object,Redval, Greenval, Blueval)   Dim Step, i,T,L, R, B   Step = (TheObject.Height / 60)   T = 0   L = 0   R = TheObject.Width   B =T +Step   '使用循环在窗体上从上至下依次绘制60个矩形   For i = 1 To 60   TheObject.Line (l, t)-(r, b), RGB(Redval, Greenval, Blueval), BF   Redval = Redval - 4   Greenval = Greenval - 4   Blueval =Blueval - 4   If Redval <= 0 Then Redval = 0   If Greenval <= 0 Then Greenval = 0   If Blueval <= 0 Then Blueval = 0   T = B   B = B +Step   Next   End Sub   Private Sub Form_Resize   Gradient Form1, 0, 0, 255   End Sub   #1四、使窗体始终保持在最上层   有时应用程序需要一个信息或查询窗体总保持在最上层,即使切换到其它窗体也能看见该窗体,这是通过调用Windows API函数SetWindowPos()实现的,示例如下:   option Explicit   Private Declare Function SetWindowPos Lib “user32”(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long   Const HWND_TOPMOST=-1   Const SWP_SHOWWINDOW=&&H40   Private sub Form_load()   Dim retValue as long   Retvalue=setwindowpos(me.hwnd,hwnd_topmost,me.currentx,me.currenty,300,300,   swp_showwindow)   End sub   #1五、使窗体以当前分辨率全屏显示   在应用程序的编制调试过程中,当屏幕分辨率改变时,窗体可能不能正常显示,这时可以在窗体的Load事件中插入系列代码,即可使窗体启动时以当前屏幕分辨率全屏显示:   Private Sub Form_Load()   Form1.Top = 0 '设置窗体上边界位置   Form1.Left = 0 '设置窗体左边界位置   Form1.Width = Screen.Width '设置窗体宽度为屏幕宽度   Form1.Height = Screen.Height '设置窗体高度为屏幕高度   End Sub