home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_BAS / VBWRAP.ZIP / VBMAGIC.ASC < prev    next >
Text File  |  1994-02-04  |  26KB  |  517 lines

  1. Useful tools to enhance product quality and aid fast development   08/24/93     Page 12
  2.                                     12
  3. PRODUCT Dans Visual Basic Tools
  4. Description Power Tools for Visual Basic(R) programmers
  5. File Name VBMAGIC.BAS
  6. Object Type Visual Basic
  7. Remarks Windows API wrap-arounds provide enhanced control features
  8. About Useful tools to enhance product quality and aid fast development
  9. Overview
  10. VBMAGIC.BAS is a collection of useful tools for visual basic programmers.
  11. The functions and subroutines provided in this module allow the basic
  12. programmer to develop applications with features normally beyond Visual
  13. Basic.  These routines are packaged as application independent modules and
  14. are easily reused in most projects.
  15. This document describes the application programmer interface to the
  16. functionality found in VBMAGIC.BAS.  Since there is now a commercial
  17. product named VB Magic, please do not confuse this file with that product.
  18. The two are very much unrelated.  VBMAGIC.BAS is periodically updated by
  19. the author, and is always available to any interested parties.  VBMAGIC.BAS
  20. is intended to function with versions of Visual Basic  2.0 and above.
  21. Support for version 1 of visual basic is not available any longer.
  22. CONTENTS
  23.      Overview                                            1
  24.      Subroutines/function                                3
  25.           ADDLISTITEM add an item to a listbox/combobox with its
  26.           associated data item                           3
  27.           ADDSLISTITEM add an item to a SORTED listbox/combobox with
  28.           its associated data item                       3
  29.           BOXTRACK routine to create a tracked listbox/textbox
  30.           relationship                                   4
  31.           CASCADECHILDREN performs Windows version independent cascade
  32.           operation on child windows                     4
  33.           CENTER routine to center the specified form on the screen
  34.           4
  35.           CRYPT encrypts and decrypts string data        5
  36.           EM_GETLINETEXT fundtion that extracts a specific line from
  37.           an edit box control.                           5
  38.           EM_GETNUMLINES function to extract the logical line count
  39.           from an edit control                           5
  40.           FINDSTRING function to position a list-box control to the
  41.           next line containing a string                  6
  42.           FINDSTRINGEXACT function to position a list-box control to
  43.           the next line matching a string                6
  44.           GETPROFILEINT function returns integer value from current
  45.           application INI file                           7
  46.           GETPROFILESTRING function to extract character values from
  47.           application INI file....                       7
  48.           GETSYSTEMDIRECTORY function returns full path to logical
  49.           WINDOWS/SYSTEM directory                       8
  50.           GETVERSION function returns windows version number as an
  51.           integer                                        8
  52.           GETWINDOWSDIRECTORY function returns full path to logical
  53.           WINDOWS directory                              8
  54.           NOTELAUNCH routine to run/activate a copy of notepad for
  55.           edit/view of a specific .TXT                   8
  56.           RELATEMETO routine to link a form to another form as
  57.           parent/child.                                  9
  58.           SEARCHWINDOW function to identify a window whose title
  59.           contains specific text                         9
  60.           SELECTSTRING function performs a positioning list box search
  61.           for an exact match on a search string          9
  62.           SETAPPNAME routine to establish application INI working file
  63.           10
  64.           SETEMREADONLY function to set an edit control to read-only
  65.           mode                                           10
  66.           SETLBTABS routine to set tab stops in a list box control
  67.           10
  68.           SET_3D routine to set-up three-dimensional border line width
  69.           and color                                      11
  70.           THREE_DEE routine to draw a three dimensional shadow box
  71.           around a control                               11
  72.           TILECHILDREN routine to rearrange children windows in tile
  73.           mode on parent window                          11
  74.           UNDROP routine to remove the drop-down list from a combo-box
  75.           (hide list)                                    12
  76.           WNDCAPTION function to return a window caption given a
  77.           window handle                                  12
  78.           WRITEPROFILESTRING function to store a value in the current
  79.           application INI file                           12
  80.  
  81. Subroutines/function
  82.  
  83. ADDLISTITEM add an item to a listbox/combobox with its associated data item
  84. Syntax:
  85.      i% = addlistitem( ctl as control, text$, index%, dataval&)
  86. where:
  87.      ctl is a reference to the listbox control being modified
  88.      text is the string to be inserted into the control
  89.      index is the position (zero based) to perform the insert
  90.      dataval is a long integer to be put into the itemdata property
  91. Remarks
  92.      This function is a legacy from the VB 1.0 days when a listbox control
  93.      did not support direct access to its itemdata value array.  It is kept
  94.      in this module so that applications written to VB 1.0 and then
  95.      converted will continue to function.  The functionality provided is
  96.      now available via VB listbox properties, although more than one line
  97.      of code is required.
  98. Return Value
  99.      This function returns the index value supplied
  100.  
  101. ADDSLISTITEM add an item to a SORTED listbox/combobox with its associated
  102. data item
  103. Syntax:
  104.      i% = addlistitem( ctl as control, text$, dataval&)
  105. where:
  106.      ctl is a reference to the listbox control being modified
  107.      text is the string to be inserted into the control
  108.      dataval is a long integer to be put into the itemdata property
  109. Remarks
  110.      This function is a legacy from the VB 1.0 days when a listbox control
  111.      did not support direct access to its itemdata value array.  It is kept
  112.      in this module so that applications written to VB 1.0 and then
  113.      converted will continue to function.  The functionality provided is
  114.      now available via VB listbox properties, although more than one line
  115.      of code is required.
  116. Return Value
  117.      This function returns the zero based index of the position where the
  118. new item has been inserted.
  119.  
  120. BOXTRACK routine to create a tracked listbox/textbox relationship
  121. Syntax:
  122.      boxtrack textboxref, listboxref
  123. where:
  124.      textboxref is a reference (name of) to a textbox control, or any
  125.      control with a TEXT property.
  126.      listboxref is a reference to a listbox (not a combobox or dropdown
  127.      list)
  128. USE:
  129.      To link a list box control to an edit control, add the code below to
  130.      the KEYPRESS event for the edit box control.  This causes each
  131.      keypress to refresh the list-box.
  132.      Example:
  133.        SUB MYEDITCONTROL_KEYPRESS (keyascii as integer)
  134.          boxtrack myeditcontrol, mylistboxcontrol
  135.        END SUB
  136. Remarks:
  137.      Tracked listboxes are seen in the standard Windows(tm) 3.1 help system
  138.      under the search feature.  The text box above the list box allows a
  139.      user to type topic names to be located in the list.  As the user types
  140.      each character, the listbox is repositioned to the nearest item.  This
  141.      function allows a programmer to provide tracked list boxes in any
  142.      application.
  143.  
  144. CASCADECHILDREN performs Windows version independent cascade operation on
  145. child windows
  146. Syntax:
  147.      cascadechildren parent_hwnd%, style%
  148. where:
  149.      Parent_Hwnd is the window handle of the specified "parent window"
  150.      Style:  Meaningful only in Windows 3.1 and up.  Refer to SDK function
  151.      reference for values.
  152.  
  153. CENTER routine to center the specified form on the screen
  154. Syntax:
  155.      center hwnd%
  156. where:
  157.      hwnd  Window handle of form to be centered
  158.      EXAMPLE:
  159.        center Me   ' centers the current form
  160.        center thee ' centers a form whose name property is set to THEE
  161.  
  162. CRYPT encrypts and decrypts string data
  163. Syntax:
  164.      s$ = crypt(action$, key$, src$)
  165. where:
  166.      s:    A string to hold the encrypted/decrypted value
  167.      key: String holding action verb (Encrypt or Decrypt):  Permissible
  168.      values: E, D
  169.      src: String holding data to be encrypted or decrypted
  170. This function performs key based XOR encryption on string data.  The
  171. algorithm used is reversible when the same key is used to encrypt and
  172. decrypt the string.  It is suggested that the key string be fairly random
  173. and not match the string being encrypted.  Longer keys yield more secure
  174. results.  The function will cycle through a key whose length is less than
  175. the length of the string being acted upon.  The result of the operation is
  176. returned as the function value.
  177.  
  178. EM_GETLINETEXT fundtion that extracts a specific line from an edit box
  179. control.
  180. Syntax:
  181.      s$ = em_getlinetext( editcontrol, lineno)
  182. where:
  183.      s:    A string to hold the return value
  184.      editcontrol: the name of an edit control (name property value)
  185.      lineno:   zero based line position to extract
  186. This useful function extracts a given visual line from a multi-line edit
  187. control.  This function will work with any control that supports EM_GETLINE
  188. messages.  The line number is zero based, meaning that the first line in
  189. the control work area is referenced with a zero value, the second line as
  190. one, etc.  Useful for extracting text as the user sees it.  This differs
  191. from extracting the text property value, which ignores line breaks.
  192.  
  193. EM_GETNUMLINES function to extract the logical line count from an edit
  194. control
  195. Syntax:
  196.      i% = em_getnumlines( editcontrol)
  197. where:
  198.      i:    An integer to hold the return value
  199.      editcontrol: the name of an edit control (name property value)
  200. This function returns the number of logical lines visible in the work area
  201. of a multi-line edit control.  The number returned will be one for a single
  202. line, two for two visible lines, etc.
  203.  
  204. FINDSTRING function to position a list-box control to the next line
  205. containing a string
  206. Syntax:
  207.      i% = findstring( listctl, start%, srch$)
  208. where:
  209.      i:    An integer to hold the return value (line number where match
  210.      occured)
  211.      listctl:  reference (name property) to a list control
  212.      start:    logical list line number to start searching at (zero based)
  213.      srch:     string variable holding the text to be located
  214. This function searches forward in a list box from the starting position
  215. specified.  Each line is examined much in the same manner as the basic
  216. command INSTR,  in a case-less comparison for a partial string match.  The
  217. first line found containing the search string (srch) will be identified in
  218. the return value.  The list is not altered in any way during the search.
  219. The programmer is responsible for taking the appropriate action.  The
  220. return value is a zero based list position.  Not found is indicated by a
  221. return value less than zero.
  222.  
  223. FINDSTRINGEXACT function to position a list-box control to the next line
  224. matching a string
  225. Syntax:
  226.      i% = findstringexact( listctl, start%, srch$)
  227. where:
  228.      i:    An integer to hold the return value (line number where match
  229.      occured)
  230.      listctl:  reference (name property) to a list control
  231.      start:    logical list line number to start searching at (zero based)
  232.      srch:     string variable holding the text to be located (exact match)
  233. This function searches forward in a list box from the starting position
  234. specified.  Each line is examined for an exact match(whole line),  in a
  235. case-less comparison.  The first line found containing the search string
  236. (srch) will be identified in the return value.  The list is not altered in
  237. any way during the search.  The programmer is responsible for taking the
  238. appropriate action.  The return value is a zero based list position.  Not
  239. found is indicated by a return value less than zero.
  240.  
  241. GETPROFILEINT function returns integer value from current application INI
  242. file
  243. Syntax:
  244.      i% = getprofileint( sect$, entry$, default%))
  245. where:
  246.      i:    An integer to hold the return value
  247.      sect:     section identifier found in INI file .  e.g. [SAMPLE]
  248.      entry:    section entry identifier.  e.g.         ENTRY= 4
  249.      default:  value to be returned if sample/entry pair not found
  250. This wrapper function has the SAME NAME as a standard windows API function,
  251. but is intended to over-ride the use of the standard call so that the
  252. global WIN.INI is left uncluttered.  This function works in conjunction
  253. with the SETAPPNAME function described under its own heading later on.  The
  254. SETAPPNAME function allows the programmer to specify the name of an INI
  255. file to be used throughout the program until the next call to SETAPPNAME.
  256. All calls to the family of INI Set/Get functions found in this function
  257. collection require that the set-up function SETAPPNAME be called first.
  258.  
  259. GETPROFILESTRING function to extract character values from application INI
  260. file
  261. Syntax:
  262.      i% = getprofilestring( sect$, entry$, default$, buffer%, size%)
  263. where:
  264.      i:    An integer to hold the return value (new length of buffer
  265.      argument)
  266.      sect:     section identifier found in INI file .  e.g.      [SAMPLE]
  267.      entry:    section entry identifier.  e.g.         ENTRY= XYZ123
  268.      default:  text value to be returned if sample/entry pair not found
  269.      buffer:   name of string variable to be populated with return value
  270.      size:     maximum number of characters to be returned.
  271. This wrapper function has the SAME NAME as a standard windows API function,
  272. but is intended to over-ride the use of the standard call so that the
  273. global WIN.INI is left uncluttered.  This function works in conjunction
  274. with the SETAPPNAME function described under its own heading later on.  The
  275. SETAPPNAME function allows the programmer to specify the name of an INI
  276. file to be used throughout the program until the next call to SETAPPNAME.
  277. All calls to the family of INI Set/Get functions found in this function
  278. collection require that the set-up function SETAPPNAME be called first.
  279. Unlike the standard Windows API with the same name, no extra care need be
  280. taken with the string return value.  The wrapper function shields the
  281. programmer from the consequences of non-allocated buffer storage.  The
  282. maximum suggested length for INI entries (Microsoft as source) is 256
  283. characters.
  284.  
  285. GETSYSTEMDIRECTORY function returns full path to logical WINDOWS/SYSTEM
  286. directory
  287. Syntax:
  288.      s$ = getsystemdirectory()
  289. where:
  290.      s:    A string to hold the return value
  291. The windows system directory is a logical path name where, upon start-up,
  292. windows found its main dynamic link library components.  On a stand-alone
  293. installation of windows, the default system directory path is
  294. C:\WINDOWS\SYSTEM.  On networks, this varies, and is PATH environment
  295. variable dependent.  To perform installation type activities, it is useful
  296. to have a function that locates the current system directory.
  297.  
  298. GETVERSION function returns windows version number as an integer
  299. Syntax:
  300.      i% = getversion()
  301. where:
  302.      i:    An integer to hold the return value
  303. This function returns the windows version number as: major * 100 + minor.
  304. Knowledge of windows minor version numbers help here.  Windows version 3.1
  305. will be returned as 310, where version 3.00 will be identified as 300 by
  306. this function.
  307.  
  308. GETWINDOWSDIRECTORY function returns full path to logical WINDOWS directory
  309. Syntax:
  310.      s$ = getwindowsdirectory()
  311. where:
  312.      s:    A string to hold the return value
  313. The windows directory is a logical path name where, upon start-up, windows
  314. found its start-up components (win.com).  On a stand-alone installation of
  315. windows, the default windows directory path is C:\WINDOWS.  On networks,
  316. this varies, and is PATH environment variable dependent.  To perform
  317. installation type activities, it is useful to have a function that locates
  318. the current windows directory.  This is also the default path where windows
  319. looks for INI files.
  320.  
  321. NOTELAUNCH routine to run/activate a copy of notepad for edit/view of a
  322. specific .TXT
  323. Syntax:
  324.      notelaunch fileroot
  325. where:
  326.      fileroot:  A string holding the name of a .TXT file.
  327. This subroutine launches or locates and activates a copy of the notepad
  328. utility that shipped with Windows, loading a copy of the file specified.
  329. After determining the file name, which is forced by the subroutine to end
  330. with .TXT, the windows task list is searched for an existing copy of
  331. notepad that is running with the same file loaded.  If none is found, a new
  332. copy of notepad is started; otherwise the existing copy is brought to the
  333. foreground.
  334. Since notepad is a seperate application, there is no further linkage to the
  335. program via this routine.
  336.  
  337. RELATEMETO routine to link a form to another form as parent/child.
  338. Syntax:
  339.      relatemeto childform, parentform
  340. where:
  341.      childform:      form name property of form to be child in the new
  342.      relationship
  343.      parentform:    form name property of form to be parent in the new
  344.      relationship
  345. This subroutine allows the programmer to alter the Visual Basic form
  346. behavior so that visual basic programs behave more like standard windows
  347. programs.  Specifically, when the parent-child relationship is established,
  348. window related events and commands can be utilized.   Some of these are
  349. cascading, tiling, minimizing.  To see the effect of this function, relate
  350. form2 to form1, display both forms, and then minimize form1.  The icon for
  351. form2 is removed if it was minimized, and the only icon remaining is that
  352. of the parent form.  Typically this relationship is established in the
  353. FORM_LOAD event.  A side effect of this function is that child forms need
  354. to know the name of the parent form.  This conflicts with good object
  355. oriented user interface design where reusable application form components
  356. should not know the calling form.
  357.  
  358. SEARCHWINDOW function to identify a window whose title contains specific
  359. text
  360. Syntax:
  361.      i% = searchwindow(stringtofind)
  362. where:
  363.      i:    An integer to hold the return window handle value
  364.      stringtofind:  A string holding the text to locate
  365. This function performs a system wide search of all visible windows,
  366. selecting the title text from each, and performing a case-insensitive
  367. search of the title text for a match on the string to find value.  The
  368. window handle value (HWND) of the first window whose title contains the
  369. search text is returned.  If no window is found that matches the search,
  370. then zero is returned.
  371.  
  372. SELECTSTRING function performs a positioning list box search for an exact
  373. match on a search string
  374. Syntax:
  375.      i% = selectstring( listctl, start%, match$)
  376. where:
  377.      i:    An integer to hold the return value
  378.      listctl:  a reference to a LISTBOX or COMBOBOX control
  379.      start:    list control line position to begin searching
  380.      match:    string variable containing text to locate
  381. Selectstring searches for an exact match in a list control, and if found,
  382. repositions the listbox so that the item is visible and selected.  Any
  383. previous selections are de-selected.
  384.  
  385. SETAPPNAME routine to establish application INI working file
  386. Syntax:
  387.      setappname INI_NAME
  388. where:
  389.      INI_NAME:  A string holding the name of the application INI file
  390. This routine sets the global application INI context for the duration of
  391. the application run (or until setappname is run again).  Calls to the
  392. profile related functions (see getprofilexxx, writeprofilexxx) will
  393. reference the INI (initialization constant file) named in the call to this
  394. routine.
  395. Using individual INI files for seperate logical applications keeps the
  396. global windows initialization file (WIN.INI) free of application specific
  397. information.  This in turn makes it easy to install copies of applications
  398. that may have conflicts in INI section names, as each logical application
  399. can see a different view.
  400. As a matter of style, it is considered poor form for an application to
  401. require an INI file that it does not create.  Suggested practice is for INI
  402. values to be built as an application runs.  INI entries are very useful for
  403. storing last view, options settings, data source information (encrypted is
  404. best), and other application specific data.  If a suite of applications
  405. needs access to common initialization information, they can easily share an
  406. INI file via use of SETAPPNAME.
  407.  
  408. SETEMREADONLY function to set an edit control to read-only mode
  409. Syntax:
  410.      i% = setemreadonly(ectl, bool%)
  411. where:
  412.      i:    An integer to hold the return value
  413.      ectl:     reference (name property value) to an edit control
  414.      bool:     boolean flag integer value (TRUE/FALSE) determine edit
  415.      control result status
  416. This function is used to set the read-only status in an edit box.  This
  417. differs from altering the enabled property in that the control text is left
  418. as is, and the control is still able to obtain input focus.  While an edit
  419. control is in read-only mode, the user may scroll any text, and perform
  420. clipboard read operations (cut/copy).  The user is unable to alter the
  421. contents of a read-only edit control.  The boolean flag value determines
  422. whether the control will be left in read-only mode (true) or normal state
  423. (false).
  424.  
  425. SETLBTABS routine to set tab stops in a list box control
  426. Syntax:
  427.      setlbtabs lbox, tablist%()
  428. where:
  429.      lbox:      a list control reference (name property value)
  430.      tablist:  an array (passed by reference) of integers
  431. Using this routine, a programmer can alter the tab stop position values in
  432. a list box that was created with the LB_HAS_TABSTOPS style.  The standard
  433. visual basic listbox (not combo-box) supports tab stops.  The array of
  434. integers holds the tab stop positions, calculated in dialog box units.  A
  435. dialog box unit is equivalent of approximately one quarter of the average
  436. width of the system font.  This awkward unit of measure is a byproduct of
  437. having proportional and sizeable fonts available.
  438. It is important to note that the first array position will determine the
  439. position of the second column.
  440.  
  441. SET_3D routine to set-up three-dimensional border line width and color
  442. Syntax:
  443.      set_3d  white&, black&, lines
  444.      set_3d 0, 0, 3
  445. where:
  446.      white:     A long integer holding the windows RGB color for the
  447.      "white" (non shadow) part of a three dimensional border.
  448.      black:    A long integer holding the windows RGB color for the "black"
  449.      (shadow) part of a three dimensional border.
  450.      lines:    the number of one pixel width lines that make up the
  451.      thickness of the border.
  452. Windows RGB colors are three byte integers that contain a red, green, and
  453. blue component.  The special values shown in the second syntax above (zero)
  454. indicates that the system defined colors for these shadow components should
  455. be determined and then used.  System defined colors are set using the
  456. control panel application, color section, button highlight, button shadow
  457. values.
  458. This routine controls the results of using the THREE_DEE routine described
  459. below
  460.  
  461. THREE_DEE routine to draw a three dimensional shadow box around a control
  462. Syntax:
  463.      three_dee ctlname, mode%
  464. where:
  465.      ctlname:  a control reference (any control on any form)
  466.      mode:          an integer (1, 0) indicating raised look (1), or sunken
  467.      look (0)
  468. Using this routine it is easy to create three dimensional controls, custom
  469. buttons, and artsy borders.  It is suggested that the AUTOREDRAW property
  470. of a form using three_dee'd controls be set to true.  Be careful to note
  471. that paint events no longer are generated if  a forms AUTOREDRAW property
  472. is true.  This should not impact design or function in any way, since PAINT
  473. is easily avoided.
  474.  
  475. TILECHILDREN routine to rearrange children windows in tile mode on parent
  476. window
  477. Syntax:
  478.      tilechildren hwnd%, style%
  479. where:
  480.      hwnd:      A window handle of the parent window
  481.      style:    (not used below windows version 3.1)  Indicates vertical or
  482.      horizontal tiling
  483. Tiled windows take the child windows and arrange them to cover the parent
  484. as if they were ceramic tiles, laid out edge to edge.  The style flag
  485. (boolean) impacts the arrangement.  Windows performs the operations
  486. performed here, and the values of style can be determined in the windows
  487. SDK documentation.
  488.  
  489. UNDROP routine to remove the drop-down list from a combo-box (hide list)
  490. Syntax:
  491.      undrop combobox
  492. where:
  493.      combobox:  combo box reference (name property value)
  494. The only known use of this routine is to remove the list from view while
  495. processing change events in the combo box.
  496.  
  497. WNDCAPTION function to return a window caption given a window handle
  498. Syntax:
  499.      s$ = windcaption(hwnd%)
  500. where:
  501.      s:    A string to hold the return value
  502.      hwnd:     a window handle to locate and extract the caption from.
  503.  
  504. WRITEPROFILESTRING function to store a value in the current application INI
  505. file
  506. Syntax:
  507.      i% = writeprofilestring( sect$, entry$, value$)
  508. where:
  509.      i:    An integer to hold the return value
  510.      sect:     section name in INI file.     e.g. [SAMPLE]
  511.      entry:    item name within section.     e.g. ITEM=somevalue
  512.      value:    the value to place on the right side of the equal sign
  513. This function stores the values passed in the current application INI file
  514. (see SETAPPNAME).  These items may be later extracted with GETPROFILESTRING
  515. and GETPROFILEINT.  If an entry does not yet exist when this function is
  516. called, the new entry/section will be created automatically.
  517.