Skip to content

Instantly share code, notes, and snippets.

View kirsbo's full-sized avatar

Morten Kirsbo kirsbo

  • Kirsbo Consult AB
  • Malmö (Sweden) and Copenhagen (Denmark)
View GitHub Profile
@kirsbo
kirsbo / How to add global hotkeys to applications in VB.NET.md
Last active February 7, 2024 17:05
VB.NET - How to add global hotkeys to applications in VB.NET

Authored in 2010

When you want to make your application do things by the user pressing a hotkey / key combination, even when your application does not have focus, you will need a so called global hotkey. A global hotkey is a form of "keyboard hook" which is a low level way of monitoring the computer for a certain key combination, aka hotkey. Below is a simple class, which let's you register a hotkey and define what code should execute, once the hotkey is pressed.

    Public Class Hotkey

#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
@kirsbo
kirsbo / VB.NET Winforms - Making form with formBorderStyle=none draggable.md
Last active November 28, 2023 13:17
VB.NET Winforms - Making form with formBorderStyle=none draggable

Authored in 2010

When building UI's for desktop applications in Winforms, you occassionally need full graphical control of the form. The default form with a title bar is thus not suitable. Setting the formBorderStyle property of forms to none, means it no longer has a top title bar, thereby giving full graphical control of the window.

A common problem with this however, is that the user can no longer move the form by dragging it's titlebar around the screen (as the titlebar has been hidden).

The following code is a workaround that allows forms to be dragged around, like with any usual application. It has to be entered in the code-behind for the form for which you want to enable dragging.

@kirsbo
kirsbo / C# - Get name of currently active application.md
Last active October 2, 2022 20:26
C# - Get name of currently active application

Authored in 2011

The following code can be copy pasted directly into a C# class. Calls to getCurrentAppName() will return a string with the name of the current application.

        #region "WinAPI"

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
@kirsbo
kirsbo / Capture process information from the active application in VB.NET.md
Created December 19, 2015 19:37
Capture process information from the active application in VB.NET

Authored in 2010

Capturing process information from the currently active application can be useful when coding interactions between applications.

The code for getting this information is relatively simple, though it involves a few different Windows API calls.

First we need to declare the Windows API functions, which are done as follows. Enter the following code in a class of your choice:

@kirsbo
kirsbo / Saving XDocument to itself in VB.NET.md
Created December 19, 2015 19:41
Saving XDocument to itself in VB.NET

Authored in 2010

While working with the LINQ XDocument object, I was attempting to modify the XML file loaded into the XDocument and then save it to itself. And while XDocument has a Save method, it needs a file path parameter. By default, XMLDocument would save to itself if no file path parameter was passed, but not so with XDocument.

Instead, you have to retrieve the file path of the loaded XML file using the BaseURI property of XDocument. The BaseURI property contains a URI (Uniform Resource Identifier), which essentially is a file path, to the "base", i.e. The loaded document. To save the XDocument to itself, you'd then think that all you need would be:

Dim xdoc As XDocument = XDocument.Load("C:\Test\Test.xml")
@kirsbo
kirsbo / Getting description from a process in VB.NET.md
Created December 19, 2015 19:39
Getting description from a process in VB.NET

Authored in 2010

The process object contains a lot of different information about the process in question, however it lacks the "description" of a process.

To see the description of a given process, hit CTRL+ALT+Delete and start the task manager. From there navigate to "Processes" and look under the "Description" column on the far right. This description is typically the easiest way to identify an application. You would think the process name would be the most obvious choice to identify an application, but this name is not always obvious.

For example, the Visual Studio 2010 process is called "devenv". Not very telling. The description of the process on the other hand is much more usable: "Microsoft Visual Studio 2010". That one doesn't leave much to the imagination, and typically this is what you are after if you are trying to identify an application based on a process.

As mentioned the process object lacks the description of a process. This is because the description is actually not stored in the proce

@kirsbo
kirsbo / Show form on top of desktop with WinAPI and VB.NET.md
Last active December 19, 2015 19:35
Show form on top of desktop with WinAPI and VB.NET

Authored in 2010

Showing a form above the desktop but below ordinary applications, can be a useful method when trying to develop an application that feels integrated to the Windows UI. This could typically be desktop gadgets / widgets and similar. You want a form that is shown always, even when users perform a "Minimize all" command.

Add the following class to your project:

Public Class WinDesktop