- PR -

非アクティブウィンドウをショートカットキーでアクティブ化したい

1
投稿者投稿内容
コウイチ
常連さん
会議室デビュー日: 2003/11/04
投稿数: 48
投稿日時: 2003-11-18 12:22
こんにちは〜

OS:WindowsXP Pro
開発環境:VS.Net2003 C#
で作成しています。

フォームからフォーカスが外れると、非表示にし、
ショートカットキー?(ホットキー?)(例:Ctrl+Shift+Q)と押すと
フォームが表示されるようにしたいの思っています。
フォーカスが外れると非表示にすることはできたのですが、
キーを押して表示(アクティブ化)させることができません。

↓のキーイベント
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);
だと、フォーカスがあたっていないとキーイベントが起こりません。

非アクティブ時に、キーを押してアクティブに(表示)するにはどうすればいいでしょうか?
一番下のOnKeyDownメソッドでConsole.WriteLineが書いてありますが、
単にキーを押したときに認識されているかどうかのチェック用に入れただけですので
気にしないでください。

よろしくお願いします。


using System;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;

namespace Rensyu2 {
/// <summary>
/// MainForm の概要の説明です。
/// </summary>
public class MainForm :Form{

private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItemExit;
private System.Windows.Forms.MenuItem menuItemVersion;
private System.Windows.Forms.MenuItem menuItemHelp;
private System.Windows.Forms.MenuItem menuItemSetting;
private System.ComponentModel.IContainer components;

[STAThread]
static void Main() {
Application.Run(new MainForm());
}

public MainForm() {
InitializeComponent();

this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItemSetting = new System.Windows.Forms.MenuItem();
this.menuItemHelp = new System.Windows.Forms.MenuItem();
this.menuItemVersion = new System.Windows.Forms.MenuItem();
this.menuItemExit = new System.Windows.Forms.MenuItem();

// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] {this.menuItemSetting});
this.contextMenu1.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] {this.menuItemHelp});
this.contextMenu1.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] {this.menuItemVersion});
this.contextMenu1.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] {this.menuItemExit});

// Initialize menuItem1
this.menuItemExit.Index = 3;
this.menuItemExit.Text = "E&xit";

this.menuItemVersion.Index = 2;
this.menuItemVersion.Text = "バージョン";

this.menuItemHelp.Index = 1;
this.menuItemHelp.Text = "ヘルプ";

this.menuItemSetting.Index = 0;
this.menuItemSetting.Text = "設定";

this.menuItemExit.Click += new System.EventHandler(this.menuItem1_Click);

// Set up how the form should be displayed.
this.ClientSize = new System.Drawing.Size(292, 266);
this.Text = "て〜す";

// Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

// The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = new Icon("pager01.ico");

// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1;

notifyIcon1.Text = "てすと〜";
notifyIcon1.Visible = true;

notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_OneClick);

// タスクバーに表示しない
this.ShowInTaskbar = false;

// フォーカスが外れたら非表示にする
this.Deactivate += new System.EventHandler(this.HideWindow);

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);
}

private void HideWindow(object Sender, EventArgs e){
this.Hide();
}

protected override void Dispose( bool disposing ) {
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose();

base.Dispose( disposing );
}

private void notifyIcon1_OneClick(object Sender, EventArgs e) {

// 表示されていない場合は、表示する
if( this.Visible == false ) this.Show();

this.WindowState = FormWindowState.Normal;

// Activate the form.
this.Activate();
}

private void menuItem1_Click(object Sender, EventArgs e) {

this.Close();
}
protected override void WndProc(ref Message m) {
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;

if(m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) {
Hide();
}
else {
base.WndProc (ref m);
}
}

private void InitializeComponent() {

this.SuspendLayout();

//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(292, 253);
this.Name = "MainForm";

this.ResumeLayout(false);
}

private void OnKeyDown( object sendar, KeyEventArgs e){
if (e.KeyCode == Keys.Q && e.Shift && e.Control) {
this.Show();
Console.WriteLine(Keys.Control + "+" + Keys.Shift + "+" + e.KeyCode + "です");
}
}
}
}
コウイチ
常連さん
会議室デビュー日: 2003/11/04
投稿数: 48
投稿日時: 2003-11-18 21:20
user32.dllのRegisterHotKeyを使えばできるのはわかったのですが、
RegisterHotKeyの第3引数の設定方法がわかりません。
第3引数を0にすれば、”Q”を押せば、フォームがアクティブになりました。
Shift+Ctrl+Qでフォームをアクティブにしたいのですが、
Shift+Qや、Ctrl+Qが上手くできません。
第3引数のキー修飾子フラグは、C#ではどのように設定すればいいのでしょうか?
MOD_SHIFT=?
MOD_CONTROL=?
よろしくお願いします。



[DllImport("user32.dll")]
public static extern int RegisterHotKey(uint hwnd, int HK_ID, uint MOD_KEY, uint KEY);


RegisterHotKey((uint) this.Handle.ToInt32(), HK_ID, (uint)Keys.Shift, (uint)Keys.Q);



protected override void WndProc(ref Message m) {

 ・・・・・

 base.WndProc (ref m);
 int param = (int) m.WParam.ToInt32();
 if(param == HK_ID){
  this.Show();
  this.Activate();
 }
}
ジェイ
ベテラン
会議室デビュー日: 2002/10/06
投稿数: 62
投稿日時: 2003-11-18 21:58
どうも、ジェイです。

私も似たような機能を実装したアプリを作成中ですが、
MOD_SHIFTは0x0004、
MOD_CONTROLは0x0002、
ついでに言うと、
MOD_ALTは0x0001のようです。

ここに書いてありました。
http://www.hidecnet.ne.jp/~sinzan/tips/win/win_tip1b.htm

Shift+Ctrl+Qでやりたいときには、
第2引数で「MOD_SHIFT | MOD_CONTROL」とやればいいようです。
おそらく、この場合だと第2引数をuintでキャストしなくていいと思います。
コウイチ
常連さん
会議室デビュー日: 2003/11/04
投稿数: 48
投稿日時: 2003-11-19 09:23
ジェイさん
返答本当にありがとうございます。

さっそく試してみたら、上手く行きました。
またよろしくお願いします。m(_ _)m
1

スキルアップ/キャリアアップ(JOB@IT)