Category Archives: Hardware/Peripherals

Posts and code related to hardware or peripheral programming in VB 6.0 & .NET.

Updated: USB using Visual Basic.NET and VB 6.0

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

The purpose of this post is simply to give some info to help with usb programming like detecting when a usb device has been added/removed, which the .NET based class helps with, ect…


Visual Basic 6.0

The first library is for Visual Basic 6.0 and is provided by Intel (Intel is the main inventer of USB as well) and includes various bas/modules for use in your applications. BTW, these  VB 6.0 USB modules do more than the below .NET library.

Click here for the Main Intel page with the downloads and tutorial…

Edit: The Intel link above is no longer valid. I have not been able to find where it was moved to or if its completely taken away. I still have the VB Bas files that are needed to do the USB programming.

Click here to download the modules.


Visual Basic.NET Compatible Class Library

Strolling through some of the latest updates at thecodeproject.com I noticed someone made a USB library using C# for detecing the attachment and detachment events of usb devices. He provides the source code, compiled class, and a demo app with the usb library in action. If you simply want to reference the class download the source code version. Then under the Bin/Release directory you will see the ‘USBClassLibrary.Dll’ which is what you want to reference.

Click here for the main USB Library page…


Hopefully this post was of some help. Have Fun!

Jason

Detecting if the User is Active or Idle – SystemWide using VB 6.0 and VB.NET

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

There are times when you want to know not only if your user is interacting with your application but actually interacting anywhere on the computer. Especially if you are waiting for somekind of user Input. Microsoft thankfully made it easy as I found a API call on MSDN.com and I converted it from C++ to work with Visual Basic (Works for .NET, VB 2008/2010, VB 2013, and 2015). I actually have a VB 6.0 Example for doing this at my www.vbcodesource.com website. Below is the little API call…

Visual Basic 6.0

    '
    'The actual API call to use.
    Private Declare Function GetLastInputInfo Lib "user32.dll" (ByRef inputStructure As inputInfo) As Boolean
    '
    Private Type inputInfo

        structSize As Long

        tickCount As Long

    End Type

Visual Basic.NET Version…

    '
    'The actual API call to use.
    Private Declare Function GetLastInputInfo Lib "user32.dll" (ByRef inputStructure As inputInfo) As Boolean
    '
    Private Structure inputInfo

        Dim structSize As Int32

        Dim tickCount As Int32

    End Structure

Now that the APIs are out of the way, you only need to setup and use the codes. The code below will work for ALL VBs with just some minor changes…


Visual Basic 6.0

        '
        'The variable that will be passed to the API call and receive the activity report.
        Private info As inputInfo
        '
        'Visual Basic 5.0/6.0…
        Dim firstTick As Long
        Dim lastTick As Long

Visual Basic.NET…

        '
        'The variable that will be passed to the API call and receive the activity report.
        Private info As inputInfo
        '
        'Visual Basic.NET…
        '
        Dim firstTick As Int32
        '
        Dim lastTick As Int32

All I did next was use the code in a Timer control and set the Interval to 1000ms (1 Second) which will update the lblTime Label Control the Last time of day the user was active. The InputInfo structure will actually give you the tickcount for when the user was last active if you want to use that. I decided to simply get the actual time they were last active

        '
        'This timer will fire every 1000ms(One Second) or so displaying the last time the user was active.
        '
        'The size of the structure for the API call.
        info.structSize = Len(info)
        '
        'Call the API.
        GetLastInputInfo(info)
        '
        'Compare the tickcount values to determine if activity has occurred or not.
        If firstTick <> info.tickCount Then
            '
            'Display the current time of the users last activity.
            '
            'Change lblTime.Caption to lblTime.Text if using .NET and likewise change Time
            'to Now instead.
            lblTime.Caption = "Last Active: " & Time
            '
            'Get the new tick value.
            firstTick = info.tickCount

        End If

The example program wth the code in action


As I mentioned already, I put that code above in a Timer control, then I set to enable and disable the timer in Two Button controls.

Now your application will know when the user was last active on the computer. But remember, just because it says they are Not active doesn’t mean that they are not reading a document, a website, or some other task that doesn’t require user input. Have fun!

          Jason

Revised: 2015

How to get the Computer Screens Resolution using VB and VB.NET

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

At some point you may need to get a computers monitor/video resolution for sizing your applications and/or your applications controls/fonts or whatever. It is actually easy to do so in ALL versions of Visual Basic. Only a very small amount of additional code is required to get the Resolution in 6.0 compared to doing it in .NET. Below is doing it in VB 6.0.


Visual Basic 6.0

To get the resolution in VB Classic is quite easy. You can use the following code below…

Screen.Width – For the Horizonal Resolution.

Screen.Height – to get the Vertical resolution.

But if you use those codes by themselves, it will not return the resolution in Pixels which is probably the format you are wanting. There is an extra piece of code for getting the resolution in Pixels…

Screen.Width / Screen.TwipsPerPixelX

Screen.Height / Screen.TwipsPerPixelY

Below is the code put together with a Messagebox displaying the resolution for the screen…

'
'Simply throws a messagebox with the current resolution of the screen...
MsgBox("The Screen Resolution is: " & Screen.Width / Screen.TwipsPerPixelX & "x" & Screen.Height / Screen.TwipsPerPixelY)

What about the VB.NET versions? Even easier actually.


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

What we want is the Screen class located at:

System.Windows.Forms.Screen

This handy little Class has data related to the computers display screen. Below is a simply example…

'Simply display a messagebox with the resolution of the computers screen.
MsgBox(Screen.PrimaryScreen.Bounds.Size.ToString)

If a computer has more than one(1) screen/monitor, then you could also use the ‘Screen’ class to get the resolution for that screen.

Example: Say your application was running on a computer with Two(2x) monitors/displays and you wanted to know the resolution of the monitor your Form is shown on or the monitor that is showing the biggest portion of your Form? The Screen Class can help you with that…

'
'If you had multiple displays, and you wanted to know the resolution of the display
' that contains your form, this code would do that for you.
MsgBox(Screen.FromControl(Me).Bounds.Size.ToString)

 

In Visual Basic 2005 and/or Visual Basic 2008, VB 2010, and newer you can even use the MY interface to access the Screen class.

Example…

'
'Use the MY shortcut in VB 05/08 to get the sacreen size...
MsgBox(My.Computer.Screen.Bounds.Size.ToString)

Well, thats all there is to it if you wanted to get the Computers Display/Screen Resolution in your Applications. Its easy to do from Visual Basic 5.0/6.0 through to Visual Basic 2005/2008. Have fun! 🙂

Jason