I’ve developed a borderless form, with full functionality, in pure code. I’ve avoided design view, because… well, just because I felt like it. Coming from a C++ background, I’m quite used to coding everything in myself.

NOTE: The output is quite ugly, but this is a basic example to demonstrate the functionality of a custom title bar. (Particularly the event handlers)

Now, down to business.

First off, we start with our using directives. We’re actually not using too much:

Now for our class variables. The scope of these is important: if we create them in our functions, we won’t have access to them in the custom event handlers:

The constructor is also very basic. I’ve commented it enough to just paste here. As I said before, it’s the event handlers we’re interested in. (Most of the constructor is just hard-coding the design anyway)

Look how many event handlers we need! But, don’t worry. Many of them can be reused, by checking which object is the sender.

This is where the interesting stuff comes into play. Working with those events.

Changing the colour

Appearance is key. While my design may be ugly, it’s important to add nice little quirks like colour changing.
First off, let’s change the appearance when the mouse enters/leaves our labels.
When the mouse enters the label, we want it to change colour to say “you’re about to click me?”
When it exits, we want to change it back:

Deciding what to do

Now, we’ve got three options: minimise, maximise, and close. We have a this.Close() function, but we don’t have minimise/maximise/restore functions. Looks like we’re going to have to code those in ourselves! Simple enough though, we can use FormWindowState like so:

Moving the form

This is the bit I really enjoyed coding. Making the form move. We do this with the event handlers of MouseUp, MouseDown, and MouseMove. If the mouse is down, we want to move it. If not, we don’t want to. This is where drag comes into play, along with startPoint.

The Final Product

Now a challenge for you

What? You’re going to want to do something on your own, right?
As an added bonus, you could also add in a context menu. When you have a borderless form, unfortunately, it automatically takes out the context menu when the user right clicks the program in the task bar.

Here’s a few hints:

  • The message for the task bar right clicking on your program is 0x0313
  • Try overriding the WndProc method: protected override void WndProc(ref Message m)

Finally, if you don’t want to do it, here it is: (you didn’t think I’d leave anything unanswered, did you? This is a tutorial, after all!)

Written by Danny