home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / code / design / 3ddemo / ctl3d.bas < prev    next >
BASIC Source File  |  1995-02-26  |  17KB  |  460 lines

  1. Option Explicit
  2.  
  3. 'Compiled by:  M. John Rodriguez,  CIS ID: 100321,620
  4. '                             Internet ID: jrodrigu@cpd.hqusareur.army.mil
  5. '                                        : 100321.620@compuserve.com
  6. '
  7. 'Please feel free to distribute this for your use and experiments.  Please ensure
  8. 'that you give credit to the folks who unknowingly helped to do this.
  9. '
  10. '
  11. 'This procedures contained in this module are the culmination of work supplied by various
  12. 'individuals.  It would not be proper for me not to include their names.  To make it easier
  13. 'to tell who authored what, their names are commented in the appropriate procedures.
  14. '
  15. '
  16. 'CTL3D API calls... If you don't have CTL3DV2.DLL, you can delete the V2 and it should
  17. 'still work properly.
  18. Declare Function Ctl3dAutoSubclass Lib "Ctl3DV2.DLL" (ByVal hInst As Integer) As Integer
  19. Declare Function Ctl3dRegister Lib "Ctl3DV2.DLL" (ByVal hInst As Integer) As Integer
  20. Declare Function Ctl3dUnregister Lib "Ctl3DV2.DLL" (ByVal hInst As Integer) As Integer
  21. Declare Function Ctl3dSubclassDlgEx Lib "Ctl3DV2.DLL" (ByVal hWnd As Integer, ByVal dFlags As Long) As Integer
  22. Declare Function Ctl3dSubclassCtlEx Lib "Ctl3DV2.DLL" (ByVal hWnd As Integer, ByVal cFlags As Integer) As Integer
  23. Declare Function Ctl3dUnsubclassCtl Lib "Ctl3DV2.DLL" (ByVal hWnd As Integer) As Integer
  24. Declare Function Ctl3DGetVer Lib "Ctl3DV2.DLL" () As Integer
  25.  
  26.  
  27. 'Other API Calls for the Forms...
  28. Declare Function GetModuleHandle Lib "Kernel" (ByVal ModuleName As String) As Integer
  29. Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Long
  30. Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer
  31. Declare Function SetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Long) As Long
  32.  
  33. Global Const FIXED_DOUBLE = 3
  34. Global Const DS_MODALFRAME = &H80&
  35. Global Const GWL_STYLE = (-16)
  36. Global Const GWW_HINSTANCE = (-6)
  37. Global Const CTL3D_ALL = &HFFFF
  38.  
  39. 'Menu API's for adjusting the 3D Dialog box system menu...
  40. Declare Function GetSystemMenu% Lib "User" (ByVal hWnd%, ByVal bRevert%)
  41. Declare Function RemoveMenu% Lib "User" (ByVal hMenu%, ByVal nPosition%, ByVal wFlags%)
  42. Global Const MF_BYPOSITION = &H400
  43.  
  44. 'Some colors for us to use...
  45. Global Const COLOR_BLACK = &H0&
  46. Global Const COLOR_LIGHT_GRAY = &HC0C0C0
  47. Global Const COLOR_DARK_GRAY = &H808080
  48. Global Const COLOR_WHITE = &HFFFFFF
  49.  
  50. '/* Ctl3d Control ID */
  51. Global Const CTL3D_BUTTON_CTL = 0
  52. Global Const CTL3D_LISTBOX_CTL = 1
  53. Global Const CTL3D_EDIT_CTL = 2
  54. Global Const CTL3D_COMBO_CTL = 3
  55. Global Const CTL3D_STATIC_CTL = 4
  56.  
  57. 'Global Variables to allow for SubClassing Ctrls in our form...
  58. Global gSubClassCtls As Integer
  59. Global gCTL3DMajorVersion As Integer
  60. Global gCTL3DMinorVersion As Integer
  61.  
  62.  
  63.  
  64. 'This procedure does a couple of things.
  65. 'First, it will attempt to register your application to the CTL3D Program.
  66. 'Second, it will attempt to tell you if the system can register 3D Controls
  67. 'Only CTL3D Version 2.63 or greater can be used to make VB controls appear 3D
  68. '
  69. '
  70. Function App3DRegister () As Integer
  71.  
  72. Dim appInst%, suc%
  73.  
  74. 'Really just needed if we can get the CTL3D or CTL3DV2 dll's
  75. On Error GoTo AppRegError
  76.  
  77. 'Do version checking.  This will also let us know if we can't get the dll's
  78. suc% = Ctl3DGetVer()
  79.  
  80. 'If we get a version number then pass check it for control subclass capability
  81. If suc% > 0 Then
  82.     gCTL3DMajorVersion = (suc% And 65100) \ (2 ^ 8)
  83.     gCTL3DMinorVersion = suc% And 255
  84.     If (gCTL3DMajorVersion > 1) And (gCTL3DMinorVersion > 12) Then gSubClassCtls = True
  85. End If
  86.  
  87. 'Get the application instance...
  88. appInst% = GetModuleHandle(App.EXEName)
  89. 'Now register the application
  90. suc% = Ctl3dRegister(appInst%)
  91. 'Did it register?
  92. If suc% = 0 Then Exit Function
  93.  
  94. 'Now subclass all of the dialog and message boxes for 3D, should work with VB
  95. suc% = Ctl3dAutoSubclass(appInst%)
  96. 'We had not problems so tell the app we registered with CTL3D
  97. App3DRegister = True
  98.  
  99. 'In case an error occurred
  100. AppRegError:
  101.  
  102. End Function
  103.  
  104. 'Before you exit your application, give this procedure a call..
  105. 'In this case, I have a procedure called ExitProgram() that allows
  106. 'me to do all of my cleanup functions.  This procedure is in there.
  107. '
  108. Sub App3DUnregister ()
  109.  
  110. 'Call this just before your application exits..
  111.  
  112. Dim appInst%, suc%
  113.  
  114. 'Get the application instance again..
  115. appInst% = GetModuleHandle(App.EXEName)
  116.  
  117. 'Now unregister us...
  118. suc% = Ctl3dUnregister(appInst%)
  119.  
  120. End Sub
  121.  
  122. '
  123. ' ControlIn3D paints a 3D-border around the control given in ctrlTarget.
  124. ' nBevel controls the the deepness, nSpace the distance between the control
  125. ' and the 3D-border and bInset sets the border to be drawn inset or outset.
  126. '
  127. ' Parts of this code are taken from the VB Tips & Tricks help file.
  128. ' Original code written by Matej Nastran.
  129. '
  130. '
  131. Sub ComboBoxIn3D (ctrlCombo As Control, nBevel As Integer)
  132.     
  133.     Dim PixelX As Integer, PixelY As Integer
  134.     Dim CTop As Integer, CRight As Integer, CBottom As Integer
  135.  
  136.     ' Just put "No 3D" in the Tag property and your ComboBox keeps 2D
  137.     If InStr(UCase(ctrlCombo.Tag), "NO 3D") = 0 Then
  138.     
  139.     ControlIn3D ctrlCombo, nBevel, 0, True
  140.     
  141.     If ctrlCombo.Style = 0 Then             'Remove white space only
  142.         PixelX = Screen.TwipsPerPixelX      'if it is a Dropdown ComboBox
  143.         PixelY = Screen.TwipsPerPixelY
  144.         CTop = ctrlCombo.Top
  145.         CRight = ctrlCombo.Left + ctrlCombo.Width
  146.         CBottom = ctrlCombo.Top + ctrlCombo.Height
  147.         ctrlCombo.Parent.Line (CRight - PixelX * 24, CTop)-(CRight - PixelX * 18, CBottom - PixelY), COLOR_LIGHT_GRAY, BF
  148.     End If
  149.     End If
  150.  
  151. End Sub
  152.  
  153. '
  154. '
  155. ' ControlIn3D paints a 3D-border around the control given in ctrlTarget.
  156. ' nBevel controls the the deepness, nSpace the distance between the control
  157. ' and the 3D-border and bInset sets the border to be drawn inset or outset.
  158. '
  159. ' Parts of this code are taken from the VB Tips & Tricks help file.
  160. ' Original code written by Matej Nastran.
  161. '
  162. Sub ControlIn3D (ctrlTarget As Control, nBevel As Integer, nSpace As Integer, bInset As Integer)
  163.     Dim CTop As Integer, CLeft As Integer, CRight As Integer, CBottom As Integer
  164.     Dim PixelX As Integer, PixelY As Integer, AddX As Integer, AddY As Integer
  165.     Dim i As Integer
  166.  
  167.     ' Just put "No 3D" in the Tag property and your control keeps 2D
  168.     If InStr(UCase(ctrlTarget.Tag), "NO 3D") = 0 Then
  169.     PixelX = Screen.TwipsPerPixelX
  170.     PixelY = Screen.TwipsPerPixelY
  171.     CTop = ctrlTarget.Top - PixelY
  172.     CLeft = ctrlTarget.Left - PixelX
  173.     CRight = ctrlTarget.Left + ctrlTarget.Width
  174.     CBottom = ctrlTarget.Top + ctrlTarget.Height
  175.     If bInset Then          ' Draw border inset
  176.         For i = nSpace To (nBevel + nSpace - 1)
  177.         AddX = i * PixelX: AddY = i * PixelY
  178.         ctrlTarget.Parent.Line (CLeft - AddX, CTop - AddY)-(CRight + AddX, CTop - AddY), COLOR_DARK_GRAY
  179.         ctrlTarget.Parent.Line (CLeft - AddX, CTop - AddY)-(CLeft - AddX, CBottom + AddY), COLOR_DARK_GRAY
  180.         ctrlTarget.Parent.Line (CLeft - AddX, CBottom + AddY)-(CRight + AddX + PixelX, CBottom + AddY), COLOR_WHITE
  181.         ctrlTarget.Parent.Line (CRight + AddX, CTop - AddY)-(CRight + AddX, CBottom + AddY), COLOR_WHITE
  182.         Next i
  183.     Else                    ' Draw border outset
  184.         For i = nSpace To (nBevel + nSpace - 1)
  185.         AddX = i * PixelX: AddY = i * PixelY
  186.         ctrlTarget.Parent.Line (CRight + AddX, CBottom + AddY)-(CRight + AddX, CTop - AddY), COLOR_DARK_GRAY
  187.         ctrlTarget.Parent.Line (CRight + AddX, CBottom + AddY)-(CLeft - AddX, CBottom + AddY), COLOR_DARK_GRAY
  188.         ctrlTarget.Parent.Line (CRight + AddX, CTop - AddY)-(CLeft - AddX - PixelX, CTop - AddY), COLOR_WHITE
  189.         ctrlTarget.Parent.Line (CLeft - AddX, CBottom + AddY)-(CLeft - AddX, CTop - AddY), COLOR_WHITE
  190.         Next i
  191.     End If
  192.     End If
  193.  
  194. End Sub
  195.  
  196. 'This procedure modifies the menu for the dialog box.
  197. 'In order for this to work correctly, the form must have the MinButton and MaxButton set
  198. 'to false if you leave the ControlBox property set to true.  Otherwise, Restore, Maximize, and
  199. 'Minimize will stay on...
  200. '
  201. 'This snippet of code was taken by a submission from
  202. 'RANDRIAMBOLOLONA Roland H. - Compuserve ID - 100331,2516
  203. '
  204. 'He says he got some of it from the MARCH '95 VBPJ  Code Listing - TIPS.TXT
  205. '
  206. 'The author did not say if he did this, I am passing the accolades - with a few
  207. 'modifications for readability
  208. '
  209. Sub DlgSysMenu (fm As Form)
  210.  
  211. Dim hSysMenu%, suc%
  212.  
  213. ' Obtain the handle to the forms System menu
  214. hSysMenu% = GetSystemMenu(fm.hWnd, False)
  215.  
  216. ' Remove all but the MOVE and CLOSE options.  The menu items
  217. ' must be removed starting with the last menu item.
  218. '
  219. suc% = RemoveMenu(hSysMenu, 8, MF_BYPOSITION) 'Switch to
  220. suc% = RemoveMenu(hSysMenu, 7, MF_BYPOSITION) 'Separator
  221. suc% = RemoveMenu(hSysMenu, 5, MF_BYPOSITION) 'Separator
  222.  
  223.  
  224. End Sub
  225.  
  226. '
  227. ' FormIn3D paints a 3D-border around controls on the given Form frmTarget.
  228. ' nBevel controls the the deepness of the 3D-border.
  229. '
  230. ' Controls that are affected:
  231. '       TextBox         ListBox         ComboBox
  232. '       DriveListBox    DirListBox      FileListBox
  233. '       Line
  234. '       ... (list can be easly expanded)
  235. '
  236. ' Just put "No 3D" in the Tag property of a specific control or the form
  237. ' itself and it is not painted in 3D.
  238. '
  239. ' Call this function from your forms Paint-event.
  240. '
  241. ' Parts of this code are taken from the VB Tips & Tricks help file.
  242. ' Original code written by Matej Nastran.
  243. '
  244. ' Modifications from original source:  bBlaster was removed because it wasn't
  245. ' necessary for this file.
  246. '
  247. Sub FormIn3D (frmTarget As Form, nBevel As Integer)
  248.     Dim DrawWidthOld As Integer, ScaleModeOld As Integer
  249.     Dim i As Integer, Ret As Integer
  250.     Dim ctrlTarget As Control
  251.     Static bBusy As Integer
  252.     
  253.  
  254.     If bBusy Then Exit Sub          'Got some DoEvents. Just in case...
  255.     bBusy = True
  256.  
  257.     DrawWidthOld = frmTarget.DrawWidth
  258.     frmTarget.DrawWidth = 1
  259.     ScaleModeOld = frmTarget.ScaleMode
  260.     frmTarget.ScaleMode = 1     'Twips
  261.  
  262.     DoEvents
  263.     
  264.     'Loop controls
  265.     For i = 0 To (frmTarget.Controls.Count - 1)
  266.     Set ctrlTarget = frmTarget.Controls(i)
  267.     If TypeOf ctrlTarget Is TextBox Then ControlIn3D ctrlTarget, nBevel, 0, True
  268.     If TypeOf ctrlTarget Is ListBox Then ControlIn3D ctrlTarget, nBevel, 0, True
  269.     If TypeOf ctrlTarget Is ComboBox Then   'ComboBoxes are special
  270.     ComboBoxIn3D ctrlTarget, nBevel
  271.     End If
  272.     If TypeOf ctrlTarget Is DriveListBox Then ControlIn3D ctrlTarget, nBevel, 0, True
  273.     If TypeOf ctrlTarget Is DirListBox Then ControlIn3D ctrlTarget, nBevel, 0, True
  274.     If TypeOf ctrlTarget Is FileListBox Then ControlIn3D ctrlTarget, nBevel, 0, True
  275.     If TypeOf ctrlTarget Is Line Then       'Lines are also special
  276.     LineIn3D ctrlTarget
  277.     End If
  278.     If TypeOf ctrlTarget Is Label Then ControlIn3D ctrlTarget, nBevel, 0, True
  279.     Next i
  280.     
  281.     frmTarget.DrawWidth = DrawWidthOld      'Always restore what you change
  282.     frmTarget.ScaleMode = ScaleModeOld
  283.  
  284.     bBusy = False
  285.  
  286. End Sub
  287.  
  288. 'This procedure makes my dialog box appear 3D.
  289. '
  290. 'This snippet of code was taken by a submission from
  291. 'RANDRIAMBOLOLONA Roland H. - Compuserve ID - 100331,2516
  292. '
  293. 'He says he got some of it from the MARCH '95 VBPJ  Code Listing - TIPS.TXT
  294. '
  295. 'This procedure was not commented, I am just telling you where I got the source
  296. 'for this because it works very well...
  297. '
  298. 'This procedure modifies the menu for the dialog box.
  299. ' This procedure makes the VB form appear as a dialog box for CTL3D to read
  300. ' and paint it...
  301. ''
  302. 'This snippet of code was taken by a submission from
  303. 'RANDRIAMBOLOLONA Roland H. - Compuserve ID - 100331,2516
  304. '
  305. 'He says he got some of it from the MARCH '95 VBPJ  Code Listing - TIPS.TXT
  306. '
  307. 'The author did not say if he did this, I am passing the accolades - with a few
  308. 'modifications for readability
  309. '
  310. Sub FormToDialog (frm As Form)
  311.  
  312.     Dim hWnd As Integer
  313.     Dim iResult As Integer
  314.     Dim lStyle As Long
  315.  
  316.     hWnd = frm.hWnd
  317.     If frm.BorderStyle = FIXED_DOUBLE Then
  318.     frm.BackColor = COLOR_LIGHT_GRAY
  319.     lStyle = GetWindowLong(hWnd, GWL_STYLE)
  320.     lStyle = lStyle Or DS_MODALFRAME
  321.     lStyle = SetWindowLong(hWnd, GWL_STYLE, lStyle)
  322.     iResult = Ctl3dSubclassDlgEx(hWnd, &H0)
  323.     End If
  324.  
  325. End Sub
  326.  
  327. '
  328. ' LineIn3D paints the given Line-control ctrlLine in 3D.
  329. ' frmTarget is the Form containing that Line.
  330. '
  331. ' Parts of this code are taken from the VB Tips & Tricks help file.
  332. ' Original code written by Matej Nastran.
  333. '
  334. Sub LineIn3D (ctrlLine As Control)
  335.  
  336.     If InStr(UCase(ctrlLine.Tag), "NO 3D") = 0 Then
  337.     ctrlLine.BorderColor = COLOR_DARK_GRAY
  338.     'Check if line is vertical or horizontal
  339.     If Abs(ctrlLine.X2 - ctrlLine.X1) > Abs(ctrlLine.Y2 - ctrlLine.Y1) Then
  340.     ctrlLine.Parent.Line (ctrlLine.X1, ctrlLine.Y1 + Screen.TwipsPerPixelY)-(ctrlLine.X2, ctrlLine.Y2 + Screen.TwipsPerPixelY), COLOR_WHITE
  341.     Else
  342.     ctrlLine.Parent.Line (ctrlLine.X1 + Screen.TwipsPerPixelX, ctrlLine.Y1)-(ctrlLine.X2 + Screen.TwipsPerPixelX, ctrlLine.Y2), COLOR_WHITE
  343.     End If
  344.     End If
  345.  
  346. End Sub
  347.  
  348. 'Make3DDlg
  349. 'Call this procedure in a forms Form_Load event to register the form
  350. 'as a 3D Dialog.  This procedure calls the appropriate subprocedures
  351. 'in making the Dialog 3D
  352. '
  353. Sub Make3DDlg (dlgfrm As Form)
  354.  
  355. 'Set the dlg forms attributes for CTL3D to paint it..
  356. FormToDialog dlgfrm
  357.  
  358. 'Now make the system menu for the form to show only Move and Close.
  359. 'NOTE:  You must set the MinButton and MaxButton properties to False.
  360. 'The ControlBox property being set to False will have no effect on
  361. 'this procedure.
  362. DlgSysMenu dlgfrm
  363.  
  364. 'Turn all of the controls 3D. If you have the wrong version, MakeDlgCtrls will not
  365. 'register the controls because the API call isn't there.
  366. 'If the CTL3D is too old, then place the following code in each dialogs Form_Paint event
  367. '   FormIn3D Me, 1
  368. '
  369. If gSubClassCtls Then MakeDlgCtrls3D dlgfrm
  370.  
  371. End Sub
  372.  
  373. ' This procedure cycles through the controls in the form and then
  374. ' attempts to subclass them for 3D effects.  Because the controls in VB
  375. ' are all class Thunder, CTL3D can't see them as they are so we force
  376. ' it to say "Hey, Paint me 3D!"
  377. '
  378. ' You can add other controls to this list as long as they match the
  379. ' specification on them.
  380. '
  381. '
  382. '
  383. '
  384. Sub MakeDlgCtrls3D (dlgfrm As Form)
  385.  
  386. Dim i As Integer
  387. Dim ctrl As Control
  388.  
  389. If Not gSubClassCtls Then Exit Sub
  390.  
  391. For i = 0 To (dlgfrm.Controls.Count - 1)
  392.     Set ctrl = dlgfrm.Controls(i)
  393.     If TypeOf ctrl Is TextBox Then Reg3DCtrl ctrl, CTL3D_EDIT_CTL
  394.     If TypeOf ctrl Is ListBox Then Reg3DCtrl ctrl, CTL3D_LISTBOX_CTL
  395.     If TypeOf ctrl Is ComboBox Then Reg3DCtrl ctrl, CTL3D_COMBO_CTL
  396.     If TypeOf ctrl Is DriveListBox Then Reg3DCtrl ctrl, CTL3D_COMBO_CTL
  397.     If TypeOf ctrl Is DirListBox Then Reg3DCtrl ctrl, CTL3D_LISTBOX_CTL
  398.     If TypeOf ctrl Is FileListBox Then Reg3DCtrl ctrl, CTL3D_LISTBOX_CTL
  399.     If TypeOf ctrl Is CheckBox Then Reg3DCtrl ctrl, CTL3D_BUTTON_CTL
  400.     If TypeOf ctrl Is OptionButton Then Reg3DCtrl ctrl, CTL3D_BUTTON_CTL
  401. Next i
  402.  
  403. dlgfrm.Refresh
  404.  
  405. End Sub
  406.  
  407. '  Used to register a control for 3D by CTL3D.  Does not have to be
  408. '  a dialog form to have it painted 3D
  409. '
  410. '
  411. Sub Reg3DCtrl (ctrl As Control, ctrltype As Integer)
  412.  
  413. Dim suc%
  414.  
  415. suc% = Ctl3dSubclassCtlEx(ctrl.hWnd, ctrltype)
  416.  
  417. End Sub
  418.  
  419. '  This unregisters controls from the dialog or whatever form.  Use this
  420. '  as a cleanup method so as not to corrupt CTL3D or waste resources.
  421. '  Called from the Form_Unload procedure
  422. '
  423. Sub Undo3DCtrls (frm As Form)
  424.  
  425. Dim i As Integer
  426. Dim ctrl As Control
  427.  
  428.  
  429. If Not gSubClassCtls Then Exit Sub
  430.  
  431. For i = 0 To (frm.Controls.Count - 1)
  432.     Set ctrl = frm.Controls(i)
  433.     If TypeOf ctrl Is TextBox Then UnReg3DCtrl ctrl
  434.     If TypeOf ctrl Is ListBox Then UnReg3DCtrl ctrl
  435.     If TypeOf ctrl Is ComboBox Then UnReg3DCtrl ctrl
  436.     If TypeOf ctrl Is DriveListBox Then UnReg3DCtrl ctrl
  437.     If TypeOf ctrl Is DirListBox Then UnReg3DCtrl ctrl
  438.     If TypeOf ctrl Is FileListBox Then UnReg3DCtrl ctrl
  439.     If TypeOf ctrl Is CheckBox Then UnReg3DCtrl ctrl
  440.     If TypeOf ctrl Is OptionButton Then UnReg3DCtrl ctrl
  441. Next i
  442.  
  443.  
  444. End Sub
  445.  
  446. '  Call this procedure to unregister your controls
  447. '  If you call the Reg3DCtrl procedure, call this one
  448. '  in the Form_Unload event for the form.
  449. '
  450. '
  451. Sub UnReg3DCtrl (ctrl As Control)
  452.  
  453. Dim suc%
  454.  
  455. suc% = Ctl3dUnsubclassCtl(ctrl.hWnd)
  456.  
  457.  
  458. End Sub
  459.  
  460.