要求:单击窗体,在任意位置显示任意大小,任意颜色的五角星
由于没有直接画出五角星的控件,我想到了在窗体上输出一个五角星(键盘中的特殊符号★)
问题是:窗体上直接通过print输出的字符是无法改变大小和颜色的,这让我又想通过标签label中的caption来显示★,但受限于以下几个问题:
1)★的大小受限于标签的大小,不可能达到任意大小
2)label标签没有currentX和currentY属性,不能实现在任意位置显示的目的
我写了点,但都是错的:
Private Sub Form_Click()
Label1.Caption = "★"
Label1.ForeColor = RGB(Int(Rnd(256)), Int(Rnd(256)), Int(Rnd(256)))
Label1.FontSize = Int(Rnd(24))
Printer.CurrentX = Int(Rnd(4801))
Printer.CurrentY = Int(Rnd(3601))
End Sub
不知道大家能否帮我实现这个窗体单击代码?
谢谢了。
由于没有直接画出五角星的控件,我想到了在窗体上输出一个五角星(键盘中的特殊符号★)
问题是:窗体上直接通过print输出的字符是无法改变大小和颜色的,这让我又想通过标签label中的caption来显示★,但受限于以下几个问题:
1)★的大小受限于标签的大小,不可能达到任意大小
2)label标签没有currentX和currentY属性,不能实现在任意位置显示的目的
我写了点,但都是错的:
Private Sub Form_Click()
Label1.Caption = "★"
Label1.ForeColor = RGB(Int(Rnd(256)), Int(Rnd(256)), Int(Rnd(256)))
Label1.FontSize = Int(Rnd(24))
Printer.CurrentX = Int(Rnd(4801))
Printer.CurrentY = Int(Rnd(3601))
End Sub
不知道大家能否帮我实现这个窗体单击代码?
谢谢了。
Randomize
ForeColor = RGB(Int(Rnd(1) * 256), Int(Rnd(1) * 256), Int(Rnd(1) * 256))
FontSize = Int(Rnd(1) * 2160)
CurrentX = Int(Rnd(1) * 4801)
CurrentY = Int(Rnd(1) * 3601)
Print "★"
End Sub
x As Long
y As Long
End Type
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const ALTERNATE = 1
Const WINDING = 2
Const BLACKBRUSH = 4
Private Sub Form_Paint()
Dim poly(1 To 10) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
Me.Cls
NumCoords = 10
Me.ScaleMode = vbPixels
Dim arr(1 To 10) As String
Dim i As Integer
Dim index As Integer
index = 1
arr(index) = "140,0"
index = index + 1
arr(index) = "185,82"
index = index + 1
arr(index) = "280,105"
index = index + 1
arr(index) = "215,171"
index = index + 1
arr(index) = "227,270"
index = index + 1
arr(index) = "140,228"
index = index + 1
arr(index) = "51,270"
index = index + 1
arr(index) = "61,171"
index = index + 1
arr(index) = "0,105"
index = index + 1
arr(index) = "94,82"
For i = 1 To 10
poly(i).x = Split(arr(i), ",")(0)
poly(i).y = Split(arr(i), ",")(1)
Next i
Polygon Me.hdc, poly(1), NumCoords
hBrush = GetStockObject(BLACKBRUSH)
hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
If hRgn Then FillRgn Me.hdc, hRgn, hBrush
DeleteObject hRgn
End Sub
Private Sub Form_Resize()
Form_Paint
End Sub
Option ExplicitPrivate Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W As Long, ByVal I As Long, ByVal u As Long, ByVal s As Long, ByVal C As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, ByVal F As String) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Sub Form_Click()
Dim I As Integer
For I = 1 To 10
star Int(Rnd * 110 + 20), Int(Rnd * 3600), Int(Rnd * Width), Int(Rnd * Height), QBColor(Int(Rnd * 16))
Next
End Sub
Sub star(ByVal size As Integer, ByVal angle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal colr As ColorConstants)
Dim mf As Long
mf = CreateFont(size, 0, angle, 0, 400, False, False, False, 1, 0, 1, 2, 0, "mf")
SelectObject hdc, mf
CurrentX = x: CurrentY = y
ForeColor = colr
Print "★"
DeleteObject mf
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Randomize
Label1.Visible = False
Label1.FontSize = Int(100 * Rnd + 5)
Label1.ForeColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
Label1.Move X, Y
Label1.Visible = True
End Sub
用 Label 也大同小异,没有难度。
Option Explicit'设计时把窗口弄得稍大些
Private Sub Form_Click()
Dim s&, c&
Cls
Randomize
c = RGB(Rnd() * 255, Rnd() * 255, Rnd() * 255) '颜色
s = 30 + 270 * Rnd() '随机产生一个字体大小
CurrentX = (ScaleWidth - s) * Rnd()
CurrentY = (ScaleHeight - s) * Rnd()
FontSize = s
ForeColor = c
Print "★"
End SubPrivate Sub Form_Load()
ScaleMode = 3
End Sub
现在我想改进一下,要求保留在窗体上生成的所有随机五角星,不知道3位兄弟有何见解?Private Sub Form_Click()
Randomize
Label1.Move Int(Rnd * Form1.ScaleWidth + 1), Int(Rnd * Form1.ScaleHeight + 1)
Label1.ForeColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
Label1.FontSize = Int(Rnd * 300)
End Sub
谢谢了!
还是同样的问题,希望4位兄弟帮我考虑这个问题:
要求保留在窗体上生成的所有随机五角星,即第n次单击鼠标要求窗体显示n个五角星
Randomize
ForeColor = RGB(Int(Rnd(1) * 256), Int(Rnd(1) * 256), Int(Rnd(1) * 256))
FontSize = Int(Rnd(1) * 500)
CurrentX = X - TextWidth("★") / 2
CurrentY = Y - TextHeight("★") / 2
Print "★"
End Sub
将form的autoredraw属性设置为true
一、俺认为,学啥之前,最重要的是,先弄明白“手册(Manual)”。手册还没弄明白,就来论坛问,完全颠倒了学习的次序。例如:
1. 审题,人家题目如果要求“绘制五角星”,就绝对不要搞什么“五角星字符”,绕过问题绝对不是新手的求解之道。
2. Label的一个简单AutoSize就解决了大小问题,为什么说“不能改变大小”呢?
3. label标签没有currentX和currentY属性,不能实现在任意位置显示的目的。难道非要CurrentX和Y属性,才能实现“任意位置”?怪透了。
二、俺认为,设计啥之前,都老老实实,踏踏实实地“临摹”几个作品。不要一开始就想当然。临摹过程中,逼着自己死啃手册,把手册吃透了,知道各种道具(“控件”等)是干嘛用的了,问题自然迎刃而解。 是不点太不用心了。呵呵。
自己看看动态生成控件的吧,比较简单,不考虑n无穷大,你想怎么画怎么保存都没问题,还能实现输入N数字,第N个已经画的五角星变颜色。
从大年初一开始学的VB,边看教材边做,现在只是先基本了解了内容,知道变量、数据类型、控件以及常用的结构,过程、函数、图形控件、菜单和数据库。各个部分都不是了解地很透彻,所以会产生一些大家看起来很简单的问题。请见谅了。