Microsoft, VB.NET, Visual Basic, Windows

Transparent Forms in VB.NET WinForms Applications

Creating transparent irregular shaped forms in a WinForms applications using Visual Basic is not too difficult. Using a photographic background with a transparent color, a programmer can create some really attractive interfaces. Of course creating an interface sans the standard titlebar chrome requires a bit more work and programming. However, if you want to provide a memorable interface that is attractive as well as personalized – its worth the extra effort required.

 

Start With a New Windows Forms Application

A few form properties need to be changed before your application form is ready for your photographic background. You’ll change these properties using the IDE (its just easier than coding).

  • Set AutoScaleMode to None
  • Set the BackColor to something like Lime or any other color that you will notice right away (it shouldn’t show anywhere in the completed application)
  • Set the FormBorderStyle to None
  • Set the TransparencyKey to White or whatever color you desire to be transparent in your background image (see below)

Create Your Background Image

Your background image should be created with the background color selected in the previous section for transparency in mind. Anything created or painted on the background image using that color will be transparent. So paint the entire background of the image with that color (on its own layer if possible).

Getting the transparency color exactly right in the image can become an issue (as it did for myself in the past). One version of Lime Green isn’t always someone elses version of the same color. Added to this is the fact that there is nowhere to enter or retrieve a hex color value in the IDE at all.

What I do is use the eyedropper tool in Photoshop on a screenshot of the IDE with the TransparencyKey property showing. This way the color is always right and I know I’ll have no problems afterward. Another way of getting it right is to simply use White as the transparency key color. In this case anywhere you would need white inside the interface you would just use an off shade of white instead. Either way you should always set up the TransparencyKey in Visual Studio first.

Adding the required buttons

After adding your background image to the form you have to add at least one button to the interface – the Close button. A minimize button probably wouldn’t be a bad idea eaither. Keep in mind that if you are adding a maximize button to your interface the extra effort that will be required to support it (graphics wise).

Keep in mind that you really don’t have to use the Button control for buttons. You can use any control that emits a Click event. I find using the Label control with the Cursor changed to a pointer is often the easiest way to get the desired effect in many cases.

Coding these buttons is as simple as creating the Click handler function for the button and closing the application or setting the window state.

close button handler

Making the form draggable

Coding the form to be draggable in Visual Basic is a bit easier than most think. Most of the time I create small, widget like applications when I use transparent forms. Applications like these should be easy to move around so I make the entire form draggable. If you are working on something larger the methodology is pretty much the same except for the fact that you’ll be coding it to a specific part of the form (for instance a custom titlebar – you code the extra math) instead of the entire form.

I use one form global to hold the initial MouseDown location on the form which is considered the offset (remember you’re moving the entire form). In the MouseMove event handler you change the Location property by subtracting that offset from the cursor location and setting it. In the MouseUp event handler  you just set the form global to nothing this way it can be used as a check in the MouseMove event handler.

mouse move code

Finally

Although creating a basic transparent form application seems simple at first glance, your project can get complicated quickly. The more functionality you want, the more things can get more tedious. Creating a maximize button means you’ll have to work out horizontal and vertical tiling on the form and certain components. A custom titlebar means more math in the mouse handler functions. If your application has a lot of animations, perhaps the project should be moved to a XAML imterface instead.

Still, if you’re after a custom look and feel for your app – there’s no better start than a transparent form with a photographic background.

Advertisements
Standard
VB.NET, Visual Basic, Windows

Getting System Information With WMI and VB.NET

35608522201_0687ca50aa_o

Machine Information app

I’ve been getting back into developing with Visual Basic lately. I’ve learned much of what I need to know in the past while in college; or so I thought. Much has changed since those days of Visual Basic 6. Everything is done a bit differently now using dot NET. Before, everything was Component Object Model components – and I really liked Visual Basic 6 for making it easier to develop those type of components. ActiveX controls, servers, hell – even application documents that ran inside of Internet Explorer was just easier to develop with VB than anything else. The Visual Basic runtime dll was in charge of running a lot of business infrastructure in the past.

Now a days its the mighty ubiquitious framework called Dot Net that drives business development. Made up of a huge collection of classes, dot NET is central to every language that runs on Microsoft. Even C++ developers write managed code these days. Instead of a runtime dll we get the Common Language Runtime, which interprets code written in any Microsoft language and churns out a bit of Intermediate Language that runs on a virtual machine. While its true that almost everything I am doing in VB now is a learning experience I see the value in this managed code base already; a lot just got easier.

One of the wisest decisions Microsoft has made recently was the release of the Community Edition of Visual Studio. A corresponding version of Microsofts flagship IDE used to cost a fortune in the past. Now, for the lone developer, its free to download and code with – great! All the code here is written and compiled with this IDE so download it and give it a try.

35570506482_ca1635cb8f_o

Explicitly adding the System.Management assembly reference to my project

Getting system information by querying into the Windows Management Instrumentation system isn’t that difficult. Setting up the project to use the namespace was out of the ordinary – you have to explicitly add the reference to the System.Management namespace assembly to the project. There will be no help from the IDE with this. So you have to go to the Project properties page, click on References in the left column, hit the Add button, click on Assemblies->Framework in the left column, go down to the System.Management namespace and make sure the checkbox is checked. The same goes for the System.ServiceProcess namespace if you’re going to look at the running services as I have.

Basically, I’ve been working off of Microsoft code examples to get to where I am with this. To query WMI you set up and use a ManagementObjectSearcher class with your query and then iterate over the results. The sample application turns the query results into a List of string Dictionaries. Most of the time you’ll want to do a custom query to retrieve the specific information you desire instead of returning entire classes of information the way I have in the example application. Also, the queries take time more time than you might be used to, and this may become a problem. Using a separate thread to run your queries will improve response time. For instance I get a ContextSwitchDeadlock Exception if I do not run the Product query on a separate thread.

Why do the application at all? The one problem I have had is remembering the exact members of the different WMI classes I am querying for. The example app returns the entire class, or array of classes, with null values turned into empty strings for reference sake. It should be enough to get anyone started with building and testing the exact query they need.

There are dozens of scenarios where you would want to glean a bit of information from the System.Management namespace. Hopefully this example can help you on your way.

Download Links:

MachineInfo.exe

Entire Project on GitHub

References:

Standard
Explorer, registry, Visual Basic, Windows

Preview File Types as Text in Preview Pane in Windows Explorer on Windows 10, 8, 7, and Vista

35321504842_fd27e533ef_o

Visual Basic file shown in the preview pan in Windows Explorer (file from the tutorial here)

If you use Windows the way I do you use the Windows Explorer Preview pane to look at text based files. Why? Because it’s just easier that way. Most of the time I have to look back at files to see exactly how I pulled something off in the past. If it wasn’t for the preview pane I would have to open that file up in whatever editor is associated with it. That takes more time.

Setting the preview pane to view a filetype as text is as simple as setting a couple of registry keys. The average user really shouldn’t alter the Windows Registry without knowing what they’re doing as it could render the system unusable. There’s a registry script that can be altered for any file type at the end of the post.

The new, more modern “Windows 10”, way of setting the preview pane to show a filetype as text is by setting two string subkeys on the filetype in HKEY_CLASSES_ROOT. Setting the subkeys “Content Type” to “text/plain”, and “PerceivedType” to “text” usually works for most filetypes. This worked like a charm for my “.json” problem.

However, that isn’t always the case as I found out when tried to preview a Visual Basic filetype. I just expected it to work because I have the most recent version of Visual Studio installed on my system.  So, once again, I opened up REGEDIT and looked at the “.vb” key. I discovered the two previously mentioned subkeys were already set properly.

After trolling the Internet (for too long I might add) I discovered the “old” way. There is another key that needs to be set on the filetype in the Registry. You have to add a special GUID key to the “shellex” key below the filetype and set that with another special GUID (which I assume is the GUID for the Preview Handler for text files). The old way worked for me.

The registry script is set up to set all of these keys. Yeah, the dead keys might add a teensy bit to the Registry size – so it’s opinionated. It’s better to have everything in one place than not. Do I think programmers that invent a new text based filetype for their apps should set these keys? Absolutely, if they’re not going to write a Preview Handler shell extension.

Just replace the “.vb” extension to whatever filetype you want to preview as text.

Download

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.vb]
"Content Type"="text/plain"
"PerceivedType"="text"

[HKEY_CLASSES_ROOT\.vb\shellex]

[HKEY_CLASSES_ROOT\.vb\shellex\{8895b1c6-b41f-4c1c-a562-0d564250836f}]
@="{1531d583-8375-4d3f-b5fb-d23bbd169f22}"
Standard
Analyze-It, Browser Extension, CEH, Google-Music-Search, Google-Open-Storage-Search, Google-Translate-It, Internet Explorer, Look-It-Up, Plurk-It, Share-It, Visual Basic, Wikipedia-Search, Windows

Internet Explorer 11 Context Menu Chooser Application Version 3

Internet Explorer 11 Context Menu Chooser Application Version 3

Internet Explorer 11 Context Menu Chooser Application Version 3

This is an application that allows you to pick and choose which of the many context menu add-ons you would like enabled in your browser. Internet Explorer does not support nested context menus so this I wrote this application to make it easy to enable and disable different menu items you may want to use (instead of separate scripts). The context menu items are broke up into 8 groups: Share-it, Analyze-it, Analyze-it-Scritch, Analyze-it-Viewdns, Plurk-it, Google-it, Look-it-up, and IE-Utility. The IE-Utility menu items are usually located in the Tools menu in the menubar on top which isn’t shown by default (just hit the Alt key and it will pop open temporarily).

There are also a couple of registry tweaks that I included in this application. One that adjusts the size of the thumbnails (on hover) of open applications on the taskbar. And another to give you more New Tab entries. There is also a reset button for the New Tab items you removed and want back. The New Tab functionality is generated automatically based on your browsing (you don’t get actual Speed Dial functionality – Internet Explorer chooses everything you see)

There are a few changes in this new version that should be mentioned. The user interface has changed, the code was ported to be compiled, and there is now a version check that will help keep the application current.

In this version the user interface has been changed to be more convenient. I traded the scripted multi-screen interface for a more basic checkbox based dialog written using WinForms. Its just easier to use having everything in front of you on one dialog while making choices.

The code was ported from jscript/hta to nice modern Visual Basic 14. This should improve application start up time as well as use less memory while running. This compiled version of the application will perform better overall.

The scripting run time has a large overhead and is not maintained by Microsoft as other technologies are. For instance, when scripting a hypertext application the jscript has to target Internet Explorer 4/5 by default. By changing properties on the hta you can target IE8. I would have considered keeping the application scripted if Microsoft ported the hypertext application system to use the Edge or Internet Explorer 11 runtime.

Internet Explorer 11 Context Menu Chooser Application Version Check

Internet Explorer 11 Context Menu Chooser Application Version Check

There is now a version check enabled in the application. This application uses many external tools and features that are completely out of my control. As such, I’ve founds the application needs to be updated more often than other projects I’ve coded. In an effort to keep the application current I enabled a version check that checks weekly in the background that will somewhat automate the process of upgrading. By pressing the “Install new version” button the application will download the updated application in the background (no browser needed) to the Temp folder and start the installer.

The installer has also been tweaked to automatically start the old version uninstaller application (if it detects one) instead of the “do or die” message box that used to pop up. As there is a dependency on .NET 4.5, the installer will detect and start the web installer for the .NET framework as needed (this should only happen in Windows 7 without .NET 4.5).

Why write and maintain this application if Edge is the newest thing? Because desktop users will still prefer Internet Explorer 11 over Edge. Because I can’t see Microsoft having the only browser not running plug-ins. Because its still included in the operating system.

Where is it? Microsoft Internet Explorer 11 is located inside the Windows Accessories folder on the Windows 10 Start Menu. Or you could just click on the symbol in the upper left corner of my application.

 

Download Internet Explorer 11 Context Menu Chooser Application Version 3

Standard