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

wdthkyou 2007-05-05 11:43:40
示意图:
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 |
|
---------------------------------------
|
|
|
显然第一行文字在左上顶头部份靠右了,没有居中;第二行文字却与第一行文字起始点一样了,应该向右缩进几个空格,因为第二行文字要短一些。真郁闷,那点出错了,请大虾们指点一下。
...全文
224 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wdthkyou 2007-05-05
  • 打赏
  • 举报
回复
Pic.Print , cTitle 应改为
Pic.Print , "2007年5月5日"
抱歉啊。
wdthkyou 2007-05-05
  • 打赏
  • 举报
回复
这样可行,结贴了。
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 = 10
fnt = 8
dd = prnt(x, y, fnt, txt)
txt = "分布图"
LTxt = TextWidth(txt)
x = (Pic.Width / 2 - LTxt) / 2
y = 15 + TextHeight("分布图")
dd = prnt(x, y, fnt, txt)
End Sub
zdingyun 2007-05-05
  • 打赏
  • 举报
回复
你的代码忽略了PICTUREBOX坐标与TextWidth()函数输出值的差异,因而造成偏差。
是指Pic.Width与Pic.ScaleWidth 两者的差异。
rainstormmaster 2007-05-05
  • 打赏
  • 举报
回复
用DrawText吧,可以利用它算出文本打印的矩形大小,也可以直接利用它打印,具体的自己看看MSDN

另外,如果按照你的思路的话,要注意TextWidth的返回值和ScaleMode有关,同时也要注意,在改变了font后,要重新计算TextWidth
zdingyun 2007-05-05
  • 打赏
  • 举报
回复
按MSDN方法:
Private Sub Command1_Click()
Dim HalfHeight, HalfWidth, Msg ' 声明变量。
Pic.AutoRedraw = -1 ' 打开AutoRedraw。
Pic.BackColor = QBColor(4) ' 设置背景颜色。
Pic.ForeColor = QBColor(15) ' 设置前景颜色。
Msg = "2007年5月5日" ' 创建信息。
'Pic.FontSize = 30 ' 设置字体大小。
HalfWidth = Pic.TextWidth(Msg) / 2 ' 计算半宽。
HalfHeight = Pic.TextHeight(Msg) / 2 ' 计算半高。
Pic.CurrentX = Pic.ScaleWidth / 2 - HalfWidth ' 设置 X。
Pic.CurrentY = 100 'Pic.ScaleHeight / 2 - HalfHeight ' 设置Y。
Pic.Print Msg ' 打印信息。
Msg = "分布图"
HalfWidth = Pic.TextWidth(Msg) / 2 ' 计算半宽。
HalfHeight = Pic.TextHeight(Msg) / 2 ' 计算半高。
Pic.CurrentX = Pic.ScaleWidth / 2 - HalfWidth ' 设置 X。
Pic.CurrentY = 400 'Pic.ScaleHeight / 2 - HalfHeight ' 设置Y。
Pic.Print Msg ' 打印信息。
End Sub
zdingyun 2007-05-05
  • 打赏
  • 举报
回复
代码简化:
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
zdingyun 2007-05-05
  • 打赏
  • 举报
回复
你的代码忽略了PICTUREBOX坐标与TextWidth()函数输出值的差异,因而造成偏差。
下面胳给出代码可解决你的问题:
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()
LTxt = TextWidth("2007年5月5日")
Text1 = LTxt
x = Pic.Width / 2 - LTxt / 2
y = 100
fnt = 8
txt = "2007年5月5日"
dd = prnt(x, y, fnt, txt)
LTxt = TextWidth("分布图")
txt = "分布图"
x = Pic.Width / 2 - LTxt / 2
y = 400
dd = prnt(x, y, fnt, txt)
End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧