C#프로젝트 -4 에서 했던 시스템 트레이와 -2 에서 했던 단축키를 이용하여 최소화를 누를경우 시스템트레이로 가고
다시 이 프로그램을 ctrl+q로 불러오는 프로그램입니다.
코드입니다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
RegisterHotKey(this.Handle, KEYid, KeyInform.Control, Keys.Q); //단축키 추가
}
//폼 최상위로 띄우기
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string SClassName, string SWindowName);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr findname);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr findname, int howShow);
private const int showNORMAL = 1;
private const int showMINIMIZED = 2;
private const int showMAXIMIZED = 3;
//단축키
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr itp, int id, KeyInform fsInform, Keys vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr itp, int id);
const int KEYid = 31197;
public enum KeyInform
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
const int HOTKEYGap = 0x0312;
protected override void WndProc(ref Message message)
{
switch (message.Msg)
{
case HOTKEYGap:
Keys key = (Keys)(((int)message.LParam >> 16) & 0xFFFF); //눌러 진 단축키의 키
KeyInform modifier = (KeyInform)((int)message.LParam & 0xFFFF);//눌려진 단축키의 수식어
if ((KeyInform.Control) == modifier && Keys.Q == key)
{
IntPtr findname = FindWindow(null, "Form1");
if (!findname.Equals(IntPtr.Zero))
{
this.Visible = true;
miniMomBtn.Visible = false;
// 윈도우 타이틀명으로 핸들을 찾는다
if (!findname.Equals(IntPtr.Zero))
{
// 프로그램이 최소화 되어 있다면 활성화 시킴
ShowWindowAsync(findname, showNORMAL);
// 윈도우에 포커스를 줘서 최상위로 만듬
SetForegroundWindow(findname);
}
}
}
break;
}
base.WndProc(ref message);
}
private void Form1_Shown(object sender, EventArgs e)
{
this.miniMomBtn.Visible = false;
miniMomBtn.ContextMenuStrip = cms;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
miniMomBtn.Visible = true;
this.Visible = false;
return;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
miniMomBtn.Visible = false;
// 윈도우 타이틀명으로 핸들을 찾는다
IntPtr findname = FindWindow(null, "Form1");
if (!findname.Equals(IntPtr.Zero))
{
// 프로그램이 최소화 되어 있다면 활성화 시킴
ShowWindowAsync(findname, showNORMAL);
// 윈도우에 포커스를 줘서 최상위로 만듬
SetForegroundWindow(findname);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
댓글