下面的代码截图取自"WPF 4未释放“。它演示了在Windows7中,可以使用WIN32 API创建空中玻璃效果。在本演示中,针对窗口实例使用了WndProc events过程。我注意到,在此例程中没有调用默认窗口过程,就好像该WPF窗口没有需要处理的其他事件一样。
我之所以提出这个问题--这是一个关于WPF的更一般的问题--是不是通常由WPF窗口处理的事件(我相信有很多)是由其他过程处理的。换句话说,WPF window与WinForms有什么不同-它是否通过其他方式从操作系统获取消息(鼠标单击、鼠标移动)?
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public MARGINS(Thickness t)
{
Left = (int)t.Left;
Right = (int)t.Right;
Top = (int)t.Top;
Bottom = (int)t.Bottom;
}
public int Left;
public int Right;
public int Top;
public int Bottom;
}
public class GlassHelper
{
[DllImport("dwmapi.dll", PreserveSig=false)]
static extern void DwmExtendFrameIntoClientArea( IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll", PreserveSig=false)]
static extern bool DwmIsCompositionEnabled();
public static bool ExtendGlassFrame(Window window, Thickness margin)
{
if (!DwmIsCompositionEnabled())
return false;
IntPtr hwnd = new WindowInteropHelper(window).Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException(
"The Window must be shown before extending glass.");
// Set the background to transparent from both the WPF and Win32 perspectives
window.Background = Brushes.Transparent;
HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor =Colors.Transparent;
MARGINS margins = new MARGINS(margin);
DwmExtendFrameIntoClientArea(hwnd, ref margins);
return true;
}
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// This can’t be done any earlier than the SourceInitialized event:
GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
// Attach a window procedure in order to detect later enabling of desktop
// composition
IntPtr hwnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_DWMCOMPOSITIONCHANGED)
{
// Reenable glass:
GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
handled = true;
}
return IntPtr.Zero;
}
private const int WM_DWMCOMPOSITIONCHANGED = 0x031E;发布于 2014-04-18 17:39:13
在使用WndProc方面,WPF窗口与WinForms窗口是相同的。我可以毫不费力地将代码片段放入我的WPF应用程序中。事实上,我没有发现任何与WndProc相关的代码,到目前为止在WPF中不起作用。
发布于 2018-02-12 19:30:48
WPF窗口类似于windows Forms窗口,也类似于经典的Windows窗口,因为它们所有的都有一个用于接收消息的Message Loop和一个用于处理消息的WindowProc (实际名称可以是程序员选择的任何名称)。所有的窗口都可以子类化(就像在About Window Procedures中一样,至少在WPF以下的级别)。我不知道WndProc for Windows Forms或者for WPF是否是相关窗口的子类,但它们可能是子类。
https://stackoverflow.com/questions/23141571
复制相似问题