Author Topic: VB6 OCX GUI  (Read 3819 times)

0 Members and 1 Guest are viewing this topic.

John Spikowski

  • Posts: 34
VB6 OCX GUI
« on: July 04, 2021, 05:48:35 PM »
This is an example of the OnLine Dictionary done with ScriptBasic and using a VB6 generated OCX for the UI. I also added resizing to this example.

OLD.sb
Code: Script BASIC
  1. IMPORT COM.sbi
  2.  
  3. servers[0]="dict.org"
  4. servers[1]="dict1.us.dict.org"
  5. servers[2]="all.dict.org"
  6.  
  7. FUNCTION btnFetch_Clicked
  8.   LOCAL dat, total, count
  9.   server_selection = COM::CBN(obj, "CurrentServer")
  10.   OPEN server_selection & ":2628" FOR SOCKET AS #1
  11.   PRINT#1,"SHOW DB\n"
  12.   LINE INPUT#1, dat
  13.   LINE INPUT#1, dat
  14.   count = 0
  15.   WHILE LEFT(dat, 1) <> "."
  16.     LINE INPUT#1, dat
  17.     IF LEFT(dat, 1) <> "." THEN total[count] = TRIM(dat)
  18.     count+=1
  19.   WEND
  20.   PRINT#1,"QUIT\n"
  21.   CLOSE(#1)
  22.   FOR cnt = 0 TO count - 2
  23.     COM::CBN obj, "AddDictionaries", :CALL, total[cnt]
  24.   NEXT
  25.   COM::CBN obj, "DefaultDictionary"
  26.   btnFetch_Clicked = TRUE
  27.   EXIT FUNCTION
  28.  
  29.  
  30. FUNCTION btnSearch_clicked
  31.   LOCAL dict, dat, total, info
  32.   whichDictionary = COM::CBN(obj, "CurrentDictionary")
  33.   searchword = COM::CBN(obj, "SearchWord", :GET)
  34.   dict = LEFT(whichDictionary, INSTR(whichDictionary, " "))
  35.   OPEN COM::CBN(obj, "CurrentServer") & ":2628" FOR SOCKET AS 1
  36.   IF COM::CBN(obj, "AllDict", :GET) THEN
  37.     PRINT#1,"DEFINE * " & searchword & "\n"
  38.   ELSE
  39.     PRINT#1,"DEFINE " & dict & " " & searchword & "\n"
  40.   END IF
  41.   REPEAT
  42.     LINE INPUT#1, dat
  43.     IF LEFT(dat, 3) = "151" THEN
  44.       total$ &= "------------------------------\r\n"
  45.       total$ &= RIGHT(dat, LEN(dat) - LEN(searchword) - LEN(dict))
  46.       total$ &= "------------------------------\r\n"
  47.       REPEAT
  48.         LINE INPUT#1, info
  49.         info = REPLACE(info, CHR(34), CHR(92) & CHR(34))
  50.         IF LEFT(info, 1) <> "." THEN total &= TRIM(info) & "\r\n"
  51.       UNTIL LEFT(info, 1) = "."
  52.       total &= "\r\n"
  53.     END IF
  54.   UNTIL LEFT(dat, 3) = "250" OR VAL(LEFT(dat, 3)) > 499
  55.   PRINT#1,"QUIT\n"
  56.   CLOSE(#1)
  57.   IF LEFT(dat, 3) = "552" THEN
  58.     total = "No match found."
  59.   ELSE IF LEFT(dat, 3) = "501" THEN
  60.     total = "Select a dictionary first!"
  61.   ELSE IF LEFT(dat, 3) = "550" THEN
  62.     total = "Invalid database!"
  63.   END IF
  64.   COM::CBN(obj, "SetTranslation", :CALL, total)
  65.   btnSearch_Clicked = TRUE
  66. EXIT FUNCTION
  67.  
  68. ' MAIN
  69.  
  70. obj = COM::CREATE(:SET, "OLD.OLDict")
  71. oCollection = COM::CBN(obj, "CallBackHandlers", :GET)
  72. COM::CBN oCollection, "Add", :CALL, ADDRESS(btnFetch_Clicked()), "win.btnFetch_Click"
  73. COM::CBN oCollection, "Add", :CALL, ADDRESS(btnSearch_Clicked()), "win.btnSearch_Click"
  74. FOR idx = 0 TO UBOUND(servers)
  75.   COM::CBN obj, "AddServer", :CALL, servers[idx]
  76. NEXT  
  77. COM::CBN obj, "DefaultServer"
  78. COM::CBN obj, "ShowOLD"
  79. COM::RELEASE obj
  80.  

VB6 Form
Code: Visual Basic
  1. Private Declare Function ext_SBCallBack Lib "COM.dll" Alias "SBCallBack" (ByVal EntryPoint As Long, ByVal arg As Long) As Long
  2. Private Declare Function ext_SBCallBackEx Lib "COM.dll" Alias "SBCallBackEx" (ByVal EntryPoint As Long, ByRef v As Variant) As Variant
  3.  
  4. Private m_owner As OLDict
  5. Private Type ControlPositionType
  6.     Left As Single
  7.     Top As Single
  8.     Width As Single
  9.     Height As Single
  10.     FontSize As Single
  11. End Type
  12.  
  13. Private m_ControlPositions() As ControlPositionType
  14. Private m_FormWid As Single
  15. Private m_FormHgt As Single
  16.  
  17.  
  18. Function ShowMain(owner As OLDict) As Long
  19.     On Error Resume Next
  20.     Set m_owner = owner
  21.     Me.Show 1
  22.     Set m_owner = Nothing
  23.     ShowMain = 0
  24.     Unload Me
  25. End Function
  26.  
  27. Private Function TriggerCallBack(nodeID As Long, argValue As Long) As Long
  28.     TriggerCallBack = ext_SBCallBack(nodeID, argValue)
  29. End Function
  30.  
  31. Private Function TriggerCallBackEx(nodeID As Long, v() As Variant)
  32.     TriggerCallBackEx = ext_SBCallBackEx(nodeID, v)
  33. End Function
  34.  
  35. Public Sub btnClear_Click()
  36.     win.serverList.Clear
  37.     win.dictTB.Text = ""
  38.     win.entry.Text = ""
  39.     win.btnFetch.SetFocus
  40.     End Sub
  41.  
  42. Private Sub btnExit_Click()
  43.     Set m_owner = Nothing
  44.     btnExit = 0
  45.     Unload Me
  46. End Sub
  47.  
  48. Private Sub btnFetch_Click()
  49.     Dim nodeID As Long
  50.     Dim arg As Long
  51.     Dim rtnVal As Long
  52.    
  53.     win.serverList.Clear
  54.     nodeID = m_owner.CallBackHandlers("win.btnFetch_Click")
  55.     arg = False
  56.     rtnVal = TriggerCallBack(nodeID, arg)
  57. End Sub
  58.  
  59. Private Sub btnSearch_Click()
  60.     Dim nodeID As Long
  61.     Dim arg As Long
  62.     Dim rtnVal As Long
  63.    
  64.     nodeID = m_owner.CallBackHandlers("win.btnSearch_Click")
  65.     arg = False
  66.     rtnVal = TriggerCallBack(nodeID, arg)
  67. End Sub
  68.  
  69. Private Sub btnAbout_Click()
  70.     MsgBox "Script BASIC VB6" & vbCrLf & "On Line Dictionary", vbInformation, "About"
  71. End Sub
  72.  
  73.  
  74. ' Save the form's and controls' dimensions.
  75. Private Sub SaveSizes()
  76. Dim i As Integer
  77. Dim ctl As Control
  78.  
  79.     ' Save the controls' positions and sizes.
  80.    ReDim m_ControlPositions(1 To Controls.Count)
  81.     i = 1
  82.     For Each ctl In Controls
  83.         With m_ControlPositions(i)
  84.             If TypeOf ctl Is Line Then
  85.                 .Left = ctl.X1
  86.                 .Top = ctl.Y1
  87.                 .Width = ctl.X2 - ctl.X1
  88.                 .Height = ctl.Y2 - ctl.Y1
  89.             Else
  90.                 .Left = ctl.Left
  91.                 .Top = ctl.Top
  92.                 .Width = ctl.Width
  93.                 .Height = ctl.Height
  94.                 On Error Resume Next
  95.                 .FontSize = ctl.Font.Size
  96.                 On Error GoTo 0
  97.             End If
  98.         End With
  99.         i = i + 1
  100.     Next ctl
  101.  
  102.     ' Save the form's size.
  103.    m_FormWid = ScaleWidth
  104.     m_FormHgt = ScaleHeight
  105. End Sub
  106.  
  107. Private Sub Form_Load()
  108.     SaveSizes
  109. End Sub
  110.  
  111. Private Sub Form_Resize()
  112.     ResizeControls
  113. End Sub
  114.  
  115. ' Arrange the controls for the new size.
  116. Private Sub ResizeControls()
  117. Dim i As Integer
  118. Dim ctl As Control
  119. Dim x_scale As Single
  120. Dim y_scale As Single
  121.  
  122.     ' Don't bother if we are minimized.
  123.    If WindowState = vbMinimized Then Exit Sub
  124.  
  125.     ' Get the form's current scale factors.
  126.    x_scale = ScaleWidth / m_FormWid
  127.     y_scale = ScaleHeight / m_FormHgt
  128.  
  129.     ' Position the controls.
  130.    i = 1
  131.     For Each ctl In Controls
  132.         With m_ControlPositions(i)
  133.             If TypeOf ctl Is Line Then
  134.                 ctl.X1 = x_scale * .Left
  135.                 ctl.Y1 = y_scale * .Top
  136.                 ctl.X2 = ctl.X1 + x_scale * .Width
  137.                 ctl.Y2 = ctl.Y1 + y_scale * .Height
  138.             Else
  139.                 ctl.Left = x_scale * .Left
  140.                 ctl.Top = y_scale * .Top
  141.                 ctl.Width = x_scale * .Width
  142.                 If Not (TypeOf ctl Is ComboBox) Then
  143.                     ' Cannot change height of ComboBoxes.
  144.                    ctl.Height = y_scale * .Height
  145.                 End If
  146.                 On Error Resume Next
  147.                 ctl.Font.Size = y_scale * .FontSize
  148.                 On Error GoTo 0
  149.             End If
  150.         End With
  151.         i = i + 1
  152.     Next ctl
  153. End Sub
  154.  

VB6 Class
Code: Visual Basic
  1. Public CallBackHandlers As New Collection
  2.  
  3. Public Function ShowOLD() As Long
  4.     ShowOLD = win.ShowMain(Me)
  5. End Function
  6.  
  7. Public Sub AddServer(server_url As String)
  8.     win.serverCombo.AddItem server_url
  9. End Sub
  10.  
  11. Public Sub DefaultServer()
  12.     win.serverCombo.ListIndex = 0
  13. End Sub
  14.  
  15. Public Function CurrentServer() As String
  16.     CurrentServer = win.serverCombo.List(win.serverCombo.ListIndex)
  17. End Function
  18.  
  19. Public Sub AddDictionaries(dictionary As String)
  20.     win.serverList.AddItem dictionary
  21. End Sub
  22.  
  23. Public Function CurrentDictionary() As String
  24.     CurrentDictionary = win.serverList.List(win.serverList.ListIndex)
  25. End Function
  26.  
  27. Public Sub DefaultDictionary()
  28.     win.serverList.ListIndex = 0
  29. End Sub
  30.  
  31. Public Sub SetTranslation(translation_text As String)
  32.     win.dictTB.Text = translation_text
  33. End Sub
  34.  
  35. Public Property Get SearchWord() As String
  36.     win.dictTB.SetFocus
  37.     SearchWord = win.entry.Text
  38. End Property
  39.  
  40. Public Property Get AllDict() As Long
  41.     AllDict = win.chkAll.Value
  42. End Property
  43.  
« Last Edit: July 04, 2021, 06:01:50 PM by John Spikowski »