سؤال

For a program I'm making with Visual Basic 2010 Express, I want to hide (minimize) my form by default. I found out how to do that, so that's not the problem.

But when I press for example the "F12" key, I want the form to be shown again. A difficulty here is that there is no focus on the form. Does anyone know how to show the form again without having focus on the application?

هل كانت مفيدة؟

المحلول

Check these links (different approaches - pick your style):

EDIT: Regarding the last one, here is a reduced example, which is very easy to reproduce. In a brand new WinForms app, drop a timer on the form, set its Enabled = True in designer, and then paste the following into your form's code:

Public Class Form1
  Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Int32) _
                                                                        As Short

  Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If GetAsyncKeyState(Keys.F12) Then Me.WindowState = FormWindowState.Normal
  End Sub
End Class

Start your app, minimize the form and press F12. This will bring your form up. You can focus on something else than VS, to confirm that it's working as expected. I tried with VS 2013 Premium RC and Google Chrome (where F12 brought up the form and also opened the dev tools).

Considering the amount of code, compared to other answers, this would be my favorite approach. For any disadvantages that experienced code gurus may find with it - please feel free to post as a comment.

Note: If you use a function signature from the link, you get a pinvoke stack imbalance.

نصائح أخرى

Take a look at this global keyboard hook library. I used it in an application that sat in the tray until a certain set of keys were pressed.

Just create a private instance of the GlobalKeyboardHook class in your form's constructor.

private GlobalKeyboardHook gkh = new GlobalKeyboardHook();

Then add the "hooked" keys and wire up event handlers

gkh.HookedKeys.Add(Keys.LControlKey);
gkh.HookedKeys.Add(Keys.RControlKey);
gkh.HookedKeys.Add(Keys.Down);

gkh.KeyDown += new System.Windows.Forms.KeyEventHandler(gkh_KeyDown);
gkh.KeyUp += new System.Windows.Forms.KeyEventHandler(gkh_KeyUp);

In the event handlers check for the correct key(s) to be pressed and then show your form again.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top