C#使用WS_EX_NOACTIVATE标志显示表单

时间:2010-06-03 20:52:32

标签: c# winforms

我有一个无边框表单,它始终位于顶部,并设置WS_EX_NOACTIVATE标志以防止它获得焦点。

const int WS_EX_NOACTIVATE = 0x08000000;

protected override CreateParams CreateParams {
    get {
        CreateParams param = base.CreateParams;
        param.ExStyle |= WS_EX_NOACTIVATE;
        return param;
    }
}

表单包含用于移动的小图片框(因为它没有边框):

private void pictureBox4_MouseMove(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
        ReleaseCapture();
        SendMessage(this.Handle, 0xa1, 0x2, 0);
    }
}

然而,当我移动窗口时,它不会被重绘/显示,只有当我释放鼠标按钮时它才会将窗体移动到新位置。

我看过应用程序以类似的方式工作,但它们确实在移动时显示窗口(例如我见过的一些虚拟键盘)。我在网上其他地方也看到过很多关于这个问题的问题,但没有答案。

有人可以告诉我是否可以在移动时显示这样的窗口/窗体(如“普通”窗口),如果是,该怎么做?

1 个答案:

答案 0 :(得分:2)

我想我找到了解决方案。如果有人能查看是否所有内容都正确完成(没有任何与邮件冲突),我将非常感激。我已将上面的代码更改为使用图片移动表单,如下所示:

[DllImportAttribute("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInstertAfter, int x, int y, int cx, int cy, uint flags);

const int SWP_NOSIZE = 0x0001;
const int SWP_NOZORDER = 0x0004;

private void pictureBox4_MouseMove(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
        //ReleaseCapture();
        //SendMessage(this.Handle, 0xa1, 0x2, 0);
        SetWindowPos(Handle, IntPtr.Zero, this.Location.X + e.X,
                this.Location.Y + e.Y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }
}

所以基本上我删除了这两个方法(函数)调用并用SetWindowPos()重新调用它们。起初我有闪烁和错误定位的问题,但后来我记得检查坐标是客户端还是屏幕坐标......