2017-04-20 35 views
0

好吧,我正在使用位圖等來製作這個簡單的截圖程序,但是當我嘗試製作像f12這樣的熱鍵時,沒有任何反應,我編碼它只是顯示一個消息框,但它甚至沒有這樣做。所以我把它設置回來做兩個消息框並截圖,但仍然無法正常工作。 private static NotifyIcon notifyIcon;如何將熱鍵設置爲非表格程序

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


    static void Program_KeyDown(object sender, KeyEventArgs e) 
    { 
     if(e.KeyCode == Keys.F12) 
     { 
      MessageBox.Show("ScreenShot Taken"); 
      TakeFullScreenShot(); 
     } 
     else if(e.KeyCode == Keys.F11) 
     { 
      Application.Exit(); 
     } 
    } 
    private static void CreateNotifyIcon() 
    { 
     notifyIcon = new NotifyIcon 
     { 
      Icon = Resources.AppIcon, ContextMenu = GetContextMenu() 
     }; 
      notifyIcon.Visible = true; 

    } 

    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 (F12)", delegate { TakeFullScreenShot(); }); 
     menu.MenuItems.Add("Open Folder", delegate { prc.Start(); }); 
     menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); }); 

     return menu; 

    } 

    private static void TakeFullScreenShot() 
    { 
     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)) 
      { 
       Point origin = new Point(0, 0); 
       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); 
     } 
    } 

你可能不需要所有這些,但它只是爲了確保我在嘗試構建它時不會弄亂任何東西。此外,這個程序沒有任何形式,它只是一個在你的任務欄中的圖標,如果你右鍵點擊它並點擊截圖(F12)它將截屏與出問題。謝謝!

+3

http://stackoverflow.com/questions/604410/global-keyboard-capture-in-c-sharp-application –

回答

0

除非程序具有焦點,否則熱鍵將不起作用。關鍵事件只有在焦點時纔會發送到您的應用程序。

相關問題