我目前正在开发一个源自标准datetimepicker控件的自定义datetimepicker控件。
问题是我在下拉时无法更改monthcalendar弹出窗口的位置。
这是我的代码,它将monthcalendar弹出控件向左移动50px,它适用于控件,但不适用于它下面的弹出窗口:
private const int DTM_FIRST = 0x1000;
private const int DTM_GETMONTHCAL = (DTM_FIRST + 8);
private const uint SWP_NOSIZE = 0x1;
[DllImport("User32.dll", SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr h, int msg, int param, int data);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
protected override void OnDropDown(EventArgs eventargs)
{
IntPtr monthCalHandle = SendMessage(this.Handle, DTM_GETMONTHCAL, 0, 0);
if (monthCalHandle == IntPtr.Zero)
{
throw new Exception("Cannot get calendar control");
}
bool success = SetWindowPos(monthCalHandle, IntPtr.Zero,
-50, 0, 0, 0, SWP_NOSIZE);
base.OnDropDown(eventargs);
}
以上是从上面的代码看起来的样子。正如您所看到的那样,控件向左移动了50px,但没有向下移动窗口:
所以我认为我应该抓住monthcalendar控件的父弹出窗口并设置它的位置,这就是我所做的:
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);
protected override void OnDropDown(EventArgs eventargs)
{
IntPtr monthCalHandle = SendMessage(this.Handle, DTM_GETMONTHCAL, 0, 0);
if (monthCalHandle == IntPtr.Zero)
{
throw new Exception("Cannot get calendar control");
}
IntPtr parentHandle = GetParent(monthCalHandle);
if (parentHandle == IntPtr.Zero)
{
throw new Exception("Cannot get calendar parent popup");
}
bool success = SetWindowPos(parentHandle, IntPtr.Zero,
-50, 0, 0, 0, SWP_NOSIZE);
base.OnDropDown(eventargs);
}
我所获得的一切都没有改变:
顺便说一下
运行应用程序时没有抛出任何异常。我正在使用VS 2015社区,该项目的目标是.NET v4.0。
感谢您的善意。
提前致谢。