Sunday, 25 October 2009

Style sheets for IDEs

As programmers, code layout is very important to us.

If code is presented in a way we are familiar with, we can skim-read entire passages of code in very little time and find our way to the significant details very quickly. With well presented code, you can often see the general flow of the code without even having to read the fine details.

Conversely, if the code we read is layed out poorly, it can be a real struggle to read through, and can take a very long time to get even a basic understanding of the code.

Unfortunately, "well presented" is a very personal, subjective description of code.

Indent with 3 spaces or 4? Tabs or spaces? Braces on the same line as other code? Alignment of variable names? Prefix? Hungarian?

There are hundreds more examples. If you surveyed every programmer you knew, I'm sure you wouldn't find any two who agreed on every point of code layout.

The trouble is, there is no right answer - there are valid reasons for all the choices.

What we need, is a way of separating out the code from its layout - syle from substance.

When we have IDEs which can present any piece of code in your style, we'll have made a great leap in the speed of code comprehension.

Wednesday, 22 July 2009

What do I want from a mobile phone?

  • I want good audio quality!
  • I want long battery life - specifically I want to be able to turn the screen on and off in order to save the battery - even during a phone call!
  • I want to be able to turn the phone off and the alarm clock to still work (I don't want to leave a "live" mobile phone on my bedside table at night every night).
  • I want the phone to charge via USB - then I don't have to carry my charger around with me everywhere.
  • I want the interface to be as customizable as possible - if I don't have a data plan, 100 internet shortcuts are useless to me!
  • I want the phone to vibrate for a phone call or text message - but not when navigating menus!
  • I want tactile feedback from the buttons on the phone - I often "touch-type" with my mobile, but if some buttons don't have the same "click" as other buttons it will throw me off.

Friday, 5 June 2009

Open File Security Warning Removal

Fed up with having to deal with the security warning every time you download a file from the internet? Supress the warning dialog with the instructions found in the first link on this page.

Go to Run -> gpedit.msc
User Configuration -> Administrative Templates -> Windows Components -> Attachment Manager -> Inclusion List for moderate risk file types.
Enable it and in the box specify the extensions such .exe, .jpg, .com

Thursday, 4 June 2009

TreeViewScroll

The .net TreeView doesn't expose a scroll event. Make your own by overriding DefWndProc.

   public class TreeViewScroll : TreeView
{
private const int WM_VSCROLL = 0x115;
private IntPtr SB_ENDSCROLL = new IntPtr(8);

public event MethodInvoker Scroll;

protected void OnScroll()
{
if (Scroll != null)
Scroll();
}

protected override void DefWndProc(ref Message m)
{
if (m.Msg == WM_VSCROLL)
{
if (m.WParam != SB_ENDSCROLL)
OnScroll();
}

base.DefWndProc(ref m);
}
}


The code above is sufficient for my needs, but if you need more details about the scroll (direction, where you scrolled to, etc) you need to interrogate the message further.

This link should tell you what the message contains:
http://msdn.microsoft.com/en-us/library/bb787577(VS.85).aspx

This link can tell you the scrollbar values:
http://www.pinvoke.net/default.aspx/Enums/ScrollBarCommands.html

Thursday, 28 May 2009

Stuck in a rut

Don't get stuck in a rut! Don't miss the wood for the trees!

The other day, I was working out some long, convoluted workaround for dealing with the fact that vector iterators can become invalid if the vector resizes.

Then someone reminded me that you can access elements of a vector by index. D'oh!

Wednesday, 27 May 2009

If in doubt, wiggle

A few years ago, part of my job involved making large print runs in Microsoft Word - in the region of 500 to several thousand pages per job. While doing this I discovered an unusual quirk.

In the status bar a little animated icon appears alongside a counter that tells you how far through your print job you are. Quite frequently with the large print jobs, I noticed that the counter would freeze on quite a low number. The printer would catch up to the counter and then nothing would happen for a little while.

But if you wanted, you could "remind" Word to carry on printing, by wiggling the moue over the status icon! While the mouse was moving, the status icon's counter was counting upwards - as soon as you stopped moving, the counter stopped counting.

An obvious explanation might be that the counter only repainted when mouse moved over it; but I experimented - the printer didn't print pages unless the status counter was incrementing. Whenever the counter froze, the printer stopped, and as soon as I moved the mouse over the icon, printing resumed.

How odd.

Sunday, 8 March 2009

Zipping in C#

After spending hours digging around, trying to find the .net solution to zipping folders using c#, the easiest solution seems to be to use the command line for 7zip.

String zipper = @"c:\program files\7-zip\7z.exe";
String args = "a -tzip " + compressPath + " " + compressFolder;

Process proc = Process.Start(zipper, args);
proc.WaitForExit();



where compressPath is something like "C:\foo.zip" and compressFolder is something like "C:\foo".

Update: 9th March 2009
It's probably a good idea to make sure both compressPath and compressFolder are surrounded by quotes in case they contain spaces!