Option Explicit
' ########################################################
' กำหนดค่ารายการต่างๆให้กับ ComboBox
Sub InitComboBox()
' ########################################################
' เคลียร์ค่าข้อมูลของ ComboBox ออกให้หมด
ComboBox1.Clear
' ค่า Index ตัวแรกในรายการของ ComboBox จะเริ่มต้นเท่ากับ 0
ComboBox1.AddItem "กรุณาเลือกรายการ" ' Index = 0
ComboBox1.AddItem "รูปสี่เหลี่ยมจัตุรัส" ' Index = 1
ComboBox1.AddItem "รูปสี่เหลี่ยมผืนผ้า" ' Index = 2
ComboBox1.AddItem "รูปวงกลม" ' Index = 3
ComboBox1.AddItem "รูปวงรี" ' Index = 4
ComboBox1.AddItem "รูปสามเหลี่ยม" ' Index = 5
ComboBox1.AddItem "ภาพสัตว์ 2 ขา" ' Index = 6
ComboBox1.AddItem "ภาพสัตว์ 4 ขา" ' Index = 7
ComboBox1.AddItem "ไปยัง Slide ถัดไป" ' Index = 8
' จะทำให้ ComboBox.ListCount = 9 (ตั้งแต่ 0 - 8 = 9 ค่า)
' กำหนดให้ไปที่ Index = 0 เพื่อแจ้งข้อความให้เลือก
ComboBox1.ListIndex = 0
' เคลียร์ค่าข้อมูลการแสดงผลคำอธิบาย
Label1.Caption = ""
' ซ่อน Control ทั้ง 2 ตัวเอาไว้ก่อน ... เพื่อให้ผู้เรียนได้เลือกรายการ
Label1.Visible = False
Image1.Visible = False
End Sub
' ########################################################
' เมื่อมีการเลือกค่าต่างๆที่อยู่ภายใน ComboBox ก็จะเกิดเหตุการณ์เปลี่ยนแปลงค่า (Change)
Private Sub ComboBox1_Change()
' ########################################################
' อ่านรายละเอียดต่างๆของ Shape Collection Object
' ลบพวกที่เป็น AutoShape ออกไป ไม่ให้แสดงผลค้างเอาไว้ เช่น รูปสี่เหลี่ยม วงกลม ... นี่แหละที่เรียกว่า Dynamic
Call RemoveShape
Image1.Visible = False
Label1.Visible = True
Select Case ComboBox1.ListIndex
' แสดงรูปสี่เหลี่ยมจัตุรัส
Case 1
With ActivePresentation.Slides(2)
' ####################################################
' Type คือ การกำหนดกำหนดเป็นรูปสี่เหลี่ยม (หรืออื่นๆ)
' Left คือ การวางตำแหน่ง Shape ทางด้านซ้ายของจอ
' Top คือ การวางตำแหน่ง Shape ทางด้านบนของจอ
' Width คือการกำหนดความกว้างของ Shape
' Height คือการกำหนดความสูงของ Shape
' ดังนั้นไม่ว่าจะเป็นวงกลม (Oval) หรือ สี่เหลี่ยม (Rectangle) ความต่างของ Shape
' ก็จะอยู่การกำหนดความกว้าง กับ ความสูงนั่นเอง
' ####################################################
' ####################################################
' ตัวอย่างของการทำ CAI ในลักษณะของ Dynamic โดยการแสดงภาพสี่เหลี่ยมไม่ให้ซ้ำขนาด
' ทำให้เราสามารถนำไปสร้างโจทย์แบบฝึกหัดได้นั่นเอง ... พอมองเห็นภาพหรือยังครับ
' เริ่มต้นในการสุ่มตัวเลขก่อน
Randomize
Dim Axis As Integer
' สุ่มตัวเลข 1 - 4 ก็พอ เดี๋ยวภาพมันจะใหญ่เกินไป เพราะต้องไปคูณกับ 50 ซม. อีก (หน่วยวัด Metric)
' Width:=Axis * 50 และ Height:= Axis * 50
Axis = Int(4 * Rnd) + 1
' ####################################################
With .Shapes.AddShape( _
Type:=msoShapeRectangle, _
Left:=100, _
Top:=200, _
Width:=Axis * 50, _
Height:=Axis * 50 _
)
' ตั้งชื่อให้ Shape เอาไว้เพื่ออ้างอิงตอนเขียนโค้ด VBA
.Name = "Sqaure"
' เติมสีลงไปใน Shape
.Fill.Solid
' กำหนดสีให้กับ Shape ตามการผสมสีเป็นเลขฐาน 10 (ค่าระหว่าง 0 - 255)
' R=Red สีแดง, G = Green สีเขียว, B = Blue สีน้ำเงิน
.Fill.ForeColor.RGB = RGB(54, 100, 154)
End With
End With
' แสดงคำอธิบายของ Shape หรือ Image ... ประกอบการเรียน และ การสอนได้นั่นเอง
' vbCrLf คือ การขึ้นบรรทัดใหม่ เมื่อ Cr = Carriage Return และ Lf = Line Feed
Label1.Caption = "คำอธิบาย" & vbCrLf & "การหาพื้นที่รูปสี่เหลี่ยมจัตุรัส" & vbCrLf
Label1.Caption = Label1.Caption & "พ.ท.รูปสี่เหลี่ยมจัตุรัส = ด้าน x ด้าน" & vbCrLf
Label1.Caption = Label1.Caption & "เมื่อด้านแต่ละด้าน มีค่า = " & Axis & " หน่วย." & vbCrLf
Label1.Caption = Label1.Caption & "มีพื้นที่ = " & Axis * Axis & " หน่วย."
' ลักษณะของ Dynamic คือ มีการเปลี่ยนแปลงได้ตลอด
' ทำให้บทเรียนจะไม่ซ้ำซาก ผู้เรียนไม่ใช้วิธีท่องจำ ... ว่างๆผมจะเสริมให้อีกทีครับ ... เรื่องหมูๆ (ของผม)
' ที่เหลือส่วนอื่นๆ ก็เหมือนกันครับ ... อันนี้รูปสี่เหลี่ยมผืนผ้า
Case 2
With ActivePresentation.Slides(2)
With .Shapes.AddShape( _
Type:=msoShapeRectangle, _
Left:=100, _
Top:=200, _
Width:=120, Height:=80 _
)
.Name = "Rectangle"
.Fill.Solid
.Fill.ForeColor.RGB = RGB(0, 100, 0)
End With
End With
Label1.Caption = "คำอธิบาย" & vbCrLf & "การหาพื้นที่รูปสี่เหลี่ยมผืนผ้า" & vbCrLf
Label1.Caption = Label1.Caption & "พ.ท.รูปสี่เหลี่ยม = กว้าง x ยาว" & vbCrLf
Label1.Caption = Label1.Caption & "เช่น ... อะไรก็ว่าไป"
' รูปวงกลม
Case 3
With ActivePresentation.Slides(2)
With .Shapes.AddShape( _
Type:=msoShapeOval, _
Left:=100, _
Top:=200, _
Width:=100, _
Height:=100 _
)
.Name = "Circle"
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 100, 0)
End With
End With
Label1.Caption = "คำอธิบาย" & vbCrLf & "นี่คือรูปวงกลม"
' รูปวงรี
Case 4
With ActivePresentation.Slides(2)
With .Shapes.AddShape( _
Type:=msoShapeOval, _
Left:=100, _
Top:=200, _
Width:=100, _
Height:=60 _
)
.Name = "Oval"
.Fill.Solid
.Fill.ForeColor.RGB = RGB(255, 100, 154)
End With
End With
Label1.Caption = "คำอธิบาย" & vbCrLf & "นี่คือรูปวงรี"
' รูปสามเหลี่ยม
Case 5
With ActivePresentation.Slides(2)
With .Shapes.AddShape( _
Type:=msoShapeIsoscelesTriangle, _
Left:=100, _
Top:=200, _
Width:=100, _
Height:=100 _
)
.Name = "Circle"
.Fill.Solid
.Fill.ForeColor.RGB = RGB(154, 100, 154)
End With
End With
Label1.Caption = "คำอธิบาย" & vbCrLf & "นี่คือรูปสามเหลี่ยม"
' แสดงรูปภาพ
Case 6
With ActivePresentation.Slides(2)
With Image1
.Left = 100
.Top = 200
' ภาพทะลุพื้นหลัง (เฉพาะนามสกุล GIF ส่วน PNG ใช้กับ Image Control ไม่ได้)
.BackStyle = fmBackStyleTransparent
' ปรับภาพให้แสดงผลตามจริง (Stretch)
.PictureSizeMode = fmPictureSizeModeStretch
' ไม่มีกรอบ
.BorderStyle = fmBorderStyleNone
' กำหนดตำแหน่งของภาพมาแสดง คือ ตำแหน่งไฟล์ PPT ปัจจุบัน ตามด้วยโฟลเดอร์ images\ชื่อรูป
.Picture = LoadPicture(Application.ActivePresentation.Path & "\images\bird-01.gif")
' ให้สามารถมองเห็นได้
.Visible = True
End With
End With
Label1.Caption = "คำอธิบาย" & vbCrLf & "นี่คือภาพสัตว์ 2 ขา"
' แสดงรูปภาพ
Case 7
With ActivePresentation.Slides(2)
With Image1
.Left = 100
.Top = 200
.BackStyle = fmBackStyleTransparent
.PictureSizeMode = fmPictureSizeModeStretch
.BorderStyle = fmBorderStyleNone
.Picture = LoadPicture(Application.ActivePresentation.Path & "\images\cow-01.gif")
.Visible = True
End With
End With
Label1.Caption = "คำอธิบาย" & vbCrLf & "นี่คือภาพสัตว์ 4 ขา"
' ไปหน้าต่อไป
Case 8
Label1.Visible = False
Image1.Visible = False
' แล้วสั่งให้ไปยัง Slide ที่ต้องการโดยกำหนดจากหมายเลขประจำ Slide (ตัวอย่างมี 3 Slide)
ActivePresentation.SlideShowWindow.View.GotoSlide (3)
' หรือ สั่งผ่าน Next หากเป็น Slide ถัดไป
'ActivePresentation.SlideShowWindow.View.Next
Case Else
Label1.Visible = False
End Select
End Sub
' ########################################################
' โปรแกรมย่อยที่ใช้ในการลบพวก Shape ต่างๆ เช่น สี่เหลี่ยม วงกลม ออกไปจาก Slide
' เพื่อไม่ให้แสดงผลบน Slide ค้างเอาไว้ ... นี่ก็คือ Dynamic อีกเช่นกัน
' เห็นขีดความสามารถของ PowerPoint กันได้อย่างชัดเจนมั้ยครับ
Sub RemoveShape()
' ########################################################
' กำหนดตัวแปร Object แบบ Shape (รูปร่าง)
Dim oSH As Shape
' ไว้นับจำนวนของ Object ที่มีอยู่บน Slide ปัจจุบันทุกตัว
Dim X As Long
' นับ Object หรือ วัตถุทุกตัวที่อยู่ใน Slide ที่กำลังแสดงผล (ActivePresentation)
For X = ActivePresentation.Slides.Count To 1 Step -1
' เลือกเฉพาะ Object ที่เป็น Shape เท่านั้น
For Each oSH In ActivePresentation.Slides(X).Shapes
' หากทดสอบว่า Object ที่ตรวจพบเป็น Shape ก็ให้ลบออกไปจากหน้าจอ Slide ใน PowerPoint
If oSH.Type = msoAutoShape Then oSH.Delete
Next
Next
End Sub
' จบการทำงาน ปิด PowerPoint
Private Sub CommandButton1_Click()
' อ้างอิงชื่อไฟล์
With Application.Presentations("PowerPoint-ComboBox.ppt")
' สั่งบันทึก (Save) ก่อนออก
.Saved = True
.Close
End With
End Sub
|