-
โครงสร้างของไฟล์
.frm ในเวอร์ชันห้า
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0";
"MSFLXGRD.OCX"
Object = "{C1A8AF28-1257-101B-8FB0-0020AF039CA3}#1.1#0";
"MCI32.OCX"
ส่วนแรกของไฟล์
.frm เป็นส่วนบอกเวอร์ชันของภาษา และส่วนแสดงรายการของไฟล์เพิ่มเติม
ในกรณีนี้ msflxgrd.ocx และ mci32.ocx เป็นไฟล์ ocx มาตรฐานที่จะติดตั้งไว้ในระบบโดยอัตโนมัตอยู่แล้วแต่โปรแกรมเมอร์จะเรียกใช้หรือไม่ก็ได้ตามต้องการ
Begin VB.Form MIE
Appearance = 0 'Flat
AutoRedraw = -1 'True
BackColor = &H80000005&
BorderStyle = 3 'Fixed Dialog
Caption = "Project"
ClientHeight = 4350
ClientLeft = 900
ClientTop = 1185
ClientWidth = 7290
ControlBox = 0 'False
ส่วนถัดมาเป็นการประกาศคุณสมบัตของฟอร์ม
เช่น apperance ซึ่งในเวอร์ชันนี้กำหนดให้เป็นแบบธรรมดา หรือแบบสามมิติก็ได้
คุณสมบัติการทำ autoredraw ซึ่งจะวาดภาพบนฟอร์มใหม่เมื่อมีการซ้อนทับ
หรือการเปลี่ยนแปลงค่า (ภาพ) บนฟอร์มโดยอัตโนมัต
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 222
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
คุณสมบัติด้านตัวอักษรของฟอร์มเอง
(font property) ถูกกำหนดไว้ในส่วนถัดมา จะเห็นรายละเอียดว่ากำหนดได้ทุกอย่าง
ไม่ว่าจะเป็นขนาด น้ำหนัก และลักษณะตัวอักษรเป็นต้น
ForeColor = &H80000008&
Icon = "abc.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
MDIChild = -1 'True
MinButton = 0 'False
MousePointer = 1 'Arrow
PaletteMode = 1 'UseZOrder
ScaleHeight = 290
ScaleMode = 3 'Pixel
ScaleWidth = 486
ShowInTaskbar = 0 'False
ถัดมายังคงเป็น
property ของ form อยู่ เช่น scalemode ซึ่งกำหนดหน่วยวัดที่จะใช้
สามารถกำหนดเป็นเซ็น เป็นนิ้ว เป็น twip หรือจะเป็น pixel ตามตัวอยางข้างต้นนี้ก็ได้
mousepointer สามารถใช้ค่าที่มีไว้ให้ล่วงหน้าทั้งสิบสองแบบได้ หรือจะใช้แบบ
custom ซึ่งโปรแกรมเมอร์สามารถกำหนดไอค่อนใหม่ได้ตามใจชอบ
Begin VB.CommandButton Button
Caption = "Button"
บนฟอร์มตัวอย่างนี้มีปุ่มแบบ
commandbutton อยู่ด้วย ค่ากำหนด property ต่างๆ ของปุ่มสามารถป้อนไว้ได้โดยตรงโดยมีรูปแบบดังที่เห็นอยู่นี้
คือเริ่มโดยคำสั่ง begin แล้วตามด้วยชื่อ ซึ่งไม่ใช่ชื่อที่โปรแกรมเมอร์กำหนด
แต่เป็นชื่อมาตรฐานของ Visual Basic เอง จะเปลี่ยนเป็นอย่างอื่นไม่ได้
ส่วน caption คือข้อความที่จะปรากฎบนปุ่ม
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 222
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
การกำหนด
font property ทำได้โดยใช้ syntax เดียวกันกับ font ของฟอร์ม โดยปุ่มแต่ละปุ่ม
จะมี font property ของมันเองแยกต่างหากจากกัน
Height = 495
Index = 0
Left = 3240
MouseIcon = "Mie.frx":030A
MousePointer = 99 'Custom
Style = 1 'Graphical
TabIndex = 13
Top = 240
Visible = 0 'False
Width = 1095
End
คุณสมบัตอื่นๆ
ของปุ่มแบบ controlbuttonจะเห็นว่าคล้ายคลึงกับ form property เช่นการกำหนดตำแหน่งโดยใช้
top และ left การกำหนดขนาดโดยใช้ width และ height เป็นต้นส่วน property
กำหนด style เป็นของใหม่ในversion 5 (และ 6) ที่สามารถใส่ภาพลงในปุ่มได้โดยตรง
และนอกจากจะสนับสนุนภาพแบบ .bmp แล้ว ยังสามารถใช้กับภาพแบบ .jpg .gif
และ .wmf ได้ด้วยทำให้เหมาะสำหรับการเขียนโปรแกรทเพื่อทำงานร่วมกับ
internet
สำหรับ control แบบอื่นๆ เช่น picture, label, text box ผู้เขียนขอละไว้ในฐานที่เข้าใจ
เพียงแต่ขอให้สังเกตุว่า control แต่ละแบบ ย่อมจะมี property ที่เหมือนกัน
และไม่เหมือนกัน property ที่มีใน control ทุกแบบคือ visible, top,
left, width, height เป็นต้น ส่วน property บางแบบก็เป็นเฉพาะของ control
นั้นๆ เช่น Style จะมีเฉพาะใน commandbutton เท่านั้น
Attribute VB_Name = "ABC"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
ส่วนสุดท้ายของ
header ของไฟล์ .frm คือส่วนกำหนด attribute ต่างๆ เช่น VB_Name ซึ่งก็คือชื่อของฟอร์มนั่เอง
ส่วน Option Explicit เป็นการบอกให้เรารู้ว่าการประกาศตัวแปร (Declare
variable) จะต้องทำเต็ม คือละไว้ในฐานที่เข้าใจไม่ได้ มิฉะนั้นจะเกิด
error message ตอนคอมไพล์ และจะคอมไพล์ไม่ผ่าน
โค้ตสำหรับเล่นเสียง
การเล่นเสียงแบบ wave file ใน visual basic ทำได้โดยใส่ MCI control
ไว้ในฟอร์ม (โดยใช้วิธี ลากมาวาดใส่แบบ
visual หรือป้อนโค้นลงใน .frm ก็ได้) จากนั้นให้ใช้คำสั่งดังต่อไปนี้
WaveMM1.Command
= "Stop"
WaveMM1.Command = "Close"
WaveMM1.FileName = "bird.wav"
WaveMM1.Command = "Open"
WaveMM1.Command = "Play"
โปรแกรมในส่วน
form load
ส่วนแรกของโค้ตที่จะทำงานโดยอัตโนมัต
(ถ้าไม่ได้กำหนดให้เป็นอย่างอื่น) ก็คือ prodedure ชื่อ form load
ซึ่งจะมีอยู่ในทุกฟอร์ม ลองดูตัวอย่างข้างล่างนี้
-
Private Sub Form_Load()
Dim i As Integer
Dim j As Integer
Dim X As Single
Dim Message As String
Dim Directory As String
Dim text As String
Static s(1 To 4) As String
-
ส่วนแรกเป็นการ delcare ชนิดของข้อมูลให้ตัวแปรต่างๆ ที่จะใช้ใน
procedure นี้ ซึ่งvariable เหล่านี้จะมีช่วงชีวิตเฉพาะใน procedure
นี้เท่านั้น เมื่อโปรแกรมเคลื่อนการทำงานออกจาก procedure นี้แล้ว
ค่าที่เก็บไว้ใน variable เหล่านี้จะหมดไป
-
OldButton(0) = ""
For i = 1 To 20
Load Button(i)
OldButton(i) = ""
Next
-
โค้ตส่วนนี้เป็นการสร้าง array control โดยสร้างปุ่ม commandbutton
ขึ้นมายี่สิบเอ็ดปุ่ม ให้สังเกตุว่า commandbutton ทั้งยี่สิบเอ็ดปุ่มนี้จะมีชื่อเดียวกัน
แต่จะมี array index ไม่เหมือนกัน คือจะอยู่ระหว่าง >= 0 และ
<= 20
โค้ตเล่นภาพวีดิโอ
การเล่นภาพวิดิโอใน Visual Basic ทำได้หลายวิธี วิธีที่ง่ายที่สุดคือใส่
MCI Control ไว้ในฟอร์มแล้วใช้คำสั่งดังตัวอย่างข้างล่างนี้
-
FXVideo.VideoType = 0
FXVideo.FileName = "video\title.avi"
FXVideo.Command = 3 ' open
If FXVideo.Error = False Then
FXVideo.Visible = True
FXVideo.Move 0, 0
FXVideo.Command = 7 ' play
End If
คำสั่งสำหรับจัดฟอร์มให้อยู่กลางจอภาพ
การเขียนโปรแกรมใน Windows โปรแกรมเมอร์จะไม่สามารถทราบขนาดจองความละเอียดของจอภาพได้
เพราะผู้ใช้แต่ละระบบสามารถกำหนดความละเอียดได้ตามความสามารถของการ์ดจอภาพ
ซึ่งแต่ละรุ่นของการ์ดจอ จะสามารถกำหนดความละเอียดได้หลายขนาด แต่โปรแกรมเมอร์ยังสามารถหาตำแหน่งกลางจอได้โดยดูจากค่า
system value ชื่อ screen จึงทำให้คำนวญตำแหน่งกลางจอได้ดังนี้
Move (Screen.Width - Width) \ 2, (Screen.Height
- Height) \ 2
โปรแกรมทิ้งท้าย
ทิ้งท้ายบทความนี้ ผู้เขียนขอเสนอโปรแกรมทำเอนิเมชันง่ายๆ ซึ่งเทคนิคที่ใช้คือการใช้
control พิเศษสำหรับทำเอนิเมชันโดยเฉพาะจากไมโครซอฟท์ นั่นคือ Picclip
control ผู้อ่านสามารถใส่ control นี้ไว้ใน project ได้โดย add ไฟล์ชื่อ
picclp32.ocx
VERSION 5.00
Object = "{27395F88-0C0C-101B-A3C9-08002B2F49FB}#1.0#0";
"PICCLP32.OCX"
Begin VB.Form Form1
BackColor = &H00C0C0C0&
Caption = "Red Top"
ClientHeight = 2328
ClientLeft = 4200
ClientTop = 2640
ClientWidth = 3588
BeginProperty Font
Name = "MS Sans Serif"
Size = 8.25
Charset = 1
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Icon = "Redtop.frx":0000
LinkMode = 1 'Source
LinkTopic = "Form1"
MaxButton = 0 'False ScaleHeight
= 194
ScaleMode = 3 'Pixel
ScaleWidth = 299
Begin PicClip.PictureClip PictureClip2
Left = 120
Top = 1920
_ExtentX = 10393
_ExtentY = 4805
Rows = 3
Cols = 6
Picture = "Redtop.frx":030A
End
Begin VB.CommandButton onoff
Caption = "spin"
Height = 390
Left = 1845
TabIndex = 1
Top = 1095
Width = 1125
End
Begin VB.CommandButton Command1
Caption = "info"
Height = 345
Left = 1845
TabIndex = 2
Top = 705
Width = 1125
End
Begin VB.PictureBox Picture6
BackColor = &H00408080&
ForeColor = &H00000000&
Height = 1245
Left = 225
ScaleHeight = 100
ScaleMode = 3 'Pixel
ScaleWidth = 109
TabIndex = 3
TabStop = 0 'False
Top = 240
Width = 1350
Begin VB.PictureBox Picture1
Appearance = 0 'Flat
AutoSize = -1 'True
BackColor = &H80000005&
ForeColor = &H80000008&
Height = 720
Left = 60
ScaleHeight = 58
ScaleMode = 3 'Pixel
ScaleWidth = 67
TabIndex = 0
TabStop = 0 'False
Top = 60
Width = 825
End
End
Begin VB.Timer Timer1
Interval = 1
Left = 2235
Top = 120
End
Begin VB.PictureBox PictureClip1
Height = 480
Left = -3135
ScaleHeight = 432
ScaleWidth = 1152
TabIndex = 4
Top = 1560
Width = 1200
End
Begin VB.Image Image1
Height = 384
Index = 17
Left = 3720
Picture = "Redtop.frx":91CC
Top = 4428
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 16
Left = 3720
Picture = "Redtop.frx":94D6
Top = 3888
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 15
Left = 3660
Picture = "Redtop.frx":97E0
Top = 3348
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 14
Left = 3660
Picture = "Redtop.frx":9AEA
Top = 2808
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 13
Left = 3660
Picture = "Redtop.frx":9DF4
Top = 2268
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 12
Left = 3660
Picture = "Redtop.frx":A0FE
Top = 1728
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 11
Left = 3660
Picture = "Redtop.frx":A408
Top = 1188
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 10
Left = 3660
Picture = "Redtop.frx":A712
Top = 648
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 9
Left = 3660
Picture = "Redtop.frx":AA1C
Top = 108
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 8
Left = 3120
Picture = "Redtop.frx":AD26
Top = 4428
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 7
Left = 3120
Picture = "Redtop.frx":B030
Top = 3888
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 6
Left = 3120
Picture = "Redtop.frx":B33A
Top = 3348
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 5
Left = 3120
Picture = "Redtop.frx":B644
Top = 2808
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 4
Left = 3120
Picture = "Redtop.frx":B94E
Top = 2268
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 3
Left = 3120
Picture = "Redtop.frx":BC58
Top = 1728
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 2
Left = 3120
Picture = "Redtop.frx":BF62
Top = 1188
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 1
Left = 3120
Picture = "Redtop.frx":C26C
Top = 648
Visible = 0 'False
Width = 384
End
Begin VB.Image Image1
Height = 384
Index = 0
Left = 3120
Picture = "Redtop.frx":C576
Top = 108
Visible = 0 'False
Width = 384
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_TemplateDerived = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim y As Integer
Dim x As Integer
Dim delay As Integer
Dim toggle As Integer
Dim flap As Integer
Private Sub Command1_Click()
infoform.Show ' Load and display infoform.
End Sub
Private Sub Form_Load()
' Use the third frame of the sequence for the startup frame.
Picture1.Picture = PictureClip2.GraphicCell(2)
y = 2
' Center the smaller picture in the larger picture.
Picture1.Left = (Picture6.ScaleWidth - Picture1.Width) / 2
Picture1.Top = (Picture6.ScaleHeight - Picture1.Height) / 2
End Sub
Private Sub onoff_Click()
If toggle = 0 Then
onoff.Caption = "stop"
toggle = 1
Else
onoff.Caption = "spin"
toggle = 0
End If
End Sub
Private Sub runtop()
' Advance animation one frame.
y = y + 1: If y = 18 Then y = 0
Picture1.Picture = PictureClip2.GraphicCell(y)
' Icon animation: This will only be evident when the
' form is minimized. Icon (.ICO) files, rather than .BMP files,
' have been loaded into the Picture3 array. This allows
' the mask feature of the icon file to be utilized, letting
' the background behind the icon show through.
Form1.Icon = Image1(y).Picture
End Sub
Private Sub Timer1_Timer()
If toggle = 1 Then runtop
End Sub
|