开发者

C# Showing Form with WS_EX_NOACTIVATE flag

I have a borderless form which is always on top and with WS_EX_NOACTIVATE flag set to prevent it for gaining focus.

const int WS_EX_NOACTIVATE = 0x08000000;

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

Form contains small picture box for moving (开发者_StackOverflow中文版since it's borderless):

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

However when I move the window it doesn't get redrawn/shown, only when I release the mouse button does it move the form to new location.

I have seen applications which work in a similar fashion but they do show the window while moving (for example some virtual keyboards I've seen). I've also seen many questions elsewhere on net about this issue but with no answer.

Can someone please tell me if it is possible to show a window/form like this while moving (like "normal" window), and if yes, how to do it?


I think I've found solution. I would very appreciate if someone could check to see if everything is done correctly (no conflict of some kind with messages). I've changed the code above for moving form using picture, into the following:

[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);
    }
}

So basically I removed those two method (function) calls and repleaced them with SetWindowPos(). at first I had problems with flickering and incorrect positioning but then I remembered to check if coordinates are client or screen coordinates...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜