首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

小弟我的控制两行文字打印在picture的左上顶头部份的居中位置失败了

2012-03-29 
我的控制两行文字打印在picture的左上顶头部份的居中位置失败了示意图:xxxxxxxxxx|yyyy||----------------

我的控制两行文字打印在picture的左上顶头部份的居中位置失败了
示意图:
        xxxxxxxxxx         |
              yyyy               |
                                    |
---------------------------------------
                                    |
                                    |
                                    |
代码如下(Pic为picture控件):
Dim   LTxt   As   Long
LTxt   =   TextWidth( "2007年5月5日 ")
With   Pic
    .Cls
    .CurrentX   =   (Pic.Width   \   2   -   LTxt)   \   2
    .CurrentY   =   10
    Pic.Print   ,   cTitle
    LTxt   =   TextWidth( "分布图 ")
    .CurrentX   =   (Pic.Width   \   2   -   LTxt)   \   2
    .CurrentY   =   15   +   TextHeight( "分布图 ")
    Pic.Print   ,   "分布图 "
End   With
可是出现的是这种情况:
              xxxxxxxxxx   |
              yyyy               |
                                    |
---------------------------------------
                                    |
                                    |
                                    |
显然第一行文字在左上顶头部份靠右了,没有居中;第二行文字却与第一行文字起始点一样了,应该向右缩进几个空格,因为第二行文字要短一些。真郁闷,那点出错了,请大虾们指点一下。

[解决办法]
代码简化:
Option Explicit
Dim LTxt As Long
Dim x, y As Long
Dim txt As String
Dim dd As Variant
Dim fnt As Integer

Public Function prnt(x As Variant, y As Variant, fnt As Variant, txt As Variant)
Pic.CurrentX = x
Pic.CurrentY = y
Pic.FontSize = fnt
Pic.Print txt
End Function

Private Sub Command1_Click()
txt = "2007年5月5日 "
LTxt = TextWidth(txt)
x = Pic.Width / 2 - LTxt / 2
y = 100
fnt = 8
dd = prnt(x, y, fnt, txt)
txt = "分布图 "
LTxt = TextWidth(txt)
x = Pic.Width / 2 - LTxt / 2
y = 400
dd = prnt(x, y, fnt, txt)
End Sub

[解决办法]
用DrawText吧,可以利用它算出文本打印的矩形大小,也可以直接利用它打印,具体的自己看看MSDN

另外,如果按照你的思路的话,要注意TextWidth的返回值和ScaleMode有关,同时也要注意,在改变了font后,要重新计算TextWidth

热点排行