获取用户的关注,而不偷焦点 [英] Get user attention without stealing focus

查看:108
本文介绍了获取用户的关注,而不偷焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我有一个程序,让用户打开几种形式。一旦给定eventoccurs(例如:30秒过去了),我需要获得用户关注的触发事件的形式,而不偷焦点。
我已经顶得上的形式


I have a program that let the user open several forms. Once a given eventoccurs ( ex : 30 seconds have passed ) I need to get user attention on the Form that triggered the event, without stealing the focus. I already get the form on top with

f.TopMost = true;



但我想实现一些替代了这一点。由于改变框的边框颜色似乎是一个几乎不可能完成的任务(该解决方案将是最好的),有人对如何获得关注,而不偷焦点任何想法?

but I'd like to implement some alternative to that. Since changing the border color of the frame seems a nearly impossible task ( this solution would have been the best one), somebody has any idea on how to get attention without stealing focus?

推荐答案

选项A:您需要使用FlashWindowEx从Windows API。这不是在.NET中,所以你需要使用的PInvoke

Option A: You need to use FlashWindowEx from the windows API. This isn't available in .NET, so you need to use PInvoke.

选项B:从系统托盘使用气球提示。这是内置于.NET,但要求你的应用程序中使用通知图标,您可能不希望。更多详细的活动: http://msdn.microsoft.com/en-美国/库/ system.windows.forms.notifyicon.showballoontip.aspx

Option B: Use a balloon tip from the system tray. This is built into .NET, but requires that your application use a notification icon, which you might not want. More details here: http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.showballoontip.aspx

下面是如何使用选项A的例子:

Here is the example for how to use Option A:

pInvoke.net拥有最好的例子: http://pinvoke.net/default。 ASPX / user32.FlashWindowEx

pInvoke.net has the best example: http://pinvoke.net/default.aspx/user32.FlashWindowEx

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FlashWindowEx(ref FLASHWINFO pwfi);



用户定义类型:

User-Defined Types:

[StructLayout(LayoutKind.Sequential)]
public struct FLASHWINFO
{
    public UInt32 cbSize;
    public IntPtr hwnd;
    public UInt32 dwFlags;
    public UInt32 uCount;
    public UInt32 dwTimeout;
}



注:

Notes:

//Stop flashing. The system restores the window to its original state. 
public const UInt32 FLASHW_STOP = 0; 
//Flash the window caption. 
public const UInt32 FLASHW_CAPTION = 1; 
//Flash the taskbar button. 
public const UInt32 FLASHW_TRAY = 2; 
//Flash both the window caption and taskbar button.
//This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. 
public const UInt32 FLASHW_ALL = 3; 
//Flash continuously, until the FLASHW_STOP flag is set. 
public const UInt32 FLASHW_TIMER = 4; 
//Flash continuously until the window comes to the foreground. 
public const UInt32 FLASHW_TIMERNOFG = 12;



提示和放大器;技巧:

Tips & Tricks:

请加些

示例代码:

/// <summary>
/// Flashes a window
/// </summary>
/// <param name="hWnd">The handle to the window to flash</param>
/// <returns>whether or not the window needed flashing</returns>
public static bool FlashWindowEx(IntPtr hWnd)
{
    FLASHWINFO fInfo = new FLASHWINFO();

    fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
    fInfo.hwnd = hWnd;
    fInfo.dwFlags = FLASHW_ALL;
    fInfo.uCount = UInt32.MaxValue;
    fInfo.dwTimeout = 0;

    return FlashWindowEx(ref fInfo);
}



...

...

/// Minor adjust to the code above
/// <summary>
/// Flashes a window until the window comes to the foreground
/// Receives the form that will flash
/// </summary>
/// <param name="hWnd">The handle to the window to flash</param>
/// <returns>whether or not the window needed flashing</returns>
public static bool FlashWindowEx(Form frm)
{
        IntPtr hWnd = frm.Handle;
        FLASHWINFO fInfo = new FLASHWINFO();

        fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
        fInfo.hwnd = hWnd;
        fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;
        fInfo.uCount = UInt32.MaxValue;
        fInfo.dwTimeout = 0;

        return FlashWindowEx(ref fInfo);
}






下面是微软官方例如: http://msdn.microsoft.com/en-us/library /ms679347(v=vs.85).aspx

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

    [StructLayout(LayoutKind.Sequential)]
    public struct FLASHWINFO
    {
        /// <summary>
        /// The size of the structure in bytes.
        /// </summary>
        public uint cbSize;
        /// <summary>
        /// A Handle to the Window to be Flashed. The window can be either opened or minimized.
        /// </summary>
        public IntPtr hwnd;
        /// <summary>
        /// The Flash Status.
        /// </summary>
        public FlashWindowFlags dwFlags; //uint
        /// <summary>
        /// The number of times to Flash the window.
        /// </summary>
        public uint uCount;
        /// <summary>
        /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
        /// </summary>
        public uint dwTimeout;
    }


    public enum FlashWindowFlags : uint
    {
        /// <summary>
        /// Stop flashing. The system restores the window to its original state.
        /// </summary>
        FLASHW_STOP = 0,

        /// <summary>
        /// Flash the window caption.
        /// </summary>
        FLASHW_CAPTION = 1,

        /// <summary>
        /// Flash the taskbar button.
        /// </summary>
        FLASHW_TRAY = 2,

        /// <summary>
        /// Flash both the window caption and taskbar button.
        /// This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags.
        /// </summary>
        FLASHW_ALL = 3,

        /// <summary>
        /// Flash continuously, until the FLASHW_STOP flag is set.
        /// </summary>
        FLASHW_TIMER = 4,

        /// <summary>
        /// Flash continuously until the window comes to the foreground.
        /// </summary>
        FLASHW_TIMERNOFG = 12
    }


    public static bool FlashWindow(IntPtr hWnd, 
                                   FlashWindowFlags fOptions, 
                                   uint FlashCount, 
                                   uint FlashRate)
    {
        if(IntPtr.Zero != hWnd)
        {
            FLASHWINFO fi = new FLASHWINFO();
            fi.cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO));
            fi.dwFlags = fOptions;
            fi.uCount = FlashCount;
            fi.dwTimeout = FlashRate;
            fi.hwnd = hWnd;

            return FlashWindowEx(ref fi);
        }
        return false;
    }

    public static bool StopFlashingWindow(IntPtr hWnd)
    {
        if(IntPtr.Zero != hWnd)
        {
            FLASHWINFO fi = new FLASHWINFO();
            fi.cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO));
            fi.dwFlags = (uint)FlashWindowFlags.FLASHW_STOP;
            fi.hwnd = hWnd;

            return FlashWindowEx(ref fi);
        }
        return false;
    }

这篇关于获取用户的关注,而不偷焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆