[VB6] การทำทูลบาร์ (ToolBar) ด้วย CodeJock ระดับมืออาชีพ (ภาค 2)
http://www.g2gnet.com/webboard/images/vb6/codejock/codejocktoolbar2.pngต่อจากภาค 1 การทำทูลบาร์ (ToolBar) ด้วย CodeJock ระดับมืออาชีพ มาอย่างทันทีทันใด เพราะเห็นหลายคนให้ความสนใจในการใช้งาน CodeJock เข้ามากันอย่างล้นหลาม คือแทบจะแยกไม่ออกเลยว่าหน้าตาโปรแกรมอันสวยงามที่ได้รับชมกันไปนั้น มันทำมาจาก Visual Studio 98 หาใช่กลุ่มตระกูลไฮโซ Dot Net แต่ประการใดไม่ ... สำหรับตอนนี้ก็ใช้โค้ดแทบจะเหมือนเดิม แต่จะเปลี่ยนจากการเลือกใช้ไอคอนทีละตัว มาเป็นกราฟิคแทน แน่นอนว่าแอดมินจะไม่สอนการใช้งาน Photoshop อันนี้ทุกๆท่านต้องไปศึกษากันเอาเองครับ ...
http://www.g2gnet.com/webboard/images/vb6/codejock/codejockpsd.png
ภาพไอคอนแต่ละตัวจะมีขนาด 32 x 32 Pixel ซึ่งในตัวอย่างจะมีทูลบาร์อยู่ทั้งหมด 6 ตัว ก็เอา 32 x 6 = ความกว้าง 192 Pixel ... (แอดมินแนบไฟล์ PSD เอาไว้ให้เป็นตัวอย่างในไฟล์ดาวน์โหลด)
ดาวน์โหลด CodeJock Suite Pro for ActiveX/COM ได้ที่นี่ (เฉพาะสมาชิกเท่านั้น)
http://www.g2gnet.com/webboard/images/vb6/codejock/codejocktoolbarnew.png
ขอให้สังเกตการแสดงผลกับภาค 1 ...
'/ โหลดภาพ PNG เข้ามาให้กับ ImageManager
ImageManager1.Icons.LoadBitmap App.Path & "\Icons\images2018.png", icoIconArray, xtpImageNormal
'/ ส่งให้กับ CommandBars อีกที เพื่อให้ ToolBar แสดงภาพ Icon ได้
CommandBars1.Icons = ImageManager1.Iconsการนำภาพบิตแมพมาใช้แทนไอคอน ...
มาดูโค้ดกันเถอะ ...
' / -----------------------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' / Microsoft Visual Basic 6.0 Service Pack 6
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can modify and/or distribute without to inform the developer.
' / -----------------------------------------------------------------------------------------------
Option Explicit
Private Declare Function InitCommonControls Lib "Comctl32.dll" () As Long
'/ การสร้าง TabToolBar
Private WithEvents TabToolBar As XtremeCommandBars.TabToolBar
'/ การสร้าง StatusBar
Dim WithEvents StatusBar As XtremeCommandBars.StatusBar
' / -----------------------------------------------------------------------------------------------
' / เหตุการณ์ในการคลิ๊กเลือก ToolBar หรือ MenuBar เพื่อสั่งให้ไปทำงานอย่างอื่น
' / -----------------------------------------------------------------------------------------------
Private Sub CommandBars1_Execute(ByVal Control As XtremeCommandBars.ICommandBarControl)
On Error Resume Next
Select Case Control.Id
' กลุ่มนี้ คือ TabToolBar ค่าคงที่ถูกกำหนดไว้ในไฟล์ Resource.bas
Case ID_USER_TOOLS_1:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 1"
Case ID_USER_TOOLS_2:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 2"
Case ID_USER_TOOLS_3:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 3"
Case ID_USER_TOOLS_4:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 4"
Case ID_USER_TOOLS_5:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 5"
Case ID_USER_TOOLS_6:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 5 และกำลังจะจบโปรแกรม"
End
End Select
End Sub
Private Sub MDIForm_Activate()
frmMain.Caption = "CodeJock Xtreme Suite Pro - Code bY Thongkorn TubTimkrob"
StatusBar.IdleText = "Developer : thongkorn@hotmail.com - www.facebook.com/g2gnet"
End Sub
Private Sub MDIForm_Initialize()
InitCommonControls
End Sub
Private Sub MDIForm_Load()
'/ หากเปิดโปรแกรมนี้ซ้ำกัน จะไม่ให้ตัวใหม่ขึ้นมาทำงาน
If App.PrevInstance Then End
'/ การตั้งค่าคุณสมบัติ เพื่อใช้ประโยชน์ในส่วนของพารามิเตอร์ภายในของ App
'/ เช่น App.Title, App.Path โดยใช้งานผ่าน CommandBars Control
CommandBarsGlobalSettings.App = App
'/ CommandBarsGlobalSettings มี Properties ให้ใช้งานได้หลากหลายมากครับ เช่น ทำโปรแกรมหลายภาษา
'/ ตัวอย่าง ...CommandBarsGlobalSettings.ResourceFile = "XTPResourceArSa.dll"
'/ ตัวแปร Control ในการสร้างกลุ่มของ ToolBar
Dim Control As CommandBarControl
Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
'/ ไม่ต้องการให้เมนูบาร์เคลื่อนที่ได้ (Stretched)
CommandBars1.ActiveMenuBar.EnableDocking xtpFlagStretched
CommandBars1.Options.KeyboardCuesShow = xtpKeyboardCuesShowWindowsDefault
CommandBars1.EnableCustomization False
' ===============================================================================
' แสดง Theme ซึ่งต้องอาศัยไฟล์ CodeJock ไฟล์เหล่านี้จะอยู่ที่โฟลเดอร์ Styles
SkinFramework1.LoadSkin App.Path + "\Styles\Office2007.cjstyles", "NORMALBLUE.ini"
'SkinFramework1.LoadSkin App.Path + "\Styles\Vista.cjstyles", "NORMALBLUE.INI"
'SkinFramework1.LoadSkin App.Path + "\Styles\Vista.cjstyles", "NORMALBLACK.INI"
'SkinFramework1.LoadSkin App.Path + "\Styles\Vista.cjstyles", "NORMALSILVER.INI"
' ===============================================================================
SkinFramework1.ApplyWindow (Me.hWnd)
SkinFramework1.ApplyOptions = SkinFramework1.ApplyOptions Or xtpSkinApplyMetrics
CommandBars1.PaintManager.RefreshMetrics
CommandBars1.RecalcLayout
' ===============================================================================
'/ เพิ่มรายการ Tabbed ToolBar
Set TabToolBar = CommandBars1.AddTabToolBar("รายการหลัก")
'/ ไม่อนุญาตให้เพิ่ม/ลบ (Add or Remove Buttons) ที่มันจะอยู่ทางด้านขวามือสุดของ TabToolBar
TabToolBar.ShowExpandButton = False
'/ กำหนดความกว้างขั้นต่ำสุดของ ToolBar แต่ละตัว (หน่วยวัด Pixels)
TabToolBar.MinimumWidth = 400
'/ เริ่มต้นการเพิ่มปุ่ม Control เข้ามายัง Tabbed ToolBar
With TabToolBar
Dim iCount As Integer
'/ สร้างรายการ ToolBar ขึ้นมา โดยใช้ชื่อกลุ่ม "User Tools" มีทั้งหมด 6 ตัว
'/ ค่าคงที่ ID_USER_TOOLS แต่ละตัว เราไปกำหนดล่วงหน้าเอาเองที่ Resource.bas
For iCount = ID_USER_TOOLS_1 To ID_USER_TOOLS_6
Select Case iCount
Case 801: Set Control = .Controls.Add(xtpControlButton, iCount, "รายการที่ 1")
Case 802: Set Control = .Controls.Add(xtpControlButton, iCount, "รายการที่ 2")
Case 803: Set Control = .Controls.Add(xtpControlButton, iCount, "รายการที่ 3")
Case 804: Set Control = .Controls.Add(xtpControlButton, iCount, "รายการที่ 4")
Case 805: Set Control = .Controls.Add(xtpControlButton, iCount, "รายการที่ 5")
Case 806: Set Control = .Controls.Add(xtpControlButton, iCount, "จบโปรแกรม")
End Select
Control.Category = "User Tools"
Next
End With
'/ ประกาศตัวแปร Array ตามจำนวน ToolBar ที่ต้องการสร้างขึ้นมา เพื่อแสดงรูป Icon
'/ ในตัวอย่างมี 6 ตัว โดยเริ่มต้นจาก 0 ถึง 5
Dim icoIconArray(0 To 5) As Long
icoIconArray(0) = ID_USER_TOOLS_1
icoIconArray(1) = ID_USER_TOOLS_2
icoIconArray(2) = ID_USER_TOOLS_3
icoIconArray(3) = ID_USER_TOOLS_4
icoIconArray(4) = ID_USER_TOOLS_5
icoIconArray(5) = ID_USER_TOOLS_6
' โหลดภาพ PNG เข้ามาให้กับ ImageManager
ImageManager1.Icons.LoadBitmap App.Path & "\Icons\images2018.png", icoIconArray, xtpImageNormal
' ส่งให้กับ CommandBars อีกที เพื่อให้ ToolBar แสดงภาพ Icon ได้
CommandBars1.Icons = ImageManager1.Icons
'/ ตั้งค่าขนาด Icon ตามที่เราออกแบบเอาไว้ แต่ละตัวจะมีขนาด 32x32 Pixel
TabToolBar.SetIconSize 32, 32
TabToolBar.ShowTextBelowIcons = True
'/ ไม่ให้เกิดการเคลื่อนที่ของ ToolBar ได้
TabToolBar.EnableDocking xtpFlagStretched
TabToolBar.Closeable = False
TabToolBar.Customizable = False
' ======================= ToolTips ==========================
Dim ToolTipContext As ToolTipContext
Set ToolTipContext = CommandBars1.ToolTipContext
ToolTipContext.Style = xtpToolTipOffice2007
ToolTipContext.ShowTitleAndDescription True, xtpToolTipIconNone
' ตั้งค่าตำแหน่งของ ToolTip
'ToolTipContext.SetMargin 2, 2, 2, 2
ToolTipContext.MaxTipWidth = 180
ToolTipContext.ShowShadow = True
' ======================= ToolTips ==========================
'/ การสร้าง StatusBar ที่อยู่ด้านล่างของฟอร์ม
Set StatusBar = CommandBars1.StatusBar
StatusBar.Visible = True
StatusBar.AddPane 0
'/ ค่าคงที่ๆกำหนดไว้ใน Resource.bas และเป็นค่าสงวนที่เปลี่ยนแปลงไม่ได้
StatusBar.AddPane ID_INDICATOR_CAPS
StatusBar.AddPane ID_INDICATOR_NUM
StatusBar.AddPane ID_INDICATOR_SCRL
StatusBar.IdleText = "Developer : thongkorn@hotmail.com - www.facebook.com/g2gnet"
End Sub
Private Sub mnuFile1_Click()
MsgBox "คุณเลือกเมนูรายการที่ 1"
End Sub
Private Sub mnuFile2_Click()
MsgBox "คุณเลือกเมนูรายการที่ 2"
End Sub
Private Sub mnuInfo1_Click()
MsgBox "คุณเลือกเมนูรายการที่ 3"
End Subมันต่างกันตรงไหนกับภาค 1 ทุกๆท่านต้องสังเกตดูด้วยล่ะกันครับ ...
ดาวน์โหลด CodeJock Suite Pro for ActiveX/COM ได้ที่นี่ (เฉพาะสมาชิกเท่านั้น)
ดาวน์โหลดโค้ดต้นฉบับเต็ม VB6 ได้ที่นี่ ...
ขอบคุณครับอาจารย์ที่แชร์ความรู้ดีๆ ขอบคุณครับ แก้ไขครั้งสุดท้ายโดย Soutk เมื่อ 2018-11-14 15:09
ผมกำลังหัดทำ ทูลบาร์ ครับ สอบถาม อาจารย์ ครับ จาก code ตัวอย่าง ของอาจารย์ หากเราต้องการ ตัด menu bar (แฟ้มข้อมูล,ข้อมูลโปรแกรม) เพื่อที่จะ เลื่อน Toolbar รายการที่ 1-6 เข้ามาแทน ต้อง แก้ตรงจุดไหนครับ
http://www.g2gnet.com/webboard/images/vb6/codejockedit.png
แสดงว่ายังไม่เข้าใจคอนเซปท์ของ CodeJock ต้องลองทดสอบด้วยตัวเองก่อนด้วยครับ เพราะผมก็ต้องลองผิดลองถูกมาตลอดครับผม ...
การแก้ไขก็แค่ไปใส่ Remark ' เอาไว้หน้าบรรทัดคำสั่งที่เราต้องการตัดออก เพื่อไม่ให้เกิดการการแปลคำสั่ง ... '/ เริ่มต้นการเพิ่มปุ่ม Control เข้ามายัง Tabbed ToolBar
With TabToolBar
'Set Control = .Controls.Add(xtpControlButton, 801, "รายการที่ 1")
'Set Control = .Controls.Add(xtpControlButton, 802, "รายการที่ 2")
Set Control = .Controls.Add(xtpControlButton, 803, "รายการที่ 3")
Set Control = .Controls.Add(xtpControlButton, 804, "รายการที่ 4")
Set Control = .Controls.Add(xtpControlButton, 805, "รายการที่ 5")
Set Control = .Controls.Add(xtpControlButton, 806, "จบโปรแกรม")
Control.Category = "User Tools"
End With
'/ ประกาศตัวแปร Array ตามจำนวน ToolBar ที่ต้องการสร้างขึ้นมา เพื่อแสดงรูป Icon
'/ ในตัวอย่างมี 4 ตัว โดยเริ่มต้นจาก 0 ถึง 3
Dim icoIconArray(0 To 3) As Long
'icoIconArray(0) = ID_USER_TOOLS_1
'icoIconArray(1) = ID_USER_TOOLS_2
icoIconArray(2) = ID_USER_TOOLS_3
icoIconArray(3) = ID_USER_TOOLS_4
icoIconArray(4) = ID_USER_TOOLS_5
icoIconArray(5) = ID_USER_TOOLS_6
อีกจุดหนึ่งในเหตุการณ์ Execute เวลาเกิดการกดปุ่ม ToolBar ...
Private Sub CommandBars1_Execute(ByVal Control As XtremeCommandBars.ICommandBarControl)
On Error Resume Next
Select Case Control.Id
' กลุ่มนี้ คือ TabToolBar ค่าคงที่ถูกกำหนดไว้ในไฟล์ Resource.bas
'Case ID_USER_TOOLS_1:
' MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 1"
'Case ID_USER_TOOLS_2:
' MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 2"
Case ID_USER_TOOLS_3:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 3"
Case ID_USER_TOOLS_4:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 4"
Case ID_USER_TOOLS_5:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 5"
Case ID_USER_TOOLS_6:
MsgBox "คุณเลือกรายการ ToolBar ตัวที่ 5 และกำลังจะจบโปรแกรม"
End
End Select
End Sub
ขอบคุณครับอาจารย์ ต้องขออภํยผมอาจจะ อธิบายไม่ชัดเจนครับ
คือผมอยากจะตัด แฟ้มข้อมูลและ ข้อมูลโปรแกรม ออกไปทั้งแถบเพื่อที่จะเลื่อนแถบรายการที่1-6 ขึ้นไปครับ Soutk ตอบกลับเมื่อ 2018-11-15 09:43
ขอบคุณครับอาจารย์ ต้องขออภํยผมอาจจะ อธิบายไม่ชัดเจน ...
การทำเมนูบาร์ให้ไปดูจากของเดิมที่ผมทำไว้นานแล้วครับ ... http://www.g2gnet.com/news/activenews_view.asp?articleID=200 ขอบคุณครับ
หน้า:
[1]