Category Archive: C#

Subcategories: No categories

Spotify Notifier

Spotify is my music player of choice and naturally I want to be able to know what I’m listening to without bringing up Spotify.

At work this is no problem as macs have growl (yes, iMac at work :). At home I have a Windows machine, although while there is growl for windows, Spotify doesn’t use it :sadface:

Solution? Write my own notifier of course, made possible by the fact that Spotify has the current track in its window title.

Long story short, it does this:

Fades in, and fades out again when a new song is playing. It also remembers its position when closed.

Its not configurable, changes to fade speed, time etc. would need to be done in code.

Written in C# with VS2008, source available via GitHub.
ClickOnce install/launcher: ClickOnce (click launch rather than install)
Traditional Installer: SpotifierNotifier-1.0.1.2 (Zip) (RAR)

New in version 1.0.1 21/05/2012 (Thanks to Petter Östergren for suggesting improvements)

  • Popup can now be resized
  • Popup remembers position/size when moved rather than closed
  • Should not steal focus

Threaded flicker-free control rendering in c#

I recently ran into a problem where a control was rendering slow enough that it caused flickering (as the screen region is cleared and then redrawn).

Looking around the ‘net I settled on rendering the control to a bitmap and then drawing the bitmap to the screen (fast operation), which is called double buffering.
(more…)

Text block clock

Clock

This is a clock that tells you the time by highlighting text in a block of text. In this case using a C# Console App. Not exactly original, but I thought I’d give it a try.
The progress bar counts the time until the next 5 minute mark.

Download Code
Download Executable
(more…)

Maximizing a print preview dialog in c#

Here is a way to get a print preview dialog (or perhaps any resizeable dialog) to be maximized when opened.

The dialog may be cast to a Form allowing WindowState to be set.
For example:

PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = doc;
((Form)dlg).WindowState = FormWindowState.Maximized;
dlg.ShowDialog();

Detecting drive insertion and removal in C#

Here is some C# code to detect when a logical volume (e.g. USB Memory Stick) is inserted or removed via the WM_DEVICECHANGE message with WndProc.

However this doesn’t tell you what has been inserted/removed, you will have to poll the drives manually to find out.
If you need that you will have to register to receive events from windows which involves P/Invoking RegisterDeviceNotification (See Drive Detector at Code Project).

[...]
using System.Runtime.InteropServices;
using System.Security.Permissions;

public class MyForm : Form
{
        const int WM_DEVICECHANGE = 0x0219;
        const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
        const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //device was removed
        const int DBT_DEVNODES_CHANGED = 0x0007; //device changed
        const int DBT_DEVTYP_VOLUME = 0x00000002; // logical volume

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_DEVICECHANGE
                && m.WParam.ToInt32() == DBT_DEVICEARRIVAL
                || m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE
                || m.WParam.ToInt32() == DBT_DEVNODES_CHANGED))
            {
                if (m.WParam.ToInt32() != DBT_DEVNODES_CHANGED)
                {
                    int devType = Marshal.ReadInt32(m.LParam, 4);
                    if (devType == DBT_DEVTYP_VOLUME)
                    {
                        //Poll drives
                    }
                }
                else
                {
                    //Poll drives
                }
            }

            base.WndProc(ref m);
        }
}

Live Mirror

Here’s a C# program to mirror all changes in one directory with another.

I build it to let me edit a PHP web project on my development PC and have it mirrored to my development server, without any action on my part.

Its all being hosted at Google Code, go on give it a try :)
(more…)

Lambda's and Anonymous functions in C# 3.5

Here’s a lambda function in use with the new extensions for a List object.

personnel.foreach(x => x.DoSomeCoolStuff());

(more…)

360° Angle Class for C#

Here is a nifty class to handle an angle. it will wrap around increments and decrements that exceed 360 or 0, e.g. adding 20 to 350 will give 10 not 370 and subtracting 10 from 5 will give 355 not -5.

  • Implicit conversion to and from double
  • ToString and GetHashCode are mapped to the inner angle
  • Operators +,-,>,>=,<,<= have been implemented.
  • Mixing this class and doubles in statements works seemlessly.

Code sample:

Angle angle = 0;
double angle2 = angle + 5;
Console.WriteLine("a1:{0) a2:{1}",angle,angle2); 
//Output: a1:0 a2:5

Files

C# Console Framework

This is a small side project that i did in my first year of university, I’ve since updated it as it was a bit hacky and built for .net v1 (now v2).
It can :

  • Read text, integer and float values from the console, while validating them within the given limits
  • Show simple menus
  • Show Titles (see below)

And all with a cool border/scrolling effect! (more…)