2012-07-03 38 views
34

tôi có một ứng dụng cửa sổ. khi tôi thu nhỏ ứng dụng cửa sổ trên thanh tác vụ để làm việc trên một ứng dụng khác. chúng tôi hava một cơ sở như gửi tin nhắn từ một ứng dụng cửa sổ đến một ứng dụng cửa sổ khácỨng dụng cửa sổ flash như màu cam trên thanh tác vụ khi thu nhỏ

vì vậy ứng dụng giành chiến thắng đầu tiên của tôi được giảm thiểu và bây giờ tôi mở ứng dụng khác và gửi tin nhắn đến ứng dụng đầu tiên nhưng ứng dụng đầu tiên là trên thanh tác vụ. vì vậy tôi muốn có chức năng như khi bất kỳ tin nhắn nào chụp được ứng dụng đầu tiên của tôi thì nó sẽ nhấp nháy như skype hoặc bất kỳ tin nhắn nào.

tôi đã thử phương pháp "FlashWindowEx" của User32.dll. nhưng không may mắn. tôi đã thử với tùy chọn "Flash liên tục cho đến khi cửa sổ đến nền trước." nhưng không may mắn.

hãy giúp đỡ để giải quyết vấn đề này với ví dụ

Cảm ơn

+0

Check-out http://stackoverflow.com/questions/ 8924556/force-window-to-blink-khi-a-cụ-sự kiện-xảy ra-in-c-sharp-wpf/8929473 # 8929473 có câu hỏi & Câu hỏi –

Trả lời

22

C#: Flash Window in Taskbar via Win32 FlashWindowEx nó làm việc cho tôi.

API Windows (Win32) có phương pháp FlashWindowEx trong thư viện User32 ; phương pháp này cho phép bạn (nhà phát triển) để Flash một cửa sổ, biểu thị cho người dùng rằng một số sự kiện lớn xảy ra trong ứng dụng yêu cầu sự chú ý của họ. Việc sử dụng phổ biến nhất của này là nhấp nháy cửa sổ cho đến khi người dùng trả về tiêu điểm cho ứng dụng . Tuy nhiên, bạn cũng có thể nhấp nháy cửa sổ một số được chỉ định lần hoặc chỉ tiếp tục nhấp nháy cho đến khi bạn quyết định thời điểm dừng.

Việc sử dụng phương pháp FlashWindowEx tuy nhiên không được tích hợp vào .NET Framework ở mọi nơi. Để truy cập nó, bạn cần sử dụng các tính năng Platform Gọi (PInvoke) của .NET để "thả" xuống Windows API (Win32) và gọi trực tiếp. Ngoài ra, cũng như nhiều chức năng khác trong API Windows (không được hiển thị trực tiếp bởi .NET), phương pháp FlashWindowEx có thể hơi khó sử dụng nếu bạn không phải là quen thuộc khi làm việc với API Windows từ bên trong. MẠNG LƯỚI.

Bây giờ thay vì đi quá sâu vào chi tiết cụ thể của phương pháp PInvoke hoặc Win32 FlashWindowEx, dưới đây là một lớp tĩnh đơn giản trong C# cho phép bạn dễ dàng sử dụng phương pháp này. Có thực sự khá một chút thông tin cần thiết để giải thích cách sử dụng PInvoke để sử dụng API cửa sổ Windows (Win32), vì vậy có lẽ tôi sẽ đề cập đến điều đó trong một bài viết trong tương lai.

Dưới đây là một số cách sử dụng ví dụ về lớp tĩnh này:

// One this to note with this example usage code, is the "this" keyword is referring to 
// the current System.Windows.Forms.Form. 

// Flash window until it recieves focus 
FlashWindow.Flash(this); 

// Flash window 5 times 
FlashWindow.Flash(this, 5); 

// Start Flashing "Indefinately" 
FlashWindow.Start(this); 

// Stop the "Indefinate" Flashing 
FlashWindow.Stop(this); 

Một điều cần lưu ý về phương pháp FlashWindowEx là nó đòi hỏi (và sẽ chỉ làm việc trên) Windows 2000 hoặc muộn hơn.

Dưới đây là đoạn code cho lớp tĩnh trong C#:

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

    [StructLayout(LayoutKind.Sequential)] 
    private 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 uint dwFlags; 
     /// <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; 
    } 

    /// <summary> 
    /// Stop flashing. The system restores the window to its original stae. 
    /// </summary> 
    public const uint FLASHW_STOP = 0; 

    /// <summary> 
    /// Flash the window caption. 
    /// </summary> 
    public const uint FLASHW_CAPTION = 1; 

    /// <summary> 
    /// Flash the taskbar button. 
    /// </summary> 
    public const uint FLASHW_TRAY = 2; 

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

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

    /// <summary> 
    /// Flash continuously until the window comes to the foreground. 
    /// </summary> 
    public const uint FLASHW_TIMERNOFG = 12; 


    /// <summary> 
    /// Flash the spacified Window (Form) until it recieves focus. 
    /// </summary> 
    /// <param name="form">The Form (Window) to Flash.</param> 
    /// <returns></returns> 
    public static bool Flash(System.Windows.Forms.Form form) 
    { 
     // Make sure we're running under Windows 2000 or later 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 

    private static FLASHWINFO Create_FLASHWINFO(IntPtr handle, uint flags, uint count, uint timeout) 
    { 
     FLASHWINFO fi = new FLASHWINFO(); 
     fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi)); 
     fi.hwnd = handle; 
     fi.dwFlags = flags; 
     fi.uCount = count; 
     fi.dwTimeout = timeout; 
     return fi; 
    } 

    /// <summary> 
    /// Flash the specified Window (form) for the specified number of times 
    /// </summary> 
    /// <param name="form">The Form (Window) to Flash.</param> 
    /// <param name="count">The number of times to Flash.</param> 
    /// <returns></returns> 
    public static bool Flash(System.Windows.Forms.Form form, uint count) 
    { 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL, count, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 

    /// <summary> 
    /// Start Flashing the specified Window (form) 
    /// </summary> 
    /// <param name="form">The Form (Window) to Flash.</param> 
    /// <returns></returns> 
    public static bool Start(System.Windows.Forms.Form form) 
    { 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_ALL, uint.MaxValue, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 

    /// <summary> 
    /// Stop Flashing the specified Window (form) 
    /// </summary> 
    /// <param name="form"></param> 
    /// <returns></returns> 
    public static bool Stop(System.Windows.Forms.Form form) 
    { 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = Create_FLASHWINFO(form.Handle, FLASHW_STOP, uint.MaxValue, 0); 
      return FlashWindowEx(ref fi); 
     } 
     return false; 
    } 

    /// <summary> 
    /// A boolean value indicating whether the application is running on Windows 2000 or later. 
    /// </summary> 
    private static bool Win2000OrLater 
    { 
     get { return System.Environment.OSVersion.Version.Major >= 5; } 
    } 
} 
+0

Tôi tin rằng nó sẽ còn lớn hơn nếu được sử dụng làm tiện ích mở rộng phương pháp để hình thành lớp :) – Jack

26

tôi làm điều đó như hình dưới đây, là chắc chắn để thêm các tài liệu tham khảo cần thiết như

using System.Runtime.InteropServices; 
using Microsoft.Win32; 

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

//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 window comes to the foreground. 
public const UInt32 FLASHW_TIMERNOFG = 12; 

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

// Do the flashing - this does not involve a raincoat. 
public static bool FlashWindowEx(Form form) 
{ 
    IntPtr hWnd = form.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); 
} 

này nên chứa tất cả mọi thứ bạn cần.

Tôi hy vọng điều này sẽ hữu ích.

+2

tôi đã thử cách đó nhưng nó không hoạt động, tôi có cửa sổ 7 os. – Hardik

+0

Thao tác này cho Windows 7. Bạn sẽ cần phải thêm một số tham chiếu. Xem chỉnh sửa ... – MoonKnight

+0

tôi đã thử cách đó nhưng nó không hoạt động, tôi đã sử dụng hệ điều hành windows 7. đèn flash màu da cam được hiển thị nhưng có hộp thông báo bên trong ứng dụng. – Hardik

1

Các bạn tôi đã tìm thấy một cách dễ dàng hơn để làm điều đó! Nếu đây là trường hợp bạn đang sử dụng nó cho.

Trong .NET 4.0, C#, nếu bạn chỉ cần sử dụng

this.WindowState = FormWindowState.Normal; 
this.Activate(); 

Dòng đầu tiên làm cho chắc chắn nó không phải hạn chế tối đa, dòng thứ hai làm cho nó tập trung. Tôi không chắc chắn lý do tại sao (hoặc một trong số họ không làm điều này), nhưng kết hợp, nếu bạn hiển thị một MessageBox từ hình thức này, chương trình của bạn nhấp nháy màu cam trên thanh tác vụ!

+6

thay đổi WindowState và Activate() không có gì để làm với nhấp nháy thanh tác vụ. –

+1

Điều này không nhấp nháy trên Thanh tác vụ, nhưng cửa sổ bật lên trở lại trên đầu tất cả các cửa sổ đang mở khác. không giống như nhấp nháy, nhưng thay thế khác. – JOW

1

Tôi biết câu hỏi này khá cũ, nhưng dựa trên câu trả lời MoonKnight tôi đã cải thiện, một số có thể hữu ích. Tôi đã chuyển đổi nó thành tiện ích mở rộng Biểu mẫu.

public static class ExtensionMethods 
{ 
    // To support flashing. 
    [DllImport("user32.dll", CallingConvention = CallingConvention.Cdecl)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool FlashWindowEx(ref FLASHWINFO pwfi); 

    //Flash both the window caption and taskbar button. 
    //This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. 
    private const uint FLASHW_ALL = 3; 

    // Flash continuously until the window comes to the foreground. 
    private const uint FLASHW_TIMERNOFG = 12; 

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

    /// <summary> 
    /// Send form taskbar notification, the Window will flash until get's focus 
    /// <remarks> 
    /// This method allows to Flash a Window, signifying to the user that some major event occurred within the application that requires their attention. 
    /// </remarks> 
    /// </summary> 
    /// <param name="form"></param> 
    /// <returns></returns> 
    public static bool FlashNotification(this Form form) 
    { 
     IntPtr hWnd = form.Handle; 
     FLASHWINFO fInfo = new FLASHWINFO(); 

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

     return FlashWindowEx(ref fInfo); 
    } 
} 

Để sử dụng nó trên một hình thức bạn chỉ cần gọi

this.FlashNotification(); 

Để thay đổi cách thức mà nó nhấp nháy chỉ cần nhìn vào this table

Các vấn đề liên quan