26 Feb 2017 @ 12:21 PM 

BASeCamp Network Menu, which I wrote about previously, was a handy little tool for connecting to my VPN networks. It, however, had one disadvantage- It was clearly out of place- the style was “outdated” for the OS themes:

BCNetMenu displaying available VPN connections.

As we can see above, the style it uses is more like Office 2003, Windows XP, etc. Since the program is intended for Windows 10, that was a bit of a style issue, I think. Since none of the other Renderers really fit the bill, I set about writing my own “Win10” style menu foldout ToolStrip Renderer; Since the intent was merely to provide for drawing this menu, I’ve skipped certain features as a result to make it a bit easier.

Windows 10 uses an overwhelmingly “flat” style. This worked in my favour since that makes it fairly easy to draw using that style. Windows Forms- and thus the ContextMenuStrip one attaches to the NotifyIcon, allows overriding the standard drawing logic with a ToolStripRenderer implementation; so the first step was to create a class which I derived from the ToolStripSystemRenderer. This attempts to mimic the appearance of many Windows 10 foldouts by first drawing a dark background, then drawing a color over top. However- the color over top is where things were less clear. We want to use the Accent Color that is defined in the Windows Display Properties. How do we find that?

As it happens, dwmapi.dll has us covered. However, it bears warning that this is currently an undocumented function- we need to reference it by ordinal, and since it’s undocumented, it could be problematic when it comes to future compatibility. It’s very much a “use at your own risk” function:

This function uses DWMCOLORIZATIONPARAMS, which we of course, need to define:

Once defined, we can now create a helper method that will give us a straight-up color value:

We allow for an “Opaque” parameter to specify whether the caller wants the Alpha value or not; of course, t he caller could always do this itself but the entire point of functions is to reduce code so may as well put it in this way. it takes the 32-bit integer representing the color and splits it into it’s appropriate byte-sized components through shift operators, and uses those to construct an appropriate Color to return.

Using this color to paint over an opaque dark background (the color used with the Taskbar Right-click menu, for example) gives the following Menu, using the new WIndows 10 Renderer I created:

Not a bad representation, if I say so myself! Not perfect, mind you, but certainly fits better than the Professional ToolStrip Renderer, so I don’t think calling it a success would be entirely out of band. A more interesting problem presents itself, however- When configured in the display properties to have transparency effects,The default Windows 10 Network foldout has a “Blur” effect. How can we do the same thing?

After unsuccessful experiments with DwmExtendGlassIntoFrame and related functions, I eventually stumbled on the SetWindowCompositionAttribute(). This could be used to set an accent on a window directly- including, setting Blur Behind. Of course, as with any P/Invoke, one needs to prepare yourself for the magical journey with some declarations:

If the Blur setting is enabled, then the EnableBlur function is called to enable blur; otherwise, to disable blur. In both cases, it tosses in the Handle of the ToolStrip that is opening, which, apparently, is the Window handle to the actual menu’s “Window”, so it actually works as intended:

I also found that darker colours being drawn seemed to be “more” transparent. Best I could determine was that there is some kind of transclucency key; the closer to black, the more “clear” the glass appears. References I found suggest that SetLayeredWindowAttributes() could be used to adjust the colour key, but I wasn’t able to get it to work as I intended; Since the main effect is that the “Disabled” text, which is gray, appears like more “clear” glass within the coloured blurred menu, I found it to be fine.

It will still be ideal to write additional custom draw routines in order to allow checked/selected items in the listing to be more apparent. As it stands the default “Check” draw routine appears more like an overlay on the top left of the icon, but it’s easy to miss; it would be better to custom draw the items entirely, and instead of a checkmark perhaps highlight the Icon in some fashion to indicate selection.

Posted By: BC_Programming
Last Edit: 26 Feb 2017 @ 12:21 PM

EmailPermalinkComments (0)
 16 Feb 2017 @ 5:16 PM 

I’ve adjusted the program to add more options:

  • Font settings can be customized
  • Left-clicking on the Notification Icon will now show the network menu
  • The tooltip will now display connected networks
  • implemented a new “Windows 10 Style” Menu Renderer. This is the default when installed on Windows 10, and will by default use the Windows 10 Accent Color as well. (No blur behind). It’s not precise and is more a stylistic imitation but it fits better with Win10 than the other Renderers (IMO)

As usual the latest source can always be found On github. And The Installer for 1.1 can be found Here.

Posted By: BC_Programming
Last Edit: 16 Feb 2017 @ 05:17 PM

EmailPermalinkComments (0)
 29 Jan 2017 @ 6:11 PM 

I wrote previously about manners in which the SimpleWifi library can be utilized to enumerate available wireless connections and disconnect or connect to them. As my entire reason for writing BCNetMenu, however, was for VPN connections- not for wireless connections- it was necessary to figure out that piece of the puzzle as well.

The approach I discovered may not be entirely forward compatible, however it appears to be functional going back through many versions of Windows, so it ought to keep working. I wasn’t able to find a more official, “sanctioned” method. Thje basic idea is to effectively read the configuration information directly. The Configuration information for VPN connections can be found in the file “%APPDATA%\Microsoft\Network\Connections\Pbk\rasphone.pbk”. I suspect it may also be in the corresponding Common Application Data folder, found set in the %PROGRAMDATA% environment variable.

Retrieving the VPN names is fairly straight forward; effectively, we just want to find the section names. We can use a straight String parse of each line, but we can also use a Regular Expression with a group to define the actual name. Matches to:

is sufficient to find all the appropriate sections, and retrieve the names via the matches. However, the name is not quite enough; we need to cross-reference this information with information available via the NetworkInterface class; then we can use appropriate properties to return a particular data object representing the VPN connection:

is a direct link to the code in question as it appears in the BCNetMenu project. I’ve been pleased with the programs performance over the past month or so in replacing the default Windows network foldout to which I have less positive affinity.

But enumerating connections is one thing- connecting or disconnecting is another. After some searching I ended up finding only one method that was suitable for my use case, as other methods either required manual password input or to have BCNetMenu manage passwords, which I felt was outside the scope of what I wanted to do. Instead, the program will basically just run rasphone.exe with the appropriate VPN name; while this will show a dialog, the saved login information is pre-populated, so, at least for my intended use, I’ve found it sufficient to improve upon the default Windows 10 VPN Foldout.

For the same of comparison, here is the “before” image:

The default Windows 10 Network Foldout

This is an entirely usable foldout- or, it appears that way. However, clicking a connection takes one to the Control Panel. For example, here is the WIndow that appears when I click the connected “Mainframe” option:

From a UI design perspective this boggles my mind. There is zero indication that I clicked “Mainframe” at all. Why are these options listed separately and clickable individually if they all lead to the same place? Clicking a connected VPN connection should disconnect it; clicking a disconnected VPN connection should connect it. The way it has been altered in Windows 10 defies good UI design as far as I’m concerned.

Not that I’m any expert on good UI design; I just know what is easy to use for myself and when a “feature” or alteration causes one to occasionally mumble to themselves angrily or laugh about how silly the feature is even months after it’s introduction it probably wasn’t for the best. As far as getting the desired behaviour, I had two alternatives; the one that I originally used was a registry adjustment which would set the foldout to use the Windows 8 implementation. This worked for some time, however I found that, since the dialog hadn’t had adjustments for Windows 10, some features didn’t work properly; I found in some cases it wouldn’t respond to clicks or refused to connect to a wireless network, but the network control panel functioned as intended. In order to bring back my own desired behaviour, I created BCNetMenu, which appears like this:

BCNetMenu displaying available VPN connections.

It’s not the fanciest thing in the world; it’s not intended to blow anybody’s mind with an amazing glass-like appearance or transparent Window blur or anything like that. It’s a relatively basic pop-up menu that just lists available connections. Clicking a connected one disconnects. Clicking a disconnected one connects.

As it should be, if you ask me!

Posted By: BC_Programming
Last Edit: 05 Feb 2017 @ 10:27 PM

EmailPermalinkComments (0)
Categories: .NET, C#
 08 Jan 2017 @ 12:10 PM 

When I wrote BCNetMenu, it was primarily for replacing Windows 10’s built in network foldout for VPN connections. However since that Network foldout also managed Wireless connections, I decided to add that in as well.

When you have a need that would be filled by a library, it’s always a good idea to look through results on NuGet and see what there is. As with most requirements there are many options when it comes to reading Access Point information. In my case, I settled on SimpleWifi

The Consumer code for SimpleWifi is, well, Simple, which is one of the reasons I opted for it. It provides the information needed and was quite easy to use. Here is an example static that provides an enumerator method that retrieves Access Points:

I found there was an odd issue with this approach. it seemed as if AP info would “trickle” in over time; the second time I opened the menu there would be more access points. I don’t know why that was, but I added a bit of extra code with the intent of providing a larger set of “seed” networks when the menu is opened for the first time. It seems like the act of inspecting Access Points causes more to be actually added. At any rate this is the logic I added to the start of the GetWirelessConnections() logic:

This appeared to rectify my problems, and the Menu that used this method was properly showing available wireless networks appropriately.

Posted By: BC_Programming
Last Edit: 08 Jan 2017 @ 12:10 PM

EmailPermalinkComments (0)
Categories: .NET, C#, Networking
 17 Dec 2016 @ 7:20 PM 

I’ve complained before about Windows 10’s rather odd VPN and even wireless connection interface, in that it has excessive levels of redirection. I went ahead and wrote a small program that appears as a notification icon which attempts to make it a bit more straightforward. It’s not fancy, but it seems to get the job done.

I’ve put it up on github, it can be found here.

Posted By: BC_Programming
Last Edit: 17 Dec 2016 @ 07:20 PM

EmailPermalinkComments (0)
Categories: .NET, C#
 09 Nov 2016 @ 8:30 AM 

I’ve previously written about making adjustments to the Windows Master Volume control programmatically. I alluded to the addition of possible other features such as being able to view the volume levels of other applications. I’ve gone ahead and made those changes.

The first thing to reiterate is that this makes use of a low-level .NET Wrapper for the Windows Core Audio API. This can be found here.

The first thing I decided to define was an object to represent a single Applications Volume Session info/properties. In addition, it will be provided a reference to the IAudioSessionControl interface representing that application’s Audio session, so it can be directly manipulated by adjusting the properties of the class.

Next, we need to declare a COM import, the Multimedia Device enumerator. Specifically, we need to import the class, as the Vannatech Library only provides interfaces, which we cannot instantiate:

Now that we have a starting point, we can create an enumerator method that retrieves all active audio sessions as “ApplicationVolumeInformation” instances:

A github repository with a more… complete… implementation of a working Console program can be found here.

Posted By: BC_Programming
Last Edit: 11 Nov 2016 @ 12:29 PM

EmailPermalinkComments (0)
Categories: .NET, C#, Programming, Windows
 27 Oct 2016 @ 12:39 PM 

This is part of a series of posts covering new C# 6 features. Currently there are posts covering the following:
String Interpolation
Expression-bodied Members
Improved Overload resolution
The Null Conditional operator
Auto-Property Initializers

Yet another new feature introduced into C# 6 are a feature called Dictionary Initializers. These are another “syntax sugar” feature that shortens code and makes it more readable- or, arguably, less readable if you aren’t familiar with the feature.

Let’s say we have a Dictionary of countries, indexed by an abbreviation. We might create it like so:

This is the standard approach to initializing dictionaries as used in previous versions, at least, when you want to initialize them at compile time. C#6 adds “dictionary initializers” which attempt to simplify this:

Here we see what is effectively a series of assignments to the standard this[] operator. It’s usually called a Dictionary Initializer, but realistically it can be used to initialize any class that has a indexed property like this. For example, it can be used to construct “sparse” lists which have many empty entries without a bunch of commas:

The “Dictionary Initializer” which seems more aptly referred to as the Indexing initializer, is a very useful and delicious syntax sugar that can help make code easier to understand and read.

Posted By: BC_Programming
Last Edit: 27 Oct 2016 @ 12:40 PM

EmailPermalinkComments (0)
Tags: , ,
Categories: .NET, C#, Programming
 08 Oct 2016 @ 4:38 PM 

One of the old standby’s of software development is manipulating bits within bytes. While it used to be that this was necessary- when you only have 8K of RAM, you have to make the most of it, which often meant packing information. Nowadays, it’s not quite as necessary, since there is so much more RAM on a typical system and it’s generally not worth the loss of performance that would come from packing unpacking bits for the tiny memory savings that would be afforded.

There are, of course, exceptions. Sometimes, trying to use conventional data types together with functions or other operations that work with compact data representations (For example, generating a Product Key) can be as awkward as Hitler at a Bar Mitzvah. In those cases, it can be quite useful to be able to pack several Boolean true/false values into a single byte. But in order to do so, we need to do some bit bashing.

Bit Bashing

“Bit Bashing” is the rather crude term for Bit manipulation, which is effectively what it says on the tin- the manipulation of the bits making up the bytes. Even the oldest Microcomputer CPUs- the Intel 4004, for example- worked with more than one bit at a time, in the case of the 4004, it worked with 4-bits at a time. The original IBM PC worked with a full byte. This means that operations work at a higher level even then the bit, so it takes some trickery to work at that level.

The core concepts are easy- you can use bitwise operators on a byte in order to set or retrieve individual bits of the byte.

Setting a Bit

Setting a bit is a different operation depending on whether the bit is being cleared or set (0 or 1). If the bit is being set, one can do so by using a bitwise “or” operation with a shifted value based on the desired bit to change:

Straightforward- create a byte based on the specified bit index (0 through 7) the bitwise or that against the original to force that bit to be set in the result.

The converse is similar, though a teensy bit more complicate. to forcibly set a bit to 0, we need to perform a bitwise “and” operation not against the shifted value, but against the bitwise complement of the shifted value:

Retrieving a bit

Retrieving a bit is as simple as seeing if the bitwise and between the value and the shifted byte is non-zero:

Being able to encode and decode bits from a byte can be a useful capability for certain tasks even if it’s necessity due to memory constraints may have long since passed.

Posted By: BC_Programming
Last Edit: 08 Oct 2016 @ 04:38 PM

EmailPermalinkComments (0)
Categories: .NET, C#, Programming
 17 Sep 2016 @ 3:42 PM 

Previously I wrote about implementing a Alpha-blended form with VB.NET. In that implementation, I had an abstract class derive from Form and then the actual forms derive from that. This causes issues with using the Form in the designer.

In order to workaround that issue I’ve redone parts of the implementation a bit to get it working as it’s own separate class. Rather than rely on the CreateParams() to adjust the GWL_EXSTYLE when the form is initially created, it merely uses SetWindowLong() to change it at runtime. Otherwise, the core of what it does is largely the same- just refactored into a package that doesn’t break the designer.

It should be noted, however, that adding controls to the form will not function as intended, though- this is inherent in the Alpha Blending feature, as it effectively just draws the bitmap. This is why it works well for Splash Screens. Controls will still respond to events and clicks however they will be invisible; making them visible would require drawing them onto the Bitmap and then setting it as the new Layered Window bitmap each time controls change.

Here is the changed code:

In usage it is no more complex than before, really:

The end result is largely the same:


Posted By: BC_Programming
Last Edit: 15 Oct 2016 @ 01:51 PM

EmailPermalinkComments (0)
 03 Aug 2016 @ 4:37 PM 

I wrote previously, where I found that the Group Policy added to the insider build of Windows 10 did not have any observable effect.

This is merely a quick note as I re-tested on the recently released Anniversary Update, and with the group policy set in addition to the new “longPathAware” manifest setting, Functions like CreateDirectoryW do in fact allow access to long path names beyond MAX_PATH without the special prefix. Basically, the group policy is now active.

Of course, with the standard .NET File API, This has no effect, as the .NET File API does it’s own checks that restrict the path length.

Posted By: BC_Programming
Last Edit: 03 Aug 2016 @ 04:37 PM

EmailPermalinkComments (0)
Categories: .NET, C#

 Last 50 Posts
Change Theme...
  • Users » 41921
  • Posts/Pages » 350
  • Comments » 104
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight


    No Child Pages.

Windows optimization tips

    No Child Pages.

BC’s Todo List

    No Child Pages.