2012-01-19 11 views
14

में कोई विशेष घटना तब होती है जब मैं एक एप्लिकेशन बनाने के लिए सी #/डब्ल्यूपीएफ का उपयोग कर रहा हूं। उस एप्लिकेशन में, यदि कोई विशेष घटना होती है तो मैं खिड़की को झपकी देना चाहता हूं ताकि उस एप्लिकेशन के उपयोगकर्ता को पता चले कि कुछ हुआ। मैं इसे अपने सी # डब्ल्यूपीएफ एप्लिकेशन में कैसे प्राप्त कर सकता हूं।सी #/डब्ल्यूपीएफ

याहू मैसेंजर की तरह, अगर आपको कोई संदेश मिलता है तो संदेश विंडो आपकी फोकस पाने के लिए झपकी देती है, मैं अपने आवेदन में उस प्रभाव का उपयोग करना चाहता हूं।

+0

मैं WPF में नया हूँ और मैं गूगल पर खोज का उपयोग कर WinAPI समारोह FlashWindowEx कॉल करने के लिए लेकिन मुझे कोई आसान समाधान नहीं मिला जो मैं आसानी से समझ सकता हूं ... –

+0

@ मार्को वह लिंक दिखाता है कि टास्कबा में विंडो ब्लिंक कैसे बनाएं आर, एक नियमित आकार की खिड़की झपकी कैसे बनाने के लिए नहीं। – Rachel

उत्तर

27

एक समान तरीके से खिड़की और टास्कबार चमकती सूचनाएं IM के लिए निम्नलिखित कोड का उपयोग कर WPF में पूरा किया जा सकता। यह PlatformInvoke का उपयोग करता है WPF की Win32 हैंडल Application.Current.MainWindow

कोड

public class FlashWindowHelper 
{ 
    private IntPtr mainWindowHWnd; 
    private Application theApp; 

    public FlashWindowHelper(Application app) 
    { 
     this.theApp = app; 
    } 

    public void FlashApplicationWindow() 
    { 
     InitializeHandle(); 
     Flash(this.mainWindowHWnd, 5); 
    } 

    public void StopFlashing() 
    { 
     InitializeHandle(); 

     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = CreateFlashInfoStruct(this.mainWindowHWnd, FLASHW_STOP, uint.MaxValue, 0); 
      FlashWindowEx(ref fi); 
     } 
    } 

    private void InitializeHandle() 
    { 
     if (this.mainWindowHWnd == IntPtr.Zero) 
     { 
      // Delayed creation of Main Window IntPtr as Application.Current passed in to ctor does not have the MainWindow set at that time 
      var mainWindow = this.theApp.MainWindow; 
      this.mainWindowHWnd = new System.Windows.Interop.WindowInteropHelper(mainWindow).Handle; 
     } 
    } 

    [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="hwnd"></param> 
    /// <returns></returns> 
    public static bool Flash(IntPtr hwnd) 
    { 
     // Make sure we're running under Windows 2000 or later 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = CreateFlashInfoStruct(hwnd, FLASHW_ALL | FLASHW_TIMERNOFG, uint.MaxValue, 0); 

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

    private static FLASHWINFO CreateFlashInfoStruct(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="hwnd">The handle of the Window to Flash.</param> 
    /// <param name="count">The number of times to Flash.</param> 
    /// <returns></returns> 
    public static bool Flash(IntPtr hwnd, uint count) 
    { 
     if (Win2000OrLater) 
     { 
      FLASHWINFO fi = CreateFlashInfoStruct(hwnd, FLASHW_ALL | FLASHW_TIMERNOFG, count, 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 Environment.OSVersion.Version.Major >= 5; } 
    } 
} 

प्रयोग

var helper = new FlashWindowHelper(Application.Current); 

// Flashes the window and taskbar 5 times and stays solid 
// colored until user focuses the main window 
helper.FlashApplicationWindow(); 

// Cancels the flash at any time 
helper.StopFlashing(); 
+0

यह एक अच्छा उदाहरण है..मेरे लिए मेरे लिए टास्कबार आइकन केवल एक बार कम से कम एक बार ब्लिंक करता है :( – Julien

+0

वास्तव में? किस ओएस पर? मैंने उपरोक्त उपयोग किया (यह WinXP दिमाग पर था) और यह एक अच्छा 5 के लिए फ्लैश करेगा सेकंड, फिर ठोस नारंगी रहें। –

+0

मेरी माफी, आपकी कक्षा के लिए उपयोग की जाने वाली विंडो हैंडल मेरी परियोजना के लिए गलत थी। मैंने इसे कस्टम हैंडल स्वीकार करने के लिए थोड़ा संशोधित किया और सब ठीक है। बहुत धन्यवाद! – Julien

6

आप अपने ऐप ब्लिंक के टास्क बार आइकन को बनाने के लिए TaskBarItem क्लास का उपयोग कर सकते हैं। यहां कुछ संसाधन इसे प्राप्त करने में मदद मिलेगी कि कर रहे हैं:
- http://social.msdn.microsoft.com/Forums/en/wpf/thread/369bc5ee-a9a7-493f-978f-27a8ddedea06
- http://www.bobpowell.net/flashbar.htm

तो आप कर सकते हैं flash, shake, fade-in fade-out या whatever one of the other zillion FX WPF एनिमेशन का उपयोग कर। यह बहुत आसान है और लगभग कोई कोड आवश्यक नहीं है, अगर आपके पास अभिव्यक्ति मिश्रण है तो नौकरी को और भी आसान बना दिया गया है।

+0

मैं सिर्फ WPF विंडो को झपकी देना चाहता हूं, मैं प्रगति नहीं दिखाना चाहता हूं। मैं इस कोड का उपयोग करता हूं लेकिन यह मेरे काम में मेरी मदद नहीं करता है। क्या आप बस मुझे बता सकते हैं कि मैं विशेष घटना पर WPF विंडो को कैसे झपकी दूंगा, चाहे मैं बटन पर क्लिक करें या विशिष्ट समय विंडो ब्लिंक पर क्लिक करें। –

+0

उत्तर अद्यतन किया गया। मैंने प्रगति पट्टी का उल्लेख नहीं किया है, आप खिड़की झपकी के अलावा, टास्कबार आइकन खुद को झपकी दे सकते हैं। – Shimmy

+1

कुछ लिंक मृत, कृपया उन्हें ठीक करें। –

संबंधित मुद्दे