c# – 无需窃取焦点即可获得用户关注

c# – 无需窃取焦点即可获得用户关注,第1张

概述我有一个程序让用户打开几个表单.一旦给定的事件发生(例如:30秒过去),我需要在触发事件的窗体上获得用户注意,而不会窃取焦点. 我已经得到了顶部的形式 f.TopMost = true; 但我想实现一些替代方案.由于改变框架的边框颜色似乎几乎不可能完成任务(这个解决方案本来是最好的),有人有什么想法如何在不偷眼的情况下得到注意? 选项A:您需要从Windows API使用FlashWindowEx 我有一个程序让用户打开几个表单.一旦给定的事件发生(例如:30秒过去),我需要在触发事件的窗体上获得用户注意,而不会窃取焦点.
我已经得到了顶部的形式
f.topMost = true;

但我想实现一些替代方案.由于改变框架的边框颜色似乎几乎不可能完成任务(这个解决方案本来是最好的),有人有什么想法如何在不偷眼的情况下得到注意?

解决方法 选项A:您需要从windows API使用FlashWindowEx.这在.NET中不可用,因此您需要使用PInvoke.

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

以下是如何使用选项A的示例:

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

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

用户自定义类型:

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

笔记:

//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;

提示&技巧:

请加一些!

示例代码:

/// <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;    }
总结

以上是内存溢出为你收集整理的c# – 无需窃取焦点即可获得用户关注全部内容,希望文章能够帮你解决c# – 无需窃取焦点即可获得用户关注所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1261945.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存