Используя код, который я нашел на CodeProject, я создал заставку. Следующая форма — это крошечная форма, которую я показываю в Панели управления, когда пользователь выбирает мою заставку.
Кажется, все работает нормально; форма правильно изменяет размеры и рисуется (пустая) в нужном месте панели управления, перемещается вместе с CP и т. д. Но когда панель управления закрывается (или заменяет мою форму мини-предварительным просмотром другой заставки), мое приложение не умри. Просто врезается в память.
Моя форма не получает сообщений о закрытии/закрытии формы, изменениях видимости и т. д. Я неправильно устанавливаю родительские права здесь?
Вот соответствующий код. Все импортированные вызовы WinAPI возвращают ожидаемые значения, а GetLastError всегда возвращает ноль, так что думаю проблема не в этом...
private void miniControlPanelForm_Load(object sender, EventArgs e)
{
// note that iphWnd is a class variable, passed to us by windows
// set our window style to WS_CHILD, so that our window is
// destroyed when parent window is destroyed.
// get the current window style, but with WS_CHILD set
IntPtr ip = new IntPtr();
int index = (int)NativeMethods.WindowLongFlags.GWL_STYLE | 0x40000000;
ip = NativeMethods.GetWindowLongPtr(this.Handle, index);
int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
// set that value as our current Style
object ohRef = new object();
HandleRef hRef = new HandleRef(ohRef, this.Handle);
IntPtr ip2 = new IntPtr();
int index2 = (int)NativeMethods.WindowLongFlags.GWL_STYLE;
ip2 = NativeMethods.SetWindowLongPtr(hRef, index2, ip);
error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
// set the passed preview window as the parent of this window
IntPtr newOldParent = NativeMethods.SetParent(this.Handle, iphWnd);
error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
//set our window's size to the size of our window's new parent
Rectangle ParentRect = new Rectangle();
NativeMethods.GetClientRect(iphWnd, ref ParentRect);
this.Size = ParentRect.Size;
//set our location at (0, 0)
this.Location = new Point(0, 0);
}
У меня есть Application.Exit в различных обработчиках событий «форма закрывается», но они никогда не вызываются...