2011-02-03 34 views
9


Tôi có một chương trình cho phép người dùng mở nhiều biểu mẫu. Sau khi một sự kiện đã cho (ví dụ: 30 giây đã trôi qua), tôi cần thu hút sự chú ý của người dùng trên Biểu mẫu đã kích hoạt sự kiện, mà không lấy cắp tiêu điểm. tôi đã lấy mẫu đơn trên đầu trang vớiThu hút sự chú ý của người dùng mà không bị lấy cắp tiêu điểm

f.TopMost = true; 

nhưng tôi muốn thực hiện một số thay thế cho rằng. Vì việc thay đổi màu viền của khung hình dường như là một nhiệm vụ gần như không thể (giải pháp này có thể là giải pháp tốt nhất), ai đó có ý tưởng về cách thu hút sự chú ý mà không lấy cắp tiêu điểm?

Trả lời

19

Tùy chọn A: Bạn cần sử dụng FlashWindowEx từ API cửa sổ. Điều này không có sẵn trong .NET, vì vậy bạn cần sử dụng PInvoke.

Tùy chọn B: Sử dụng đầu bóng từ khay hệ thống. Điều này được tích hợp vào .NET, nhưng yêu cầu ứng dụng của bạn sử dụng biểu tượng thông báo mà bạn có thể không muốn. Xem thêm chi tiết ở đây: http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.showballoontip.aspx

Dưới đây là ví dụ về cách sử dụng Lựa chọn A:

pInvoke.net có ví dụ tốt nhất: http://pinvoke.net/default.aspx/user32.FlashWindowEx

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

User-Defined loại:

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

Ghi chú:

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

Mẹo & Thủ thuật:

Vui lòng thêm một số!

Sample Code:

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

Đây là quan chức Microsoft dụ: 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; 
    } 
+0

Gọi điện tốt bằng cách sử dụng 'NotifyIcon'! –

+0

Quan trọng: Trên Windows 7, một số người dùng đã báo cáo điều này là Không hoạt động. Sau một số nghiên cứu, tôi đã tìm thấy lý do là nếu ứng dụng của bạn có nhiều cửa sổ và nếu bất kỳ cửa sổ nào đang hoạt động trong khi bạn gọi API này để flash một cửa sổ khác, nó sẽ không hoạt động. Nó sẽ chỉ hoạt động nếu tất cả các cửa sổ trong chương trình của bạn không hoạt động. Nếu bạn đang mắc kẹt trong kịch bản đầu tiên sau đó sử dụng cũ api FlashWindow: http://pinvoke.net/default.aspx/user32/FlashWindow.html – tunafish24

7

Trong Windows 7, một thanh tiến trình trên một biểu mẫu được thể hiện trong taskba của nó nút r; bạn có thể tận dụng điều đó. Ngoài ra còn có một cách để chỉ đơn giản là làm nổi bật các nút thanh tác vụ, giống như các chương trình IM làm khi bạn nhận được một tin nhắn mới.

+0

Tôi rất thích nhìn thấy làm thế nào để chỉ đơn giản là làm nổi bật nút thanh tác vụ thay vì nhấp nháy nó ... gặp khó khăn trong việc tìm kiếm các mẫu mã tốt để làm điều đó. –

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