【问题标题】:How to set a hotkey to a Non-Form Program如何为非表单程序设置热键
【发布时间】:2017-09-16 21:52:32
【问题描述】:

好的,所以我正在使用位图等制作这个简单的屏幕截图程序,但是当我尝试制作像 f12 这样的热键时,没有任何反应,我对其进行编码只是为了显示一个消息框,但它没有甚至这样做。所以我把它重新设置为同时做​​消息框和截图,但仍然不起作用。 私有静态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)它将毫无问题地截取屏幕截图。谢谢!

【问题讨论】:

标签: c# hotkeys


【解决方案1】:

除非程序有焦点,否则热键将不起作用。 Key 事件仅在获得焦点时发送到您的应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2011-06-17
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多