久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

通知窗口 - 防止窗口獲得焦點(diǎn)

Notification Window - Preventing the window from ever getting focus(通知窗口 - 防止窗口獲得焦點(diǎn))
本文介紹了通知窗口 - 防止窗口獲得焦點(diǎn)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我在讓通知框在 c# 中正確運(yùn)行時(shí)遇到了一些問(wèn)題.基本上我在屏幕的右下角顯示一個(gè)無(wú)邊框的表格,它顯示一條消息幾秒鐘然后消失.問(wèn)題是我需要它出現(xiàn)在其他窗口的頂部而它永遠(yuǎn)無(wú)法竊取焦點(diǎn).理想情況下,我希望它是純托管代碼,盡管查看類似的示例我懷疑這是可能的.

I'm having some issues getting a notification box to behave correctly in c#. Basically I'm showing a boarderless form in the lower right hand side of the screen, which displays a message for a few seconds and then disappears. The problem is that I need it to appear on top of other windows without it ever being able to steal focus. Ideally, I want it to be purely managed code, although looking through similar examples I doubt this will be possible.

目前我正在防止它在使用覆蓋調(diào)用 Form.Show() 時(shí)竊取焦點(diǎn):

At the moment I'm preventing it from stealing focus when calling Form.Show() with an override:

protected override bool ShowWithoutActivation // stops the window from stealing focus
{
    get { return true; }
}

然后忽略鼠標(biāo)點(diǎn)擊:

    private const int WM_MOUSEACTIVATE = 0x0021;
    private const int MA_NOACTIVATEANDEAT = 0x0004;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_MOUSEACTIVATE)
        {
            m.Result = (IntPtr)MA_NOACTIVATEANDEAT;
            return;
        }
        base.WndProc(ref m);
    }

但是我發(fā)現(xiàn)如果我將這些與 TopMost = true(我需要)結(jié)合使用,它無(wú)論如何都會(huì)獲得焦點(diǎn),如果所有其他窗口都最小化,它也會(huì)獲得焦點(diǎn).

However I find that if I use these in conjunction with TopMost = true (which I need), it gains focus anyway, and if all other windows are minimised, it also gains focus.

那么,有什么方法可以阻止表單獲得焦點(diǎn)(無(wú)論是通過(guò)鼠標(biāo)單擊、alt-tab 等),同時(shí)仍然是最重要/第二個(gè)最重要的表單?即使只是立即將焦點(diǎn)返回到它竊取它的窗口也會(huì)起作用(盡管會(huì)引入閃爍).

So, is there any way to flat out prevent a form from ever gaining focus (whether via mouse click, alt-tab, etc), while still being the top most/second top most form? Even just giving focus immediately back to the window it stole it from would work (although introduce flickering).

任何建議將不勝感激,我真的堅(jiān)持這一點(diǎn).

Any suggestions would be greatly appreciated, I'm really stuck with this.

好的,所以我終于設(shè)法使用:

Ok, so I finally managed to get this working using:

protected override bool ShowWithoutActivation // stops the window from stealing focus
{
    get { return true; }
}

// and

const int WS_EX_NOACTIVATE = 0x08000000;
const int WS_EX_TOPMOST = 0x00000008;

protected override CreateParams CreateParams
{
    get
    {
        CreateParams param = base.CreateParams;
        param.ExStyle |= WS_EX_TOPMOST; // make the form topmost
        param.ExStyle |= WS_EX_NOACTIVATE; // prevent the form from being activated
        return param;
    }
}

// and

[DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);
private const int WM_ACTIVATE = 6;
private const int WA_INACTIVE = 0;

private const int WM_MOUSEACTIVATE = 0x0021;
private const int MA_NOACTIVATEANDEAT = 0x0004;

protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_MOUSEACTIVATE)
    {
        m.Result = (IntPtr)MA_NOACTIVATEANDEAT; // prevent the form from being clicked and gaining focus
        return;
    }
    if (m.Msg == WM_ACTIVATE) // if a message gets through to activate the form somehow
    {
        if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
        {

            if (m.LParam != IntPtr.Zero)
            {
                SetActiveWindow(m.LParam);
            }
            else
            {
                // Could not find sender, just in-activate it.
                SetActiveWindow(IntPtr.Zero);
            }

        }
    }

我還在 GotFocus 事件中添加了 Form.Hide(),這樣即使它以某種方式獲得焦點(diǎn),它也會(huì)關(guān)閉并盡快離開(kāi)用戶.

I also added Form.Hide() to the GotFocus event so that even if it does somehow get focus, it simply closes and gets out of the users way asap.

此外,如果有人想知道,可以在 WINUSER.H 中找到所有窗口樣式等的常量,其在線地址為 http://www.woodmann.com/fravia/sources/WINUSER.H 如果找不到.

Also, if anyone is wondering, the constants for all the window styles etc. can be found in WINUSER.H, its online at http://www.woodmann.com/fravia/sources/WINUSER.H if you can't find it.

然而,如果有人能看到一種更優(yōu)雅的方式來(lái)做到這一點(diǎn),我們將不勝感激.

However, if anyone can see a more elegant way of doing this, it would be appreciated.

推薦答案

在 WPF 中試試這個(gè):

In WPF try this:

ShowActivated="False"

這篇關(guān)于通知窗口 - 防止窗口獲得焦點(diǎn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

LINQ to SQL and Concurrency Issues(LINQ to SQL 和并發(fā)問(wèn)題)
Yield return from a try/catch block(try/catch 塊的收益回報(bào))
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務(wù)的 SqlCommand 時(shí),我應(yīng)該調(diào)用 Parameters.Clear 嗎?)
what does a using statement without variable do when disposing?(處理時(shí)不帶變量的 using 語(yǔ)句有什么作用?)
Why doesn#39;t TransactionScope work with Entity Framework?(為什么 TransactionScope 不適用于實(shí)體框架?)
How to dispose TransactionScope in cancelable async/await?(如何在可取消的 async/await 中處理 TransactionScope?)
主站蜘蛛池模板: 国产一区二区三区久久 | 午夜视频网 | 中文字幕日韩视频 | 日本中文字幕视频 | av网站导航 | 一级特黄aaaaaa大片 | 久久机热这里只有精品 | 草久久| 九九热精品在线观看 | 亚洲欧美网站 | 黄色a毛片 | 免费成人结看片 | 亚洲欧美在线视频 | 国产精品久久久久久妇女6080 | 黄色片免费 | 亚洲福利视频一区 | 免费国产黄色 | 久久不射网| 亚洲一级大片 | 日本少妇一区二区 | 在线观看日本 | 免费视频一区二区 | 99热这里| av在线播放免费 | 天天干网| 91精品国产99久久久久久红楼 | 日韩欧美国产精品 | 日韩精品在线视频 | 日本精品免费 | 欧美日韩免费视频 | 国产精品三 | 91在线看片 | 91亚色视频 | 在线免费看黄色 | 亚洲伊人av | 毛片网站免费 | 国产午夜精品视频 | 久久久国产精品人人片 | 91久久综合亚洲鲁鲁五月天 | 国产成人免费在线观看 | 欧美aaaaa|