2017-04-26 7 views
0

좋아, 그럼 간단한 스크린 샷 프로그램을 만들려고하는데 스크린 샷 찍을 때와 프로그램을 실행하도록 설정할 때 BalloonToolTip을 표시하는 프로그램이 필요합니다. 시작에. 아래의 코드는 제 전체 프로그램을 보여 주며, 형식이 없으며 디자이너도 없습니다. Program.cs에서 실행되는 프로그램이며 콘솔 응용 프로그램이 아닙니다.C# 비 형식 응용 프로그램에서 BalloonToolTip 만드는 방법

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은 여전히 ​​실행되지 않습니다. Im은 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를 사용할 때 –