相关疑难解决方法(0)

如何通过拖动扩展窗口框架使WPF窗口可移动?

在Windows资源管理器和Internet Explorer等应用程序中,可以抓住标题栏下方的扩展框架区域并拖动窗口.

对于WinForms应用程序,表单和控件尽可能接近原生Win32 API; 一个人只是简单地覆盖WndProc()其表单中的处理程序,处理WM_NCHITTEST窗口消息并欺骗系统,使其认为点击框架区域实际上是通过返回点击标题栏HTCAPTION.我已经在我自己的WinForms应用程序中做到了令人愉快的效果.

在WPF中,我还可以实现一个类似的WndProc()方法并将其挂钩到我的WPF窗口的句柄,同时将窗口框架扩展到客户端区域,如下所示:

// In MainWindow
// For use with window frame extensions
private IntPtr hwnd;
private HwndSource hsource;

private void Window_SourceInitialized(object sender, EventArgs e)
{
    try
    {
        if ((hwnd = new WindowInteropHelper(this).Handle) == IntPtr.Zero)
        {
            throw new InvalidOperationException("Could not get window handle for the main window.");
        }

        hsource = HwndSource.FromHwnd(hwnd);
        hsource.AddHook(WndProc);

        AdjustWindowFrame();
    }
    catch (InvalidOperationException)
    {
        FallbackPaint();
    }
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr …
Run Code Online (Sandbox Code Playgroud)

.net c# wpf winapi windows-7

52
推荐指数
3
解决办法
1万
查看次数

标签 统计

.net ×1

c# ×1

winapi ×1

windows-7 ×1

wpf ×1