Monday, July 23, 2007

Displaying the Calendar view on a DateTimePicker Control in .NETCF

I've recently made a solution where the customer requested to be able to bring up the calendar view in a DateTimePicker control by pressing on a specific button on the screen. The solution to that was really simple: Create a control that inherits from System.Windows.Forms.DateTimePicker and add a method called ShowCalendar() which I call to bring up the Calendar view.


public class DateTimePickerEx : DateTimePicker
{
  [DllImport("coredll.dll")]
  static extern int SendMessage(
    IntPtr hWnd, uint uMsg, int wParam, int lParam);

  const int WM_LBUTTONDOWN = 0x0201;

  public void ShowCalendar() {
    int x = Width - 10;
    int y = Height / 2;
    int lParam = x + y * 0x00010000;

    SendMessage(Handle, WM_LBUTTONDOWN, 1, lParam);
  }
}

4 comments:

  1. Hi Christian,

    It's Jenson here.

    Hmm, sorry I'm quite new in coding and I found out that I'm always scared of coding something like this, which I have no knowledge or experience on:

    SendMessage(Handle, WM_LBUTTONDOWN, 1, lParam);

    Are they native codes or something else?I don't really know how to name them. I think you are either a C# guy, or you have background on Java, I've been trying hard to squeeze out time to learn Java too -.-"

    Great blog and resources >.<" can learn a lot from here.

    One more thing, your photo is so cool XD

    ReplyDelete
  2. Hi Jenson,

    Nice of you to stop by. And thanks for all the compliments.

    WM_LBUTTONDOWN is the Windows Message that a control receives when it is clicked using the left mouse button. Windows programming can be quite cryptic to see at first.

    Try reading up on Win32 Programming a little, you will see the bigger picture and you might also thank god for managed code!

    As for my programming background, i mainly work with C# now but i've played with quite a few other technologies in the past. I never really got proficient with Java though. I believe that Java was a part of my Linux phase..

    ReplyDelete
  3. yeah, it's always my pleasure to visit blogs, especially those who has helped me a lot. I'll find myself rude if I stop by and say Hi to you =)

    Well, there will be another project coming in pretty soon. I will be quite busy soon -.-"

    And yeah, thanks God for the managed code, else I will have to remember all these WM_LBUTTONDOWN stuff, btw, the new project would use purely PDA for client side, so might be Windows CE 6.0 as they dont need the mobile functions.

    Have a nice day! Take care!

    ReplyDelete
  4. I'm realy trying hard to translate this to VS2008 - and can't get it work - can anybody help ?

    ReplyDelete