PC Review


Reply
Thread Tools Rate Thread

callback fields of DateTimePicker (Handling of DTN_FORMAT)

 
 
reddog
Guest
Posts: n/a
 
      9th Nov 2008
The System.Windows.Forms.DateTimePicker doesn't support a callback fields at
a custom format. I need to use it, so tring to implement it.

I made a new NewDTP class inherited DateTimePicker, and override WndProc
and handled DTN_FORMAT, DTN_FORMATQUERY and DTN_WMKEYDOWN
messages. But The callback fields was shown empty. could you help me?

It seems to me that DTN_FORMAT handling has something problem.

This is a entire project file.
http://reddog.s35.xrea.com/software/NewDTPTest.zip

This is a NewDTP class I wrote.

namespace NewDTPTest {
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;

public class NewDTP : DateTimePicker {
#region Win32 Defines
private const int WM_NOTIFY = 0x004e;
private const int WM_REFLECT = 0x2000;
private const int DTN_FORMAT = -743;
private const int DTN_FORMATQUERY = -742;
private const int DTN_WMKEYDOWN = -744;

[StructLayout(LayoutKind.Sequential)]
protected struct NMHDR {
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}

[StructLayout(LayoutKind.Sequential)]
protected struct SIZE {
public long cx;
public long cy;
}

[StructLayout(LayoutKind.Sequential)]
protected struct SYSTEMTIME {
[MarshalAs(UnmanagedType.I2)]public short wYear;
[MarshalAs(UnmanagedType.I2)]public short wMonth;
[MarshalAs(UnmanagedType.I2)]public short wDayOfWeek;
[MarshalAs(UnmanagedType.I2)]public short wDay;
[MarshalAs(UnmanagedType.I2)]public short wHour;
[MarshalAs(UnmanagedType.I2)]public short wMinute;
[MarshalAs(UnmanagedType.I2)]public short wSecond;
[MarshalAs(UnmanagedType.I2)]public short wMilliseconds;
}

[StructLayout(LayoutKind.Sequential)]
protected struct NMDATETIMEFORMAT {
public NMHDR nmhdr;
public IntPtr pszFormat;
public SYSTEMTIME st;
public IntPtr pszDisplay;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)] public string szDisplay;
}

[StructLayout(LayoutKind.Sequential)]
protected struct NMDATETIMEFORMATQUERY {
public NMHDR nmhdr;
public IntPtr pszFormat;
public SIZE szMax;
}

[StructLayout(LayoutKind.Sequential)]
protected struct NMDATETIMEWMKEYDOWN {
public NMHDR nmhdr;
public int nVirtKey;
public IntPtr pszFormat;
public SYSTEMTIME st;
}
#endregion


public NewDTP():base() {
}

#region Handles


private void WmFormat(ref Message m) {
NMDATETIMEFORMAT nmdatetimeformat =
(NMDATETIMEFORMAT)m.GetLParam(typeof(NMDATETIMEFORMAT));
string format = Marshal.PtrToStringAuto(nmdatetimeformat.pszFormat);
DateTime time = SysTimeToDateTime(nmdatetimeformat.st);

// XXX: Here is the problem. The "ABCD" does not shown in custom fields.
Marshal.Copy("ABCD".ToCharArray(), 0, nmdatetimeformat.pszDisplay, 4);
m.Result = IntPtr.Zero;
}


private void WmFormatQuery(ref Message m) {
NMDATETIMEFORMATQUERY nmdatetimeformatquery =
(NMDATETIMEFORMATQUERY)m.GetLParam(typeof(NMDATETIMEFORMATQUERY));
string format = Marshal.PtrToStringAuto(nmdatetimeformatquery.pszFormat);
IntPtr dst = (IntPtr)(m.LParam.ToInt64() +
Marshal.OffsetOf(typeof(NMDATETIMEFORMATQUERY), "szMax").ToInt64());

// TODO: the Size value is for debugging.
Marshal.Copy(new long[] { 60, 16 }, 0, dst, 2);
m.Result = IntPtr.Zero;
}


private void WmKeyDown(ref Message m) {
NMDATETIMEWMKEYDOWN nmdatetimewmkeydown =
(NMDATETIMEWMKEYDOWN)m.GetLParam(typeof(NMDATETIMEWMKEYDOWN));
string format = Marshal.PtrToStringAuto(nmdatetimewmkeydown.pszFormat);

//TODO: Do Something

m.Result = IntPtr.Zero;
}
#endregion


private DateTime SysTimeToDateTime(SYSTEMTIME st) {
return new DateTime(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute,
st.wSecond);
}

/// <summary>
///
/// </summary>
/// <param name="m"></param>
/// <returns>If we handled the message return true, else false</returns>
private bool WmReflectCommand(ref Message m) {
if (m.HWnd == Handle) {
NMHDR nmhdr = (NMHDR)m.GetLParam(typeof(NMHDR));

switch (nmhdr.code) {
case DTN_FORMAT:
WmFormat(ref m);
return true;

case DTN_FORMATQUERY:
WmFormatQuery(ref m);
return true;

case DTN_WMKEYDOWN:
WmKeyDown(ref m);
return true;
}
}
return false;
}


[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case WM_REFLECT + WM_NOTIFY:
if (WmReflectCommand(ref m) == false) {
break;
}
return;
}
base.WndProc(ref m);
}

}
}

 
Reply With Quote
 
 
 
 
New Member
Join Date: Jan 2012
Posts: 1
 
      5th Jan 2012
Hi, I have the same problem, did you solve it?
Please help me if you can.
Thank you.
 
Reply With Quote
 
New Member
Join Date: Feb 2012
Posts: 1
 
      3rd Feb 2012
hi,

Just wondering if this has been resolved, cause i have the same problem.

Gary
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTimePicker fields - which one is active? Richard Lewis Haggard Microsoft Dot NET Framework Forms 4 11th Jul 2007 09:46 PM
Handling Strange Database Fields David P. Donahue Microsoft C# .NET 0 28th Mar 2005 03:43 AM
Handling empty fields =?Utf-8?B?WGVybw==?= Microsoft VB .NET 14 26th Oct 2004 11:55 PM
event handling in datetimepicker native control. Sai Kiran Microsoft Dot NET Compact Framework 0 14th Oct 2004 03:24 PM
RE: DatetimePicker with NULL values handling =?Utf-8?B?bWFyY21j?= Microsoft Dot NET Compact Framework 1 1st Jul 2004 10:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:12 PM.