// Same a a click on the Date Time drop down button void IInvokeProvider.Invoke() { // Make sure that the control is enabled if (!SafeNativeMethods.IsWindowEnabled(_hwnd)) { throw new ElementNotEnabledException(); } IntPtr hwndCalendar; if (!WindowsDateTimePicker.IsCalendarPopupVisible(_hwnd, out hwndCalendar)) { Misc.PostMessage(_hwnd, NativeMethods.WM_KEYDOWN, (IntPtr)NativeMethods.VK_F4, IntPtr.Zero); Misc.PostMessage(_hwnd, NativeMethods.WM_KEYUP, (IntPtr)NativeMethods.VK_F4, IntPtr.Zero); // Wait for the window to come up; 10 tries for (int i = 0; i < 10 && !WindowsDateTimePicker.IsCalendarPopupVisible(_hwnd, out hwndCalendar); i++) { System.Threading.Thread.Sleep(0); } } else { // A post message must be used at the processing of the WM_KEYDOWN is achieve in a PeekMessageLoop Misc.PostMessage(hwndCalendar, NativeMethods.WM_KEYDOWN, (IntPtr)NativeMethods.VK_ESCAPE, new IntPtr(0x10001)); // Wait for the window to go away; 10 tries for (int i = 0; i < 10 && UnsafeNativeMethods.IsWindow(hwndCalendar); i++) { System.Threading.Thread.Sleep(0); } } }