2017-04-20 1 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

프로그램에 포커스가 있지 않으면 단축키가 작동하지 않습니다. 키 이벤트는 포커스가있을 때만 응용 프로그램으로 보내집니다.

관련 문제