简体   繁体   中英

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 get 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM