2017-04-26 36 views
0

好了做一個BalloonToolTip,所以我試圖做一個簡單的截圖程序,我需要的程序顯示一個BalloonToolTip時的截圖拍攝並在程序設置爲運行在啓動。下面的代碼顯示了我的整個程序,沒有形式,也沒有設計師。只是一個從Program.cs運行的程序,它不是控制檯應用程序。C#如何從非形式應用

using System; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using System.Windows; 
using FullscreenShot.Properties; 
using GlobalHotKey; 
using System.Windows.Input; 
using System.Timers; 
using System.Threading.Tasks; 
using Microsoft.Win32; 

namespace FullscreenShot 
{ 
class Program 
{ 
    private static NotifyIcon notifyIcon; 
    private static HotKeyManager hotKeyManager; 

    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     System.Windows.Forms.Application.EnableVisualStyles(); 
     System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); 
     // We need to dispose here, or the icon will not remove until the 
     // system tray is updated. 
     System.Windows.Forms.Application.ApplicationExit += delegate{ notifyIcon.Dispose(); }; 
     CreateNotifyIcon(); 
     System.Windows.Forms.Application.Run(); 
    } 

    /// <summary> 
    /// Creates the icon that sits in the system tray. 
    /// </summary> 

    private static void CreateNotifyIcon() 
    { 
     notifyIcon = new NotifyIcon 
     { 
      Icon = Resources.AppIcon, 
      ContextMenu = GetContextMenu() 
     }; 
     notifyIcon.Visible = true; 
/*------------------------------------------------------------*/ 
     hotKeyManager = new HotKeyManager();//Creates Hotkey manager from GlobalHotKey 

     var hotKey = hotKeyManager.Register(Key.F12, ModifierKeys.Control); // Sets Hotkey to Control + F12, ModifierKeys must be pressed first/ 

     hotKeyManager.KeyPressed += HotKeyManagerPressed; //Creates void for "HotKeyManagerPressed" 

     void HotKeyManagerPressed(object sender, KeyPressedEventArgs e) //Checks to see if hotkey is pressed 
     { 
      if(e.HotKey.Key == Key.F12) 

      { 
       TakeFullScreenShotAsync(); 
       BalloonTip(); 
       // MessageBox.Show("Screenshot Taken"); 
      } 
     } 

    } 

    /// <summary> 
    /// Creates BalloonTip to notify you that your Screenshot was taken. 
    /// </summary> 
    private static void BalloonTip() 
    { 
     notifyIcon.Visible = true; 
     notifyIcon.Icon = SystemIcons.Information; 
     notifyIcon.ShowBalloonTip(740, "Important From Screenshot", "Screenshot Taken", ToolTipIcon.Info); 
    } 

    private static void BalloonTip2() 
    { 
     notifyIcon.Visible = true; 
     notifyIcon.Icon = SystemIcons.Information; 
     notifyIcon.ShowBalloonTip(1, "Important From Screenshot", "Screenshot will now begin on startup", ToolTipIcon.Info); 
    } 

    ///<summary> 
    ///Creates the contextmenu for the Icon 
    ///<summary> 
    private static ContextMenu GetContextMenu() 
    { 
     string myPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); 
     System.Diagnostics.Process prc = new System.Diagnostics.Process(); 
     prc.StartInfo.FileName = myPath; 
     ContextMenu menu = new ContextMenu(); 
     menu.MenuItems.Add("Take Screenshot (Ctrl+F12)", delegate { TakeFullScreenShotAsync(); }); 
     menu.MenuItems.Add("Open Folder", delegate { prc.Start(); }); 
     menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); }); 
     menu.MenuItems.Add("Run On Startup", delegate { RunOnStartup(); }); 

     return menu; 

    } 

    /// <summary> 
    /// Simple function that finds Registry and adds the Application to the startup 
    /// </summary> 
    private static void RunOnStartup() 
    { 
     RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
     reg.SetValue("MyApp", Application.ExecutablePath.ToString()); 
     BalloonTip2(); 
     MessageBox.Show("The Program will now start on startup"); 
    } 


    /// <summary> 
    /// Gets points for the screen uses those points to build a bitmap of the screen and saves it. 
    /// </summary> 
    private static async void TakeFullScreenShotAsync() 
    { 
     await Task.Delay(750);//Allows task to be waited on for .75ths of a second. Time In ms. 


     int width = Screen.PrimaryScreen.Bounds.Width; 
     int height = Screen.PrimaryScreen.Bounds.Height; 

     using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb)) 
     { 
      using (Graphics graphics = Graphics.FromImage(screenshot)) 
      { 
       System.Drawing.Point origin = new System.Drawing.Point(0, 0); 
       System.Drawing.Size screenSize = Screen.PrimaryScreen.Bounds.Size; 
       //Copy Entire screen to entire bitmap. 
       graphics.CopyFromScreen(origin, origin, screenSize); 
      } 

      //Check to see if the file exists, if it does, append. 
      int append = 1; 

      while (File.Exists($"Screenshot{append}.jpg")) 
       append++; 

      string fileName = $"Screenshot{append}.jpg"; 
      screenshot.Save(fileName, ImageFormat.Jpeg); 
     } 
    } 
} 
} 

現在,你可能並不需要所有這一切,但我要確保我沒惹什麼都漲的過程中,並且是被發現我的資源和圖標設置,我只是不理解爲什麼這不起作用。

+1

是否採取截圖?當你調試應用程序時,你可以在'BalloonTip'方法中找到一個斷點嗎?不知道你是否有複製/粘貼問題 - 但我不確定你是否允許嵌套方法('HotKeyManagerPressed'在'CreateNotifyIcon'內)? –

回答

1

我只是複製所有的代碼有一個小問題,不知道它的複製粘貼錯誤你需要保持HotKeyManagerPressed之外。它爲我工作,我看到通知我這是Windows 10的通知。

using System; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using System.Windows; 
using GlobalHotKey; 
using System.Windows.Input; 
using System.Timers; 
using System.Threading.Tasks; 
using Microsoft.Win32; 
using FullScreenShot.Properties; 

namespace FullScreenShot 
{ 
    class Program 
    { 
     private static NotifyIcon notifyIcon; 
     private static HotKeyManager hotKeyManager; 

     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      System.Windows.Forms.Application.EnableVisualStyles(); 
      System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); 
      // We need to dispose here, or the icon will not remove until the 
      // system tray is updated. 
      System.Windows.Forms.Application.ApplicationExit += delegate { notifyIcon.Dispose(); }; 
      CreateNotifyIcon(); 
      System.Windows.Forms.Application.Run(); 
     } 

     /// <summary> 
     /// Creates the icon that sits in the system tray. 
     /// </summary> 

     private static void CreateNotifyIcon() 
     { 
      notifyIcon = new NotifyIcon 
      { 
       Icon = Resources.AppIcon, 
       ContextMenu = GetContextMenu() 
      }; 
      notifyIcon.Visible = true; 
      /*------------------------------------------------------------*/ 
      hotKeyManager = new HotKeyManager();//Creates Hotkey manager from GlobalHotKey 

      var hotKey = hotKeyManager.Register(Key.F12, ModifierKeys.Control); // Sets Hotkey to Control + F12, ModifierKeys must be pressed first/ 

      hotKeyManager.KeyPressed += HotKeyManagerPressed; //Creates void for "HotKeyManagerPressed" 



    } 
     private static void HotKeyManagerPressed(object sender, KeyPressedEventArgs e) //Checks to see if hotkey is pressed 
     { 
      if (e.HotKey.Key == Key.F12) 

      { 
       TakeFullScreenShotAsync(); 
       BalloonTip(); 
       // MessageBox.Show("Screenshot Taken"); 
      } 
     } 
     /// <summary> 
     /// Creates BalloonTip to notify you that your Screenshot was taken. 
     /// </summary> 
     private static void BalloonTip() 
     { 
      notifyIcon.Visible = true; 
      notifyIcon.Icon = SystemIcons.Information; 
      notifyIcon.ShowBalloonTip(740, "Important From Screenshot", "Screenshot Taken", ToolTipIcon.Info); 
     } 

     private static void BalloonTip2() 
     { 
      notifyIcon.Visible = true; 
      notifyIcon.Icon = SystemIcons.Information; 
      notifyIcon.ShowBalloonTip(1, "Important From Screenshot", "Screenshot will now begin on startup", ToolTipIcon.Info); 
     } 

     ///<summary> 
     ///Creates the contextmenu for the Icon 
     ///<summary> 
     private static ContextMenu GetContextMenu() 
     { 
      string myPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); 
      System.Diagnostics.Process prc = new System.Diagnostics.Process(); 
      prc.StartInfo.FileName = myPath; 
      ContextMenu menu = new ContextMenu(); 
      menu.MenuItems.Add("Take Screenshot (Ctrl+F12)", delegate { TakeFullScreenShotAsync(); }); 
      menu.MenuItems.Add("Open Folder", delegate { prc.Start(); }); 
      menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); }); 
      menu.MenuItems.Add("Run On Startup", delegate { RunOnStartup(); }); 

      return menu; 

     } 

     /// <summary> 
     /// Simple function that finds Registry and adds the Application to the startup 
     /// </summary> 
     private static void RunOnStartup() 
     { 
      RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
      reg.SetValue("MyApp", Application.ExecutablePath.ToString()); 
      BalloonTip2(); 
      MessageBox.Show("The Program will now start on startup"); 
     } 


     /// <summary> 
     /// Gets points for the screen uses those points to build a bitmap of the screen and saves it. 
     /// </summary> 
     private static async void TakeFullScreenShotAsync() 
     { 
      await Task.Delay(750);//Allows task to be waited on for .75ths of a second. Time In ms. 


      int width = Screen.PrimaryScreen.Bounds.Width; 
      int height = Screen.PrimaryScreen.Bounds.Height; 

      using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb)) 
      { 
       using (Graphics graphics = Graphics.FromImage(screenshot)) 
       { 
        System.Drawing.Point origin = new System.Drawing.Point(0, 0); 
        System.Drawing.Size screenSize = Screen.PrimaryScreen.Bounds.Size; 
        //Copy Entire screen to entire bitmap. 
        graphics.CopyFromScreen(origin, origin, screenSize); 
       } 

       //Check to see if the file exists, if it does, append. 
       int append = 1; 

       while (File.Exists($"Screenshot{append}.jpg")) 
        append++; 

       string fileName = $"Screenshot{append}.jpg"; 
       screenshot.Save(fileName, ImageFormat.Jpeg); 
      } 
     } 
    } 
} 

檢查圖像 enter image description here

+0

他們不再嵌套在一起,但balloontip仍不能運行,還林在Win10 64bit系統。任何其他建議 –

+0

我還注意到的另一件事是你不等待TakeFullScreenShotAsync,也許嘗試。 – Krishna

0

你有沒有叫你TakeFullScreenShotAsync()方法BalloonTip()方法。

private static async void TakeFullScreenShotAsync() 
    { 
     await Task.Delay(750);//Allows task to be waited on for .75ths of a second. Time In ms. 


     int width = Screen.PrimaryScreen.Bounds.Width; 
     int height = Screen.PrimaryScreen.Bounds.Height; 

     using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb)) 
     { 
      using (Graphics graphics = Graphics.FromImage(screenshot)) 
      { 
       System.Drawing.Point origin = new System.Drawing.Point(0, 0); 
       System.Drawing.Size screenSize = Screen.PrimaryScreen.Bounds.Size; 
       //Copy Entire screen to entire bitmap. 
       graphics.CopyFromScreen(origin, origin, screenSize); 
      } 

      //Check to see if the file exists, if it does, append. 
      int append = 1; 

      while (File.Exists($"Screenshot{append}.jpg")) 
       append++; 

      string fileName = $"Screenshot{append}.jpg"; 
      screenshot.Save(fileName, ImageFormat.Jpeg); 

      // Call the Show Tip Message Here... 
      BalloonTip(); 
     } 
    } 

行:因爲我有一個熱鍵已經被註冊,因此我將其更改爲別的,爲我工作hotKeyManager.Register(...)拋出一個錯誤。

+0

我沒有必要調用BalloonTip();功能在異步,因爲我把它叫做當你使用熱鍵Ctrl + F12 –