Code Samples, Tips, Tricks and
other Neat Stuff for the VB developer

All Content Copyright © 2000
New Vision Software
All rights reserved

VB Petition

Many of you may not be aware that Microsoft's mainstream support for VB6 officially ended on March 31st, 2005.  Now that this has occurred, it becomes increasingly more likely that future updates to Windows may break existing VB code.  If you are concerned about this and/or are concerned about migration issues associated with bringing existing VB code to the .NET platform, please take a moment to read and sign the petition asking Microsoft to extend there COM based VB product line.  The impetus behind the petition is the desire to give companies and individuals with substantial investments in existing VB code a path forward to the latest development platform without the current requirement for wholesale rewrites of existing code.  Thanks for your support!

Bryan

   

Home

All Sample Projects

Grouped Samples

Common Dialogs

Control Stuff

Date & Timezone

Drawing

Graphics

Text

File Routines

Form Modifications

Icon Routines

Listbox & ComboBox

Menus

Mouse

Subclassing

Text Parsing Routines

UserControl Demos

Specific Samples

Activate Previous

Instance

API & OLE DragDrop

Environment Variables

GUID - UUID

ListView

Message Box

Collections

Thunking

File Split Utility

Save Clipboard Utility

Visual Basic Tips!


 

Sample Projects

The following are some of my sample projects.  See House Rules for the rules pertaining to the use of the code contained herein.  The samples are all written in VB5 with SP3 installed.

 

App Activate Demo.zip (12KB)  This project shows how to activate a previous instance of an application.  It works even if the previous instance is minimized OR if it has a variable title (like Notepad or Word) OR if the exe being started lives in a different directory than the original instance.

CallWindowProc Demo.zip (4K)  This project shows how you can call VB functions using a memory pointer with the CallWindowProc API function.  Not particularly useful in every day coding but there are situations where this technique could be just the ticket!

Caption Buttons Demo.zip (8KB)  This project implements a class that will draw any of the standard caption buttons on a VB form.  You can put a help or minimize button on a tool window or a do just about anything your heart desires with respect to caption buttons.  The only thing this class will not do is draw the button on a MDI child form when the child form is maximized (Hey! I had to leave something for you to do!).  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Caption Buttons Demo (Multi).zip (11KB)  This project is an extension of the Caption Buttons Demo above.  It handles the drawing of multiple buttons on a single form.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Capture Mouse Demo.zip (3K)  This project shows how you can use the SetCapture API function to know when the mouse leaves the area of a given control.  See the TrackMouseEvent Demo farther down on this page for a more correct but also more complicated method for determining when the mouse leaves the client area of a control.

CD Monitor Demo.zip (7KB)  This project shows how you can subclass the WM_DEVICECHANGE message to receive notifications when a CD in inserted or removed from a drive on the system.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Change BorderStyle Demo.zip (3KB)  The BorderStyle property of a VB form is read only at runtime.  This project shows how to use the SetWindowLong API function to change this property at will at runtime.

ChooseColor Dialog Demo.zip (2KB)  This project shows how to call the ChooseColor API function to display a choose color dialog without using the common dialog control.

CPlApplet Demo.zip (50KB)  This project shows how to enumerate and display all of the Control Panel applets on the system.  This demo also shows how to implement "flat thunks" from 32bit to 16bit code.  See the article on thunking for more details on this subject.

CustomDrawLV Demo.zip (5KB)  This project shows how to display any row, item or subitem in a listview control in a different color than the rest of the rows by subclassing the NM_CUSTOMDRAW message that is sent to the parent of the listview before each item is drawn.  If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Drag Controls Demo.zip (2KB)  This project shows how to easily enable dragging for controls on a VB form at runtime using the SendMessage API function with the WM_NCLBUTTONDOWN and HTCAPTION messages.

DragDrop Files Demo.zip (8KB)  This project shows both the API and OLE methods for handling files dropped on a VB application from Windows Explorer or other source.  The project also shows how to use OLE dragging to allow files to be dragged from your VB application to other drag targets in the system.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

DragList Demo.zip (10KB)  This project shows how to change a standard VB listbox into a drag list style listbox that gives visual feedback when the user drags an item using the mouse.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

DrawState Demo.zip (13KB)  This project shows how to use the DrawState API function to draw icons and text in a disabled state.  The demo also demonstrates how icons can be retrieved from a resource file using the LoadImage API function.

DrawText Path Ellipsis Demo.zip (4KB)  This project shows how to use the DrawText API function to alter the length of a string and insert ellipsis (...) in the section of the string where the text was removed.

EnsureVisible Demo.zip (5KB)  This project shows how to use the API to create a simple method for making sure a given control is visible on the screen.  The demo uses a MDI interface but the routine can be adapted to just about any situation.

Enumerator Fun Demo.zip (5KB)  This project shows how to access the Skip, Reset and Clone methods of an IEnumVARIANT object returned by the NewEnum method of a collection.  This demo also shows what is actually happening under the hood when VB implements a For...Each...Next loop.  You will want to check out the articles in the Collections section for a full explanation of what an IEnumVARIANT object is if you do not already know.

FindFiles Demo.zip (3KB)  This project shows how to use the API to find files as well as how to tell if files exist.  The routines are simple and very fast.

Form Icon Bug Fix Demo.zip (10KB)  This project shows how to work around a bug in VB that causes icons in forms to be rendered incorrectly in some cases.  This bug appears connected to the video settings on effected machines and only occurs when an Icon containing both 16x16 and 32x32 pixel icons is assigned to the form's Icon property.  When the bug manifests itself, it causes a shrunken version of the large icon to be displayed in the titlebar of the form even though a small icon is available.

GetLocaleInfo Demo.zip (6KB)  This project shows how to return some of the commonly needed system formatting characters like the date and currency separators.  It also shows how to create a short date format string that uses a four digit year regardless of the current settings on the system.  All of the format retrieval code is wrapped in an easy to use class module.

GETMINMAXINFO Demo.zip (5KB)  This project shows how to subclass the WM_GETMINMAXINFO message to limit the minimum, maximum and tracking size of a window.

GetTimezoneInfo Demo.zip (4KB)  This project shows how to call the GetTimeZoneInformation API function to return the time difference between the local time on the calling machine and Coordinated Universal Time (UTC).

GUID - UUID Demo.zip (3KB)  This project shows how to create a GUID/UUID and convert it to a string but it does it with a twist.  All of the other routines for creating GUIDs/UUIDs I have ever seen call functions in one of the system OLE dlls.  These functions in turn call the functions I am calling in rpcrt4.dll.  The other functions are probably a little easier to use from VB and therefore may be a better choice for use unless, for some reason speed is a critical factor.  I mostly just threw this together for the fun of it and include it here because it shows how to get the length of an ANSI string and copy the string from a pointer converting it to a VB unicode string along the way.

HotKey Demo.zip (5KB)  This project shows how to use the WM_SETHOTKEY message to set a global hotkey for any form in your VB project.  A global hotkey gives users quick access to a window from anywhere in the system.

Interapplication COM Communication Demo.zip (7KB)  This project shows how to create a simple ActiveX (COM) server that can be instantiated and called from another application on the system.  The demo is quite simple and only shows how to pass text messages back and forth, however far more complex communication is possible using this type of interapplication communication.

LimitEdit Demo.zip (9KB)  This project shows how to subclass the WM_CHAR and WM_PASTE messages for an edit control (textbox) to limit the text that can be entered in the control.  This technique works well for instances where only numeric input is to be allowed.  The LimitEdit object handles several types of numeric entries and can be expanded to handle any type of input limits.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Linked Listboxes Demo.zip (5KB)  This project shows how VB listboxes can be scrolled synchronously as well as positioned to make them appear to be a single list with multiple columns.  It also shows how allow the "columns" to be sized at runtime as well as how to simulate header buttons by drawing them yourself.

Listbox Columns Demo.zip (2KB)  This demo shows how to simulate columns in a standard VB listbox by setting tabstops to the desired positions.

MDI Background Demo.zip (25KB)  This project shows how you can use memory device contexts to create pictures that can be assigned to the picture property of a VB MDI parent form.  The project contains routines to stretch a bitmap across the background of the form and center a bitmap on top of a background as well as a routine for tiling a bitmap onto the background.  It also contains code to draw transparent bitmaps that is used when overlaying a foreground bitmap onto the background.  This project replaces a previous demo that used subclassing to attain a similar result.  The routines in this project are generic enough that they can be used with any VB form or control that has a picture property.

Menu Columns Demo.zip (4KB)  This project shows how to ad vertical column separators to VB menus using the Menu API functions.

MessageBox Demo.zip (6KB)  This project shows some nifty things that are possible when you mix messageboxes and the API.  It shows how to position a messagebox anywhere on the screen you want it to display as well as how to close a messagebox after a specified period of time elapses.  It also shows how the font and button captions can be changed but these last changes are not recommended for production applications.

MSDE Installer Demo.zip (132 KB)  This project shows how you can create a custom installer for installing the Microsoft SQL Server Desktop Engine (MSDE) 2000.  The demo shows how to install and configure MSDE using the sixteen MSI instance files from an MSDE 2K install.  Since this method uses the standard MSI files from the Microsoft MSDE 2K install, all of the OS version checking and other complexities are handled for you.  All you need to worry about is providing an instance name and security info.  The demo also shows a method for creating a wizard interface that allows the user to navigate forward and backward through the install dialogs.  The demo contains an implementation of a custom command button as well as a progress bar control.

No Clipboard Demo.zip (7KB)  This project shows how you can keep the image of a form from being placed on the clipboard by using the clipboard API functions.  I do not necessarily recommend the use of this technique since it doesn't play very nicely with bitmap data on the clipboard.  The demo is just a response to a question I have seen many times on various forums  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Option Button Demo.zip (4KB)  This project shows how you can draw your own option buttons using either the DrawFrameControl API function or the Marlett font.

Parse ASCII URLs Demo.zip (6KB)  This project uses the ReadFile API function to read a file containing ASCII text and parse out any URLs without allowing VB to convert the entire file to unicode.  This technique would be useful for reading very large files since it uses half of the memory that would be necessary if you used the VB file access functions to read the file into a string.

PathFindExtension Demo.zip (4KB)  This project shows how to use both the ANSI and the Wide character versions of the PathFindExtension API function to return the extension for a given file.  It shows some useful techniques for calling the unicode versions of API functions.  Note that IE4 or later must be installed on the system to use this function.

PopUp Button Demo.zip (14KB)  This project show how to create a popup button using the TrackMouseEvent API function.  The button can display both text and graphics.  This demo uses all of the modules from the TrackMouseEvent demo and requires Internet Explorer 3.0 or higher.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

PopUp Listbox Demo.zip (35KB)  This project shows how you can create a popup virtual listbox similar to the one used for VB's intellisense in the IDE.  It is based on Matt Hart's concept of hooking the WM_CREATE message for VB controls to enable changing window styles that can only be set when a control is created.  This technique was published in the July, 1999 issue of the VBPJ.  I have taken the concept a step further and encapsulated it in a user control.  The listbox is also "virtual" in the sense that the items it displays are actually kept in a separate array.  The list only calls for the text of an item when it needs to be drawn.  This is very useful for situations where you have a large number of items that would require a long time to load into a regular listbox.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Proxy For Taskbar Demo.zip (5KB)  This project shows how to create an API window who's icon will be displayed in the taskbar for a VB form that has no titlebar or caption.

Regions (Clickable Shapes) Demo.zip (4KB)  This project shows how to use the region API function to create shapes and lines that can respond to user mouse clicks.

Remove Focused Rect Demo.zip (8KB)  This project shows how to remove the focused rectangle (the dotted line around the edge) from VB command buttons.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Resize Handles Demo.zip (10KB)  This project shows how to create 100% API resizing grab handles for a VB control. This allows users to resize controls on your forms at runtime just like you do in the VB IDE.  This is implemented as a class object that is very simple to use and that handles everything including resizing the control for you!  The 'grab handles' are actually small windows created using the CreateWindowEx API function.  No more messing with control arrays of tiny pictureboxes when doing runtime control resizing!  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Resolving Pointers Demo.zip (3KB)  This project shows how you can resolve a 'dumb' pointer to an object obtained via the hidden VB ObjPtr() function back into a fully referenced object reference on which you can call methods and set properties.  The first time I saw this concept was in Bruce McKinney's book "Hardcore Visual Basic" so Mr. Mckinney deserves any credit for the idea behind the demo.

Return Value From Modal Form Demo.zip (3KB)  This project shows an easy method for returning values from forms that are displayed modally.  It takes advantage of the fact that code execution stops on the line of code where the show command is issued until the modally displayed form is either unloaded or hidden.

Save And Open File Dialog Demo.zip (7KB)  This project shows how to call the GetSaveFileName and GetOpenFileName API functions to display the "Save As..." and "Open" file dialogs without using the common dialog control.  It includes the constants for version 5 of the shell (Win2K).  It also shows how to implement a hook proc that allows setting of the initial view style for the file list.

Scrollbars (Windows) Demo.zip (15KB)  This project implements a class object that will display the standard Windows scrollbars in VB forms, pictureboxes and usercontrols.  VB normally keeps these scrollbars hidden but you can bring them out of VB's closet with a few API calls.  the difficult part is getting and translating the messages sent from the scrollbars to allow you to scroll items.  The class wraps all of the tuff stuff and let's you dump those VB scrollbars with the blinking buttons!  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Set Default Menu Item_Demo.zip (3KB)  This project shows how to set a bolded, default item in a VB menu using the SetMenuItemInfo API function.

Set Environment Demo.zip (8KB)  This project contains two projects.  The first shows how to use the CreateProcess API function to create a custom environment for a shelled application.  This allows you to control the environment variables that are accessible in the child process.  The second project is spawned by the first project.  It shows how to loop through the environment variables in an application and add them to a listbox.

Simple Regions Demo.zip (3KB)  This project shows how the region API functions can be used to manipulate the portions of forms and controls that will be displayed on the screen.

Splitter Bar Demo.zip (8KB)  This project shows how to create both vertical and horizontal splitter bars to allow resizing of windows within your forms.  The demo also shows how you can create an animated splitter like the one used in Books Online.

Subclass Demo (Basic).zip (5KB)  This project explains the concept of subclassing windows messages and includes a basic routine for subclassing in Visual Basic.  While the code sample in this project will work in VB applications, a more robust routine that allows breaking in the IDE is preferred.  This project is targeted at those who are just beginning to explore subclassing to allow them to "get their feet wet" and get a better understanding of the concept before moving on to more complex subclassing schemes.  See Subclass Demo (Advanced).zip for a more robust subclassing routine.

Subclass Demo (Advanced).zip (7KB)  This project implements advanced subclassing techniques that allow for multiple instances of the subclassing object as well as the use of a "debug" dll to make setting breakpoints in the IDE without crashing possible while subclassed.  If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

System Menu Demo.zip (8KB)  This project shows how you can remove items from the system menu of a form to manipulate the buttons available in the form's title bar.  It also shows how to add items to the system menu and how to receive notifications when the added items are selected.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

TrackMouseEvent Demo.zip (7KB)  This project shows how to use the TrackMouseEvent API function which is the "correct" method to use for determining when the mouse leaves or hovers over the client area of a given control or form.  The project implements a class that raises both events (MouseHover & MouseLeave) and is quite easy to use.  Requires Internet Explorer 3.0 or higher.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

TrackPopUpMenu_Demo.zip (3KB)  This project shows how to use some of the menu API functions to create an API popup menu that can be displayed using the TrackPopupMenu API function.  This technique is especially useful if you have a MDI Child form that needs to display a popup menu but you don't want the menus on the MDI Child to override the menus on the MDI Parent.

Virtual Listbox Demo.zip (34KB)  This project shows how you can create an owner drawn virtual listbox that allows you to set the color of each item to any color you want dynamically.  It is based on Matt Hart's concept of hooking the WM_CREATE message for VB controls to enable changing window styles that can only be set when a control is created.  This technique was published in the July, 1999 issue of the VBPJ.  I have taken the concept a step further and encapsulated it in a user control.  The listbox is also "virtual" in the sense that the items it displays are actually kept in a separate array.  The list only calls for the text of an item when it needs to be drawn.  This is very useful for situations where you have a large number of items that would require a long time to load into a regular listbox.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Virtual ListView Demo.zip (19KB)  This project shows how you can create a fully virtual listview control using the listview in the common controls library.  In this context, "virtual" refers to a control that only displays data but doesn't actually store the data that it displays.  For instance, you could have data in an array or recordset and display that data in a virtual list without having to add each item to the control.  This is accomplished via a callback that occurs each time the control needs to draw one of the items visible in the control.  Before each visible item is drawn, the control raises an event to ask for the text that is to be drawn for the given item.  This scheme allows you to streamline your code in several ways by allowing you to eliminate duplication of data.  So, rather than having a recordset AND a listview that contain the same data, you can just keep the data in the recordset and save the overhead of having to load the duplicate data into the listview.  A virtual list is also blindingly fast at displaying large quantities of items.  In fact, this demo displays over ONE MILLION items faster than the blink of an eye!  Although displaying large amounts of data usually reflects very bad design principals, there are times where there is no way around it.  A virtual list handles these situations nicely.  The demo is based on Matt Hart's concept of hooking the WM_CREATE message for VB controls to enable changing window styles that can only be set when a control is created.  This technique was published in the July, 1999 issue of the VBPJ.  I have taken the concept a step further and encapsulated it in a user control.  This demo uses subclassing techniques so, If you don't already have it, you will want to get the Debug Object (Dbgwproc.dll) DLL to allow stepping through subclassed code in break mode in the VB IDE.

Watch Directory Demo.zip (11KB)  This project shows how the FindFirstChangeNotification and FindNextChangeNotification API functions can be used to watch directories and directory trees for changes.  This demo implements an API timer to check a series of change notification handles without the serious degradation in performance associated with checking wait handles in a Do...Loop routine.

What's This Help Demo.zip (5KB)  This project shows how simple it is to add "What's This Help" to a VB project.  For those unfamiliar with what "What's This Help" is....  It is the context sensitive help provided when the "?" icon is clicked in the title bar of a form that has the WhatsThisButton property set to True.

This page is still under construction and I keep adding my sample projects as time allows.  If you don't find what you are looking for, drop me a line and if I have a demo for it, I'll up load it PRONTO! 

federation-glossy
federation-glossy
federation-glossy
federation-glossy