Tag Archives: api

Advanced Textbox Manipulation in Visual Basic and VB.NET | Part 2

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.33 out of 5)
 

This is Part Two of the Advanced Textbox Control Manipulation series. If you haven’t already you should check out Part 1 of this series. That article showed how to use the SendMessage API call to make a Textbox control Page Left, Page Right, Line Left, Line Right, Left Edge, and go to the Right Edge of the contents. This Part 2 post will show how to add the functionality that is outlined below…

Page UP

Page Down

Line UP

Line Down

Top Edge

Bottom Edge

Note: These codes are basically taken from an example I for VB.NET that shows lots and lots of various textbox based manipulating and functionality. Just go to http://www.vbcodesource.com under the Visual Basic.NET – Examples page.

Continue reading

Advanced Textbox Manipulation in Visual Basic and VB.NET | Part 1 of 2

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (20 votes, average: 3.95 out of 5)
 

This is the first post in a two part series on how to do add some extra textbox features not exposed in your normal VB or VB.NET textbox. For instance, Page Left/Right and Line Up/Down.

Note: These codes are basically taken from an example I made at my vbcodesource.com site for VB.NET that shows how to do lots and lots of various textbox based manipulating. Just go to http://www.vbcodesource.com/ under the Visual Basic.NET – Examples page.

So in Part #1 I will provide code to add six new abilities to a Textbox control…

  • Page Left
  • Page Right
  • Line Left
  • Line Right
  • Left Edge
  • Right Edge

To accomplish these features I will use the tried and true Windows API. Specifically the SendMessage Function.


First is the API function

Visual Basic 6.0


    'Used to Send Messages to the control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Long, _
        ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Visual Basic.NET, VB 2008 and Visual Basic 2010

    'Used to Send Messages to the control.
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal winHandle As Int32, _
        ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

Now for some Constants…

Visual Basic 6.0


    Private Const EM_SCROLL = &HB5

    Private Const SB_PAGEDOWN = 3

    Private Const SB_PAGEUP = 2

    Private Const SB_LINEUP = 0

    Private Const SB_LINEDOWN = 1

    Private Const SB_BOTTOM = 7

    Private Const SB_TOP = 6

Visual Basic.NET through VB 2008, VB 2010, and newer


    Private Const EM_SCROLL As Int32 = &HB5

    Private Const SB_PAGEDOWN As Int32 = 3

    Private Const SB_PAGEUP As Int32 = 2

    Private Const SB_LINEUP As Int32 = 0

    Private Const SB_LINEDOWN As Int32 = 1

    Private Const SB_BOTTOM As Int32 = 7

    Private Const SB_TOP As Int32 = 6


All that’s really needed is the Textbox you want to manipulate and call Sendmessage with the right combination of constants to perform the intended function.

NOTE: If your using VB 6.0 then change one thing in the rest of the code below…

  • Instead of txtControl.Handle.ToInt32, make it txtControl.hWnd

That’s basically the only change needed.


Page Left / Page Right

        'Move the position pageleft.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_PAGELEFT, 0

        'Move the position pageright.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_PAGERIGHT, 0

Line Left / Line Right

        'Move the position left.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_LINELEFT, 0

        'Move the position right.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_LINERIGHT, 0

Left Edge / Right Edge

        'Move the position to the left edge.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_LEFT, 0

        'Move the position to the right edge.
        SendMessage txtControl.Handle.ToInt32, WM_HSCROLL, SB_RIGHT, 0

As you can see these non-native features were simple to add and configure.

Part #2 will go over:

  1. Page Up
  2. Page Down
  3. Line Up
  4. Line Down
  5. Top Edge
  6. Bottom Edge

Well that’s basically it for this article, Have fun!

              Jason

Revised: 2014

How-to Playback Movies and Videos with VB and Visual Basic .NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (25 votes, average: 4.12 out of 5)
 

As you will see, playing videos is easy using the Windows MCI Command interface for the playback which is supported in Windows XP, Windows Vista, Windows 7, Windows 8, and Windows 10. Specifically the MCISendString function will be used in this tutorial.

MCI should by default already support the common formats like avi, mpg, mpeg, wmv, etc. with a basic Windows installation. But you can actually get MCI to playback nearly all formats if the correct Codec/Driver is installed. For Example, you can playback Apple QuickTime .Mov, MPEG 4 DivX, or H.264 .MP4 by simply installing the codec for each format. After that just program the device like you normally would.

This article is only to show how to do basic playback of videos and not features like duration/length, position, or resizing the video. You can check out my MCI MultiMedia Tutorial to learn how to program MCI and add your own features.

If your using VB 6.0 select the correct MCISendString declare below, change any IntPtr.Zero code to 0, and use ‘.hWnd’ instead of ‘.Handle’.

Edit: 2014, December 17th: I decided to make a download available that uses all of the codes in this article with a couple extras to help resizing and such. I also changed some of the code in the program for Option Explicit.

Download Example: Click Here

View Screenshot: Click Here


Visual Basic 6.0 and Older

    'The Main API call that will be used for the playback.
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _
        lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
            Long, ByVal hwndCallback As Long) As Long

 

Visual Basic.NET and Newer

    'The Main API call that will be used for the playback.
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal _
        lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
            Integer, ByVal hwndCallback As IntPtr) As Integer

 

OR You Can Use…

    'The main API call.
    <DllImport("winmm.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function mciSendString(commandString As String, returnString As String, _
        returnStringLength As Integer, _mciCallback As IntPtr) As Int32
    End Function

Now make a couple variables.

    'Will hold the path to the movie file.
    Dim filename As String

    'Holds the return value of mciSendstring. Not used for anything in this article though.
    Dim retVal As Integer

 

Then set a path to the file you want to play. If you’ve seen my other MCI articles you should remember about adding “Quotes” around the path and filename, or at least supply the file’s short pathname.

        'The path to the movie or video to play.
        filename = "c:\movies\MyMovieFile.mp4"

        'Now add the quotes around the path.
        filename = Chr(34) & filename & Chr(34)

Next thing to do is open and setup a playback device. I usually use the MPEGVideo device since it works with audio and video. The MPEGVideo device is DirectShow down under.

There are a couple ways you can open the video. You can have it display in its own popup window or have it displayed in a control like a Panel or Frame. Basically any control with a .hWnd or .Handle property.

This command will display open a new device and have the video display in a control window.

'This code below will open a new mpegvideo device and play the movie in the “movieWindow” control which is
'nothing more than a GroupBox/Frame control I used in an example app. Basically any control with a
'handle can be used. For VB 6 you would use the controls .hWnd property instead of the
'Handle.ToInt32 property.
'If you want MCI to select which device to use just take out "type mpegvideo" from the code.

retVal = mciSendString("open " & filename & " type mpegvideo alias myMovie parent " _
& movieWindow.Handle.ToInt32 & " style child", vbNullString, 0, IntPtr.Zero)

 

If you want the video to open in a separate window use this code instead.

'Specify the mpegvideo driver to play the movies which should play most movie formats without any problems.
'This code will have the video open in its Own window and the alias name will be “myMovie”.
If you want MCI to select which device to use just take out "type mpegvideo" from the code.

retVal = mciSendString("open " & filename & " type mpegvideo alias myMovie", vbNullString, 0, IntPtr.Zero)

OK, now that you’ve opened a device, all you have to do is call the Play command to begin playback. The codes below will Play, Stop, Pause, and Resume.

Play

 '
 'Start Playing the movie once you’ve setup the device with your file.
 retVal = mciSendString("play myMovie", vbNullString, 0, IntPtr.Zero)

 

Stop


'Will Stop the playback if its currently playing.
 retVal = mciSendString("stop myMovie", vbNullString, 0, IntPtr.Zero)

 

Pause


'Will Pause the playback if its playing.
 retVal = mciSendString("pause myMovie", vbNullString, 0, IntPtr.Zero)

 

Resume


'Will Resume the playback if it has been paused.
retVal = mciSendString("resume myMovie", vbNullString, 0, IntPtr.Zero)

 

You want to use this code when your done with your device and/or closing your program so the mci resources will be cleaned up. You could change ‘close myMovie’ and put ‘close all’ to free everything MCI has used.

        'Will make sure the previous alias is destroyed. If the alias “myMovie” hasn’t been created yet,
        'this code will NOT cause any errors or anything. So there is no need to worry about that.
        retVal = mciSendString("close myMmovie", vbNullString, 0, IntPtr.Zero)

Well, that’s all there is to it. Hope you found something useful from this article. Have Fun!

Jason

Revised: 2015

How to Set an Inputbox to Show Password Protected Characters using VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 3.80 out of 5)
 

I made a previous article on password protecting an application and also using Inputboxes to get the username and password. A comment was left on how to make the Inputbox show password characters instead of the actual password being inserted. Well, that is what this article is for, to show how to use .NET and set the Inputbox (Modal Window) to display the input in its textbox as password based characters like asterisks or another char instead of the actual password. Its also called masking the characters in the inputbox. This article will mask the inputbox using Asterisks. But you can set whatever you want the characters to be.


NOTE: While the code in this article is for Visual Basic.NET, VB 2008, and VB.NET 2010, the same principals apply to classic VB as well. You want to Subclass, checking for WM_Create messages. When a WM_Create message is activated, you want to use the Findwindow API to determine whether the newly created window matches the title of the Inputbox you want to show. If it is use GetWindow to get the handle to the Child window which is the edit/text box. Then use the SendMessage API to tell the first ChildWindow in the Inputbox to display all input characters as password characters like asterisks.

Update note 02/27/2011: Steve mentioned that the API call FindWindow may not format correctly when you use Auto in declaring it. So you may have to change Auto to “Ansi” instead. For example: Private Declare Ansi Function FindWindow…..

Instead of Auto or Unicode. The alias name in the api call ends with an ‘A’ which denotes Ansi. If it ended with ‘W’ then it would be Unicode based.

If you run into that problem, use this Declare instead of the one later in the article.


    Private Declare Ansi Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32


AS outlined in a note above, the way I created a password only inputbox was to Subclass and monitor all the WM_Create messages being sent. When the application is notified that a new window was created, it will use the FindWindow API to check for the title of the newly created window. If the title of the window matches the title of the inputbox to be, it will return the handle to that window and call the GetWindow API function to get a handle to the first Child window, which with the Inputbox modal, is the Inputs Edit Box (Textbox) that you type the characters in. Once a handle to the Edit Box is taken, then a call to the SendMessage function to send a EM_SETPASSWORDCHAR message which will make the textbox display password characters which in this article tells the API to use Asterisks characters as the displayed password. You can make it display pretty much any character you want it to. That’s all I did!


    'If you used the Declare Ansi Function FindWindow version above, then ignore this one.
    'Will be used to get the window handles.
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32

Now for some more api calls and variables.

    'Using it to get a handle to the child window of the inputbox.
    Private Declare Function GetWindow Lib "user32"? (ByVal hwnd As Int32, ByVal wCmd As Int32) As Int32

    'It tells the editbox (textbox) to display the contents/input as password characters.
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

    'This will be the title of the Inputbox you will be throwing.
    'This is important since this is the key factor that the example looks for to identify the Inputbox.
    Dim inputTitle As String = "Enter the Password!"

    'Will hold the password returned by the inputbox message.
    Dim returnedPW As String

    'Will be used to code the password characters to appear as Asterisk characters.
    'You can actually have the password text display pretty much any character if you want too.
    'Just change the value of the const to the keycode character you want to use.
    Const asteriskKeyCode As Int32 = 42

    'Used by the SendMessage API to tell the inputbox editbox (textbox) to display the input as password characters.
    Public Const EM_SETPASSWORDCHAR As Int32 = &HCC

    'The message for when a new window is created.
    Public Const WM_CREATE As Int32 = &H1

    'Will hold the return value when calling the FindWindow function which will be the handle to the window.
    Dim windowHandle As Int32

    'Used to have the API call to look for a child based window.
    Public Const GW_CHILD As Int32 = 5

Now that the APIs and Variables are setup, its time for the rest of the codes.

This is the code I put in a button control.

    'This is a Button control I put some of the codes in…
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        '
        'Simply display the inputbox with the title specified in the inputTitle variable and get the value that was put in the edit box.
        '
        'IMPORTANT: The Inputbox is required to have the same title as the window title passed to the FindWindow function. You can try using the Classname if you want to I guess.
        '
        'Clear the previous correct password even if one wasn’t selected.

        lblPw.Text = "InputBox Password:"

        returnedPW = InputBox("Enter the Password in the box!", inputTitle, "password!", 0)

        If Not returnedPW = Nothing AndAlso returnedPW = "password!" Then

            lblPw.Text = "InputBox Password is: " & returnedPW

        Else

            lblPw.Text = "InputBox Password is: Wrong!"

            MessageBox.Show("Wrong Password!", " Try Again")

        End If

    End Sub

This will set the password if the subclass method said their is a inputbox matching the one we want.

    Public Sub setPassword()

        'The codes below will setup the Inputbox Textbox (Edit Window) to be a password edit box and use the asterisk character as the display character.
        Dim editWindow As Int32

        'The Edit Window (Textbox) in the Inputbox is the first Child in the Inputbox Z Order. So just simply tell the API to get the handle to the first child window.
        editWindow = GetWindow(windowHandle, GW_CHILD)

        SendMessage(editWindow, EM_SETPASSWORDCHAR, asteriskKeyCode, 0)

    End Sub

Now for the subclass based procedure.

    '
    'This is used for the actual Subclassing to monitor for Created_Windows and will check to see if a Window matching the title of the inputbox we want.
    '
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        'Have the base class handle all messages.
        MyBase.WndProc(m)

        'Check if a new window has been created.
        If m.Result.ToInt32 = WM_CREATE Then

            'Check the new window to see if it has the charateristics that we are looking for. Namely that is has the Title that we want to find.
            windowHandle = FindWindow(vbNullString, inputTitle)

            'Make sure that the WindowHandle is not 0 which should mean it have a handle to the Inputbox window that we are wanting.
        ElseIf windowHandle > 0 Then

            'Call the code to set the textbox to a password textbox.
            setPassword()

            'Reset the handle back to 0 since the window handle will be different each time you display Inputbox window.
            windowHandle = 0

        End If

    End Sub

That’s all! As is usually the case, with my articles, there is a example available that show how to do this. Simply head over to my http://www.vbcodesource.com/ site and look under the Visual Basic.NET – Examples. Anyways, have fun!

Jason

Revised: 2015

Auto Complete Support in Textbox and Combobox Controls using VB 6.0 and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 2.00 out of 5)
 

Autocomplete is a very useful feature that Microsoft added with the release of Internet Explorer 5.0 and therefore Internet Explorer 5.0 or higher needs to be installed on the target computer. Adding autocomplete to your applications can be a real benefit to the user and is a easy feature to implement.

Note: Visual Basic 2005 and Visual Basic 2008 already has Autocomplete capabilities in the Textbox and Combobox controls by default.


There are a few different locations you can get the autocomplete data from…

  • File System
  • History List
  • URLs Recently Used
  • ALL URLs
  • ALL System Sources

There are likewise a few different options available to set the way you want AutoComplete to react.

  • Append
  • Suggest
  • Append & Suggest

Visual Basic.NET

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Int32, ByVal _
        completeFlags As Int32)

Visual Basic 6.0

    '
    'The API call that enables the autoComplete feature to your combo or textbox controls.
    Private Declare Sub SHAutoComplete Lib "shlwapi.dll" (ByVal controlHandle As Long, ByVal _
        completeFlags As Long)

The SHAutoComplete API Sub is easy to use. The first parameter is the Handle to the Edit (Combobox/Textbox) control you want to enable Auto Complete too. The second Parameter is how you want your Autocomplete to react and display.


 

    '
    'Various Flags you can set to be the source.
    '
    Private Const Defaults = &H0

    Private Const Filesystem = &H1

    Private Const HistoryList = &H2

    Private Const URLRecentlyUsedList = &H4

    Private Const AllURLs = (HistoryList Or URLRecentlyUsedList)

    Private Const AllSystemsSources = AllURLs Or FileSystem

The flags below are used to specify how you want your autocomplete feature to react.

    '
    'Flags you can use to specify how your want autocomplete to react.
    '
    Private Const append = &H40000000 Or &H20000000

    Private Const suggest = &H10000000 Or &H80000000

    Private Const suggestAppend = &H40000000 Or &H10000000

Now that the API based codes are out of the way, you can setup the edit based controls (Like Textbox and Combobox) to have Autocomplete capabilities. Below is a simple example of adding autocomplete to a textbox control that I named txt.

        'Set autocomplete to the textbox using the system defaults.
        SHAutoComplete(txt.hWnd, Defaults)

        'You can also specify how it will react and what it will display.
        SHAutoComplete(txt.hWnd, suggest Or AllURLs)

If your using .NET, just change txt.hWnd to txt.Handle.ToInt32.

Unfortunately adding this feature to a Combobox isn’t as straight forward. Thats because when you use the Handle/Hwnd Property for the Combobox it returns the Main Window Portion of the control and NOT the Edit Window portion. There are a couple ways to get around this though. One way it to use the GetComboBoxInfo API or to use the FindWindowEx API call. I chose to go ahead and use the FindWindowEx API.


Visual Basic.NET

    '
    'API Function used to get the Edit window of a combobox control.
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As  _
        Int32, ByVal hwndChildAfter As Int32, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32

Visual Basic 6.0

    '
    'API Function used to get the Edit window of a combobox control.
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal handle As _
        Long, ByVal hwndChildAfter As Long, ByVal lpszClass As String, ByVal lpszWindow As String) As Long

Now you just need to use the FindWindowEx Function using the 1st and 3rd parameter. And once you call the FindWindow function, simply pass its return value to the SHAutoComplete Method.

        '
        'Simply change this variable type to a Integer if your using VB.NET.
        Dim cmbHandle As Long

        'Retrieve the handle to the ‘Edit’ Window of the combobox. I named my Combobox control cmb.
        cmbHandle = FindWindowEx(cmb.hWnd, 0, "Edit", vbNullString)

        'Activate Autocomplete to the combobox edit window.
        SHAutoComplete(cmb.hWnd, Defaults)

Tip: Here is how you can specify exactly how to react and display….

        '
        'You can also specify how it will react and what it will display.
        SHAutoComplete(cmb.hWnd, suggest Or AllURLs)

Again, change cmb.hWnd to cmb.Handle.ToInt32 if your using Visual Basic.NET.

Thats all there is to it! Depending on your application, you or your apps user will really appreciate you putting that feature in your program. Well, I guess there is nothing more to add. Have fun!

Jason

How to Get the ShortPathName from a LongPathName in Visual Basic and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

There could be a time when you may need to convert a path to its short name alternative. Some APIs may need you to use the Short Path representation of the normal long path name. To convert a long path to a short path name is very easy to do thanks to a little function. The function is called: GetShortPathName() and as the name suggests, it will convert the path to its short path equivalent. To get started you will want to put the API Function in declaration section of your project.

Visual Basic 6.0 Version

    Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal longPath As _
        String, ByVal shortPath As String, ByVal shortBufferSize As Long) As Long

Visual Basic.NET, VB 2008, Visual Basic 2010, and VB 2013

    Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal longPath As _
        String, ByVal shortPath As String, ByVal shortBufferSize As Int32) As Int32

And now you just need to call that function with your data in the parameters.

Visual Basic 6.0

        'The path you want to convert to its short representation path.
        Dim longPathName As String

        longPathName = "C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705"

        'Get the size of the string to pass to the string buffer.
        Dim longPathLength As Long

        longPathLength = Len(longPathName)

        'A string with a buffer to receive the short path from the api call…
        Dim shortPathName As String

        shortPathName = Space$(longPathLength)

        'Will hold the return value of the api call which should be the length.
        Dim returnValue As Long

        'Now call the function to do the conversion…
        returnValue = GetShortPathName(longPathName, shortPathName, longPathLength)

        MsgBox(shortPathName)

Visual Basic.NET, VB 2008, Visual Basic 2010, and VB 2013

        'The path you want to convert to its short representation path.
        Dim longPathName As String = "C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705"

        'Get the size of the string to pass to the string buffer.
        Dim longPathLength As Int32 = longPathName.Length

        'A string with a buffer to receive the short path from the api call…
        Dim shortPathName As String = Space(longPathLength)

        'Will hold the return value of the api call which should be the length.
        Dim returnValue As Int32

        'Now call the function to do the conversion…

        returnValue = GetShortPathName(longPathName, shortPathName, longPathLength)

        MsgBox(shortPathName.ToString)

That’s all there is to it! The new ShortPathName from the Long path in the example code above converts the path to: C:\WINDOWS\MICROS~1.NET\FRAMEW~1\V10~1.370

So if you’ve been needing a way to get a short path of a long path, this code should do it for you just fine.

Jason

How to make the Applications Titlebar Flash

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 1.00 out of 5)
 

Sometimes you may want to get the attention of your users and causing your apps titlebar is a good way to do so. There is a simple little Windows API call that will do this for you. (This little function can be used with all versions of Visual Basic. Just be sure to set the parameter types to the proper ones for whichever version you are using.) Below is the API declare for the FlashWindow function…

Declare for .NET

Private Declare Function FlashWindow Lib “user32? Alias “FlashWindow” (ByVal wHandle As Int32, ByVal invertStates As Boolean) As Int32

Declare for VB 6.0

PrivateDeclareFunction FlashWindow Lib "user32" Alias "FlashWindow" (ByVal wHandle As Long, ByVal invertStates As Boolean) As Long

All thats left to do it call this function with the handle of your application via the wHandle parameter. You will want to set the invert parameter to True which will make the titlebar flash while changing states.

FlashWindow(Me.Handle.ToInt32, True)

For Visual Basic 6.0 just use it like so…

FlashWindow Me.hWnd, True

If you want your window to flash multiple times, you can call the function in a timer control set to the interval for how often to flash. Otherwise you can use the – FlashWindowEx function. You should have no problems finding info for that function. It gives you more control over the flashing. I will go over that function at another time…

Jason

How to Play Wave Files Easily with VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
 

Playing Wave files with Visual Basic (6.0 and older versions) is quite easy using the Windows APIs. The API you would be interested in using to play wav files is the: PlaySound – API Call. Below is the Constants and Function which should be put in a Module or Declaration section of your project…

   '
    'These Constants and API call was copied from the API-Guide program.

    Private Const SND_APPLICATION = &H80 ' look for application specific association

    Private Const SND_ALIAS = &H10000 ' name is a WIN.INI [sounds] entry

    Private Const SND_ALIAS_ID = &H110000 ' name is a WIN.INI [sounds] entry identifier

    Private Const SND_ASYNC = &H1 ' play asynchronously

    Private Const SND_FILENAME = &H20000 ' name is a file name

    Private Const SND_LOOP = &H8 ' loop the sound until next PlaySound

    Private Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file

    Private Const SND_NODEFAULT = &H2 ' silence not default, if sound not found

    Private Const SND_NOSTOP = &H10 ' don't stop any currently playing sound

    Private Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy

    Private Const SND_PURGE = &H40 ' purge non-static events for task

    Private Const SND_RESOURCE = &H40004 ' name is a resource name or atom

    Private Const SND_SYNC = &H0 ' play synchronously (default)

    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal _
lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Below is how to use the Playsound function to play a wave file…

'
'This will simply play a wave sound from a file location and aSync so it uses
'a thread other than  your applications thread and will not keep your application
'from responding till its done, like playing Sync would do.
'
'The flag: SND_FILENAME I believe is the default for the PlaySound function.
'You can execute'that flag if you want to make sure it plays the sound file fine.
'
PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC)
'
'
'IF you want to use more than 1x flag with the api call, then just call it like below....
'
'This will play a wave file async and loop it over and over till you call the PlaySound
'Function again.
PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC Or SND_LOOP)

And that all there is to it to call and use the Playsound function to play a wave file.

But what if you wanted to stop a wave file that is already playing? That is taken care of below…

'
'This will call the Playsound function to execute and play nothing which will interupt and stop the previous wave file from playing.

PlaySound(vbNullString, 0, 0)

And that is all you have to do to play wav sound files with Visual Basic 6.0. Hope you found this useful 🙂

Jason

 

Minor Revision: 2014

How to Autocomplete ComboBoxes like Textboxes using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

I’ve seen this question before and the answer is pretty simple when you get an understanding of the combo control. When you use the handle associated with the Combobox, it is the main portion of the Control that the handle refers to and NOT the edit box which is the important part.

Note: IF your using Classic Visual Basic (5.0 or 6.0) then simply change the int32 and Integer variable types to Long types since the codes here are setup for VB.NET.


To get the handle to this area of the Combobox will require a windows api call. The API Call is: FindWindowEx. This call will get the Handle of the specified control according to the specified Class Name.

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As  _
        Int32, ByVal hwndChildAfter As Integer, ByVal lpszClass As String, ByVal lpszWindow As String) As Int32

The parameters we are interested in is hWnd and lpszClass. You can set the others to Null. The Handle would be the handle to the main portion of a control. Which is what the ComboBox.Handle.ToInt32 method returns. The class name is the class type you want to get the handle too. Which happens to be “Edit”

        'Will hold the “Edit” handle given by the FindWindowEx API call from the ComboBox.
        Dim cmbHandle As Int32 = FindWindowEx(ComboBox.Handle.ToInt32, 0, "Edit", vbNullString)

And thats all you need to make the combobox Autocomplete capable. Simply specify the cmbHandle integer in the SHAutoComplete ‘handle’ parameter and it will give your combobox the Autocomplete feature!

More Info: If you want the complete source code to autocomplete combobox controls and textbox controls with the actual SHAutoComplete source codes as well, then simply click on this link to view the post.

               Jason

Revised: 2015

How to Easily Play Wave Files with Visual Basic.NET and Newer

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

It’s actually quite easy to play wave files (.wav waveform based sound files) using just alittle code in some cases. The .NET Framework 2.0, 3.5, 4.0, and higher has a managed wrapper around the PlaySound API.

The easiest way to play wave files with Visual Basic 2005, VB 2008, VB 2010, and newer is as shown below…

    'Play a wave sound file from a specified location.
    My.Computer.Audio.Play("mysoundlocation.wav")

    'Stop the file from playing.
    My.Computer.Audio.Stop()

    'Play a Windows system sound.
    My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)

The code above simply used the MY Interface for quick access to the Audio class to play a file, stop the playback, and then playback a system sound.


What about playing wave sound files with Visual Basic.NET 2002 and Visual Basic.NET 2003?

Continue reading