网上有如下代码,生成字体窗体
'API声明
Private Declare Function MoveTo Lib "gdi32" Alias "MoveToEx" ( _
     ByVal hdc As Long, ByVal x As Long, ByVal y As Long, _
     Optional lpPoint As Any = 0) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    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 GetRgnBox Lib "gdi32" _
    (ByVal hRgn As Long, lpRect As RECT) As Long
    Private Declare Function CreateRectRgnIndirect Lib "gdi32" _
    (lpRect As RECT) 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 Const RGN_AND = 1
    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 ReleaseCapture Lib "user32" _
    () 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 Const WM_NCLBUTTONDOWN = &HA1
    Private Const HTCAPTION = 2
    '窗体代码
    Private Sub Form_Load()
    Dim hRgn1, hRgn2 As Long
    Dim rct As RECT
    With Me
    .Font.Name = "宋体"
    .Font.Size = 200
    .FontTransparent = True
    '读者可设置为False观察其效果
    End With
    BeginPath hdc
    '为窗体形状产生路径
'******************************************
'*    TextOut hdc, 20, 20, "国", 2       '*
'******************************************
    EndPath hdc
    hRgn1 = PathToRegion(hdc)
    '将指定路径转换为区域
    GetRgnBox hRgn1, rct
     '获取完全包含指定区域的最小矩形
    hRgn2 = CreateRectRgnIndirect(rct) '创建rct确定的矩形区域
    CombineRgn hRgn2, hRgn2, hRgn1, RGN_AND
     DeleteObject hRgn1
    '删除GDI对象,释放占用的系统资源
    SetWindowRgn hwnd, hRgn2, 1
    End Sub
本人把用星号(*)包装的一句替换成以下代码,却不能生成矩形窗体,请问何解?
'***************************
'*   For y = 30 To 200    '*
'*   MoveTo hdc, 30, y    '*
'*   LineTo hdc, 200, y   '*
'*   Next y               '*
'***************************

解决方案 »

  1.   

    其实你可以通过直接创建一个区域来实现啊,没有必要用通道(Path)通道是为了更复杂的形状(例如输出文字)而使用的。
      

  2.   

    看你要组合什么复杂形状了,如果要组合复杂形状,可以考虑用PolyGon之类的函数,另外用LineTo的话,你的Line需要组合成一个闭合形状,例如:            LineTo Me.hdc, 200, 0   '*
                LineTo Me.hdc, 200, 200
                
                LineTo Me.hdc, 0, 200
                
                LineTo Me.hdc, 0, 0
      

  3.   

    楼主使用:MOVETOEX这个API吧,我用MOVETOEX和LINTTO这两个API的。没有任何问题
      

  4.   

    同意楼上的,你可以看一下BeginPath的帮助,MoveTo是不被BeginPath支持的,只有MOVETOEX被支持。
      

  5.   

    我的图形需要逐点画上,例如
    Dim pp as POINTAPI
    For x=0 to 200
    For y=0 to 200
    MovetoEx hdc,x,y,pp
    Lineto hdc,x,y
    Next x
    Next y这样为何不行?
      

  6.   

    你没有形成封闭区域,那么PathToRegion创建出来的区域就是空区域,你可以在
    GetRgnBox hRgn1, rct
    后面加上一句:
        If IsRectEmpty(rct) Then
            MsgBox "Empty Rect"
        End If可以看到获得的外接矩形是空的。