注册 登录
编程论坛 VB6论坛

gdi 绘图的问题

afeibfp 发布于 2014-05-31 01:20, 505 次点击
在dc里绘制一个矩形,外框是否绘制根据设置分别用LineTo绘制,包括外框的粗细,代码如下:
程序代码:
Public Sub DrawRectBox(hdc As Long)
Dim cRect As RECT
Dim hBrushFill As Long
Dim hNewPen As Long
Dim tLB As LOGBRUSH
Dim off1 As Long, off2 As Long

    'Debug.Print Me.Name
    Call SetRect(cRect, 8, 8, 112, 64)  '设置绘制的矩形
   
    '根据边框粗细设置画笔
    tLB.lbColor = GetSysColor(COLOR_WINDOWTEXT)
    hNewPen = ExtCreatePen(PS_GEOMETRIC Or PS_ENDCAP_FLAT Or PS_SOLID, mlBorderSize, tLB, 0, ByVal 0&)
    hNewPen = SelectObject(hdc, hNewPen)

    '根据边框大小(1-4),计算偏移
    off1 = mlBorderSize \ 2: off2 = mlBorderSize - off1 - 1
    Debug.Print off1, off2

    If (mlBorderStyle And afrEdgeTop) <> 0 Then     '上边
        Call MoveToEx(hdc, cRect.Left - off1, cRect.Top, 0&)
        Call LineTo(hdc, cRect.right + off2, cRect.Top)
    End If
   
    If (mlBorderStyle And afrEdgeBottom) <> 0 Then  '下边
        Call MoveToEx(hdc, cRect.Left - off1, cRect.bottom, 0&)
        Call LineTo(hdc, cRect.right + off2, cRect.bottom)
    End If
    If (mlBorderStyle And afrEdgeLeft) <> 0 Then    '左边
        Call MoveToEx(hdc, cRect.Left, cRect.Top - off1, 0&)
        Call LineTo(hdc, cRect.Left, cRect.bottom + off2)
    End If
    If (mlBorderStyle And afrEdgeRight) <> 0 Then   '右边
        Call MoveToEx(hdc, cRect.right, cRect.Top - off1, 0&)
        Call LineTo(hdc, cRect.right, cRect.bottom + off2)
    End If

    hNewPen = SelectObject(hdc, hNewPen)    '删除gdi对象
    Call DeleteObject(hNewPen)
End Sub


但是绘制直线的位置是错误的,可是令人惊奇的是在代码前添加 debug.print me.name 时却绘制正确了。图见附件
根据debug.print 可以看到off1,off2的值已经发生变化了
只有本站会员才能查看附件,请 登录

这到底是为什么???
1 回复
#2
afeibfp2014-05-31 03:09
已经把bug定位到MoveToEx了,请看这个帖子:https://bbs.bccn.net/viewthread.php?tid=432302&extra=page%3D1&frombbs=1
1