2010-02-08 50 views
1

好了,所以谷歌是不是我的朋友今晚...如何知道何时在显示属性对话框中选择了不同的屏幕保护程序?

我有一个屏幕保护程序,CC.Votd (Full source on Codeplex),和我刚开始实施预览模式(/ P参数),这是工作确定。当它处于预览模式时,我的表单成为小电脑显示器窗口的一个孩子,并且在那里绘制。

这工作得很好,我的应用程序退出时,如果显示属性对话框消失。

的问题是,如果我从列表中选择我的屏幕保护程序,然后选择不同的屏幕保护程序矿继续运行,并绘制了新选择屏幕保护程序的预览。

那我怎么知道什么时候选择不同的屏幕保护程序和矿应该关闭?


编辑:对于匿名,这里是我使用,使我的形式预览窗口的子代码:

P /调用:

[DllImport("user32.dll")] 
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

[DllImport("user32.dll")] 
static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); 

[DllImport("user32.dll", SetLastError = true)] 
static extern int GetWindowLong(IntPtr hWnd, int nIndex); 

[DllImport("user32.dll")] 
static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect); 

的代码:

SetParent(Handle, _PreviewHandle); 
SetWindowLong(Handle, -16, new IntPtr(GetWindowLong(Handle, -16) | 0x40000000)); 

Rectangle parentRectangle; 
GetClientRect(_PreviewHandle, out parentRectangle); 
Size = parentRectangle.Size; 

Location = new Point(0, 0); 

填写表单代码:http://ccvotd.codeplex.com/SourceControl/changeset/view/40085#862458


忘了提,我尝试使用IsWindowVisible()并没有奏效,因为预览窗口仍然可见并且具有相同的手柄时选择了我的屏幕保护程序的。

编辑:我加入了SetParent()和相关的调用之前我的应用程序将继续运行显示对话框关闭后,所以我认为这部分工作,当用户选择不同的屏幕保护程序不同的东西发生。


正如John K建议我一直在用Spy ++来查看我的表格。我从来没有看到应用WS_CHILD样式。不过,我所有的调试都表明它应该是。我修改了代码:

long style = GetWindowLong(Handle, -16); 
System.Diagnostics.Trace.WriteLine("Original Style: " + style); 
style &= ~0x800000000; 
style |= 0x40000000; 
System.Diagnostics.Trace.WriteLine("Adjusted Style: " + style); 

SetWindowLong(Handle, -16, new IntPtr(style)); 
System.Diagnostics.Trace.WriteLine("After Set Style: " + GetWindowLong(Handle, -16)); 
SetParent(Handle, _PreviewHandle); 
System.Diagnostics.Trace.WriteLine("After Set Parent: " + GetWindowLong(Handle, -16)); 

而且款式是在最后三个痕迹,其中两个应该从窗体本身来获取值相同。将研究我的本地API调用并清理它们的声明以了解我能弄清楚的内容。

感谢所有帮助迄今为止!


解决方案:问题结束了,我被设置,导致潜在的.NET控件覆盖我的新的风格形式的几个属性。所以改变:

SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); 

Capture = true; 

if (!_IsPreview) 
{ 
    // Removed ... 
} 
else 
{ 
    SetWindowLong(Handle, -16, new IntPtr(GetWindowLong(Handle, -16) | 0x40000000)); 
    SetParent(Handle, _PreviewHandle); 

    Rectangle parentRectangle; 
    GetClientRect(_PreviewHandle, out parentRectangle); 
    Size = parentRectangle.Size; 

    Location = new Point(0, 0); 
} 

ShowInTaskbar = false; 
DoubleBuffered = true; 
BackgroundImageLayout = ImageLayout.Stretch; 

要:

SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); 

BackgroundImageLayout = ImageLayout.Stretch; 
Capture = true; 
DoubleBuffered = true; 
ShowInTaskbar = false; 

if (!_IsPreview) 
{ 
    // Removed ... 
} 
else 
{ 
    SetWindowLong(Handle, -16, new IntPtr(GetWindowLong(Handle, -16) | 0x40000000)); 
    SetParent(Handle, _PreviewHandle); 

    Rectangle parentRectangle; 
    GetClientRect(_PreviewHandle, out parentRectangle); 
    Size = parentRectangle.Size; 

    Location = new Point(0, 0); 
} 

解决了这一问题。简单的错误:-)


正确的方法来解决它......覆盖的CreateParams:

protected override CreateParams CreateParams 
{ 
    get 
    { 
     CreateParams createParams = base.CreateParams; 

     if (!DesignMode && _IsPreview) 
     { 
      createParams.Style |= 0x40000000; 
     } 

     return createParams; 
    } 
} 
+2

您可以发布您用于创建预览窗口的代码作为传入HWND的子代吗? – 2010-02-08 02:14:50

+0

@Anon:当然,我从一个例子中发现它,并没有真正检查过它似乎为我工作的事实:-) – 2010-02-08 02:21:35

+1

屏幕保护程序对话会在用户切换屏幕保护程序时破坏预览窗口 - 您是否听这个? – 2010-02-08 02:35:51

回答

1

一旦APON时间试图改变一个窗口WS_CHILD样式它已经创建之后会只是悄然失败。我认为他们在当前版本的Windows中改变了这一点,但可以肯定的是,您应该从一开始就将您的预览窗体创建为子窗口。

我有一个预感,你的窗口没有结束预览的子窗口。 你可以试试这个。

SetParent(Handle, _PreviewHandle); 
SetWindowLong(Handle, -16, new IntPtr(GetWindowLong(Handle, -16) | 0x40000000)); 
SetParent(Handle, _PreviewHandle); 

的setparent 当你改变了你的窗口风格WS_CHILD。

另外,您的窗体上可能没有WS_POPUP样式,但是如果您这样做,则需要将其删除。

int style = GetWindowLong(Handle, -16); 
style &= ~0x800000000; 
style |= 0x40000000; 
SetWindowLong(Handle, -16, new IntPtr(style)); 

这里发生的事情是,设置的setParent子窗口的父,但它设置的WS_POPUP和WS_OVERLAPPED窗口所有者

+0

@John:再次感谢您指点我正确的方向。 – 2010-02-23 23:50:42

相关问题