软件注册站
热情软件屋

 
是否有改变Form外形(如圆形)的方法
编号: QA000260    
建立日期: 1998年12月26日 最后修改日期: 2000年12月11日
所属类别: Visual Basic - 窗体与菜单
   
    是否有改变Form外形(如圆形)的方法?(fsf)
   
    利用Windows API的SetWindowRgn可以做这件事,下面就是一个这方面的例子。
    Option Explicit
    Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
    Private Declare Function CreateRectRgn Lib "gdi32" (ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
    Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
    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 CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode 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 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Type POINTAPI
     X As Long
     Y As Long
    End Type
    Private Const RGN_AND = 1
    Private Const RGN_COPY = 5
    Private Const RGN_DIFF = 4
    Private Const RGN_OR = 2
    Private Const RGN_XOR = 3
    Private Function CreateFormRegion() As Long
     Dim ResultRegion As Long, HolderRegion As Long, ObjectRegion As Long, nRet As Long
     Dim PolyPoints() As POINTAPI
     ResultRegion = CreateRectRgn(0, 0, 0, 0)
     HolderRegion = CreateRectRgn(0, 0, 0, 0)
    
    
    '!Shaped Form Region Definition
    '!3,16,105,423,328,0,0,1
     ObjectRegion = CreateEllipticRgn(105 * 12 / Screen.TwipsPerPixelX, 16 * 12 / Screen.TwipsPerPixelY, 423 * 12 / Screen.TwipsPerPixelX, 328 * 12 / Screen.TwipsPerPixelY)
     nRet = CombineRgn(ResultRegion, ObjectRegion, ObjectRegion, RGN_COPY)
     DeleteObject ObjectRegion
     CreateFormRegion = ResultRegion
    End Function
    
    Private Sub Form_Load()
     Dim nRet As Long
     nRet = SetWindowRgn(Me.hWnd, CreateFormRegion, True)
     'If the above two lines are modified or moved a second copy of
     'them may be added again if the form is later Modified by VBSFC.
    End Sub
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'Next two lines enable window drag from anywhere on form. Remove them
    'to allow window drag from title bar only.
     ReleaseCapture
     SendMessage Me.hWnd, &HA1, 2, 0&
    End Sub
    

    孙亮朝朋友提供了一个程序formshape.zip,这个程序可以工具你指定的图形生成任意形状的窗口。你也可以在http://www.ComPorts.com/AlexV/VBSFC.html下载VB Shaped Form Creator (VBSFC),这个工具可以帮助你生成全部代码。
    通常奇怪形状的Form都不使用Windows默认的标题条,你需要在Form上绘制标题条。这就带来一个如何拖动没有标题条的Form的问题,你可以参考QA000091 "如何实现浮动没有标题的窗体"

    

此问题由李海回答。

附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, 窗体与菜单, form, window, tform

   
 
把这个问题推荐给朋友
   
 
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio