Home > Net >  reopen minimized window visual studio application developpment
reopen minimized window visual studio application developpment

Time:10-24

im using visual studio to make a screen recorder using the ScreenRecorderLib library (but it is not related to my question) and c#. It is my first app ever even though i made some games in unity using c#. I want to, on record button click or keyboard shortcut (for example crtl a), to minimize the window in taskbar. I managed to do this using

private void RecordButton_Click(object sender, EventArgs e)
    {
        ActiveControl = null;

        try
        {
            if (_IsRecording)
            {
                //some actions to stop recording (not interresting for my question)
                WindowState = FormWindowState.Normal;
            }
            UpdateProgress();

            

            if (_rec == null)
            {
                // some actions to make settings active and launch the recording
                WindowState = FormWindowState.Minimized;
            }


        }}

and my code for the shortcuts


private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode.ToString() == "A")
            {
                RecordButton.PerformClick();
            }
        }

This is not my real code but i tried to select some interresting parts for my question. I manage to minimize my button in taskbar with shortcut or with button, but not to stop the recording using the shortcut while it is minimized. I would like that while recording and window minimised to be able to click crtl a to stop the recording

Any ideas???

CodePudding user response:

"How to use shortcut keys when the application is inactive" has been collected in API: MouseKeyHook.

You only need to modify the operations you need in the appropriate places.

enter image description here

You can also write global hotkeys yourself.

Only realize the function of shrinking the window:

private void Form1_Load(object sender, EventArgs e) {
    this.KeyPreview = true;
}

private void Form1_KeyDown(object sender, KeyEventArgs e) {
    if (e.Control && e.KeyCode == Keys.A) {
        this.WindowState = FormWindowState.Minimized;
    }
}

CodePudding user response:

https://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/#comments

This perfectly answers my question. I will put a code down below for the people who want to have more than one shortcut. I'm a beginner, so mabe my explanations are not correct, but i tried to explain them in the esasiest way and in the way i understood them

public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id1, int fsModifiers, int vk);
        //for creating another hotKey
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool RegisterHotKey1(IntPtr hWnd, int id2, int fsModifiers, int vk);
//here you can create even more shortcuts

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id1);
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool UnregisterHotKey1(IntPtr hWnd, int id2);
//remember unreg them
        enum KeyModifier
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            WinKey = 8
        }
        public Form1()
        {
            InitializeComponent();

            int id1 = 0;     // The id of the hotkey. 
            RegisterHotKey(this.Handle, id1, (int)KeyModifier.Shift, Keys.F1.GetHashCode());
            int id2 = 1;     // The 2nd id of the hotkey. 
            RegisterHotKey(this.Handle, id2, (int)KeyModifier.Shift, Keys.F2.GetHashCode());// Register Shift   A as global hotkey. 
        }


        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.Msg == 0x0312)
            {

                if (m.WParam.ToInt32() == 0)
                {
                    RecordButton.PerformClick();
                }
                if(m.WParam.ToInt32() == 1)
                {
                    PauseButton.PerformClick();
                }
                //add the other shortcuts just replace == 1 by the id value
            }
        }

        private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnregisterHotKey(this.Handle, 0);       // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey.
            UnregisterHotKey(this.Handle, 1);
        }
//delete them with the id

  • Related