Archive

Archive for the ‘Uncategorized’ Category

How to use WM_SETICON properly

July 17th, 2009

It’s not rocket science, but you’d be surprised to see how many people get this wrong. Here’s a code snippet of what people usually do:

const HICON hicon = ::LoadIcon(
  ::GetModuleHandle(0),
  MAKEINTRESOURCE(IDR_MAINFRAME)
  );
if (hicon)
{
  ::SendMessage(hwnd, WM_SETICON, ICON_BIG, reinterpret_cast(hicon));
  ::SendMessage(hwnd, WM_SETICON, ICON_SMALL, reinterpret_cast(hicon));
}

This code would set the large icon (as displayed in the ALT-TAB switch window dialog) and the small icon (as displayed in the window title bar or the task bar) of the window (whose handle is hwnd).

Except for the error checking which is not particularly thorough, most people would consider this code to be correct. You could also have used LoadImage with LR_DEFAULTSIZE.

The problem reveals itself with an icon which has a sligthly different design for the small 16×16 size compared to the large 32×32 size. The 16×16 icon will look like a scaled down version of the 32×32 one. The reason why the smaller icon looks different in the first place is usually because the large icon’s design is a bit overloaded and cannot be downscaled without becoming unrecognizable; so you will notice!

What’s wrong? If you dig into the documentation you will found the following information:

  • LoadIcon loads the most appropriate icon, at one fixed size, which is SM_CXICON by SM_CYICON pixels, ie the size of a large icon.
  • The small icon should be SM_CXSMICON by SM_CYSMICON pixels.
  • WM_SETICON for ICON_SMALL would downscale a larger icon to SM_CXSMICON by SM_CYSMICON pixels automatically.
  • This odd behavior is due to backward compatibility with legacy versions of windows, which only had one size of icons.

Therefore the fix is easy, all you need to do is call LoadImage twice and explicitely provide the size of your icon.

const HANDLE hbicon = ::LoadImage(
  ::GetModuleHandle(0),
  MAKEINTRESOURCE(IDR_MAINFRAME),
  IMAGE_ICON,
  ::GetSystemMetrics(SM_CXICON),
  ::GetSystemMetrics(SM_CYICON),
  0);
if (hbicon)
  ::SendMessage(hwnd, WM_SETICON, ICON_BIG, reinterpret_cast(hbicon));

const HANDLE hsicon = ::LoadImage(
  ::GetModuleHandle(0),
  MAKEINTRESOURCE(IDR_MAINFRAME),
  IMAGE_ICON,
  ::GetSystemMetrics(SM_CXSMICON),
  ::GetSystemMetrics(SM_CYSMICON),
  0);
if (hsicon)
  ::SendMessage(hwnd, WM_SETICON, ICON_SMALL, reinterpret_cast(hsicon));

Reference (MSDN):

Programming, Uncategorized, Windows, win32

10 tips to avoid spam

March 1st, 2009
Comments Off

I set up email on my phone and even though I did not receive that many spams it was more annoying than on a computer. Over the past few months I changed one of my main email addresses and took some radical steps to fight against spam.

Here are my few tips for a spamless life:

  1. Do not publish your email address anywhere.
  2. Do not register your email address anywhere.
  3. If you need to register to a website then create a new account. Use this account just for the purpose of registering and enable mail forwarding to your main account. As soon as you start receiving spam from that account (or legitimate but annoying commercial content), disable email forwarding but leave the account alive (so that spammers waste resources).
  4. Never click on “unregister” links from emails.
  5. Disable HTML viewing especially the viewing of images. You may set up a whitelist from known senders.
  6. Use state of the art antispam. I really love google gmail antispam. I forward it to my final email provider that does use good spam-assassin rules.
  7. Try to have a core of 2 or 3 different email addresses shared among the people you know (no websites!). When you need to change your email, you will be upsetting fewer people. An idea is to set up groups like: family, friends from university, other friends, colleagues or former colleagues… groups of people you are unlikely to email simultaneously.
  8. Update your core email addresses when they are compromised. Do it gradually over an entire year by keeping email forwarding on and adding an auto reply rule informing your contacts they should stop using the old email. Do not write your new email in clear inside the automated message because it could be harvested by bots.
  9. If your address is publicly available and spammed in huge amounts, make it mandatory for people to add some text in the title in order to contact you. Filter out everything not complying to the rule.
  10. If your address is publicly available and the previous tip does not work (someone is deliberately targeting you) remove the contact email address from public space and replace it with a contact form using captcha.

Uncategorized

Hello World!

February 21st, 2009

Very original title, very original first post! 

And a brand new website, with a very original title too!

Uncategorized