Access Win32/MFC "Stuff" from C#
There's a great code sample in this post:
DateTimePicker automatically move to next datepart
which shows how to clean up some of the behavior of the DateTimePicker in .NET. Unfortunately the code won't compile, since .NET can't understand the WM_KEYDOWN type stuff. I was able to google and get values for a lot of the constants, like
WM_KEYUP = 0x0101;
But I'm really stuck getting NMHDR and WM_REFLECT to work. Is there some sort of Win32 assembly I need to add to my project to 开发者_如何学Pythonget all of this to work?
Thanks!
You need to use what is known as P/Invoke.
[...] As an increasing developer base moves its production applications to managed code, it seems only natural that there will be even more occasions for developers to dip down into the underlying operating system for some critical tidbit of functionality—at least for the time being. Thankfully, the interop features of the common language run-time (CLR), called Platform Invoke (P/Invoke), are very complete [...].
http://www.pinvoke.net/default.aspx/Constants/WM.html
private const UInt32 WM_KEYDOWN = 0x0100;
http://www.pinvoke.net/default.aspx/Structures/NMHDR.html
[StructLayout(LayoutKind.Sequential)]
struct NMHDR
{
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}
Visit pinvoke.net for these declarations. Or use the PInvoke Interop Assistant.
精彩评论