WinForm] 단축키
단축키 관련 해서 질문글이 올라오길래 간단히 작성해보았음.
Key 와 대응하는 Execute 핸들러를 등록하여 관리 가능.
Execute 핸들러를 별도로 관리한다면... 게임에서 단축키등 교체하는 형태로도 바꿀수 있지 않을까??
namespace KeyMapping
{
public partial class Form1 : Form
{
KeyCommandHelper KeyHelper = null;
public Form1()
{
InitializeComponent();
//사용방법1
//KeyCommand F1CMD = new KeyCommand(Keys.F1, (s, e) => { label1.Text = "" + e.keyCMD.Key; });
//KeyCommand F2CMD = new KeyCommand(Keys.F2, (s, e) => { label1.Text = "" + e.keyCMD.Key; });
//KeyHelper = new KeyCommandHelper() {
// F1CMD,
// F2CMD
//};
//사용방법2
KeyHelper = new KeyCommandHelper() {
new KeyCommand(Keys.F1, (s, e) => { label1.Text = "" + e.keyCMD.Key; }),
new KeyCommand(Keys.F2, (s, e) => { label1.Text = "" + e.keyCMD.Key; })
};
//기타..
// KeyHelper.Add...
// Event별도로 추가가능...
}
// 간단하게!!
//protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
//{
// if (keyData == Keys.F1)
// {
// label1.Text = ""+keyData;
// }
// return base.ProcessCmdKey(ref msg, keyData);
//}
// 복잡하게
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
KeyHelper.Execute(keyData);
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
// KeyCommand Interface
public interface IKeyCommand
{
Keys Key { get; }
void Execute();
}
// KeyCommand 객체
public class KeyCommand : IKeyCommand
{
public KeyCommand(Keys key)
{
Key = key;
}
public KeyCommand( Keys key,EventHandler<KeyCommandExecuteEventArgs> handler)
{
Key = key;
Executed += handler;
}
public Keys Key
{
get;
private set;
}
public void Execute()
{
OnExecute(new KeyCommandExecuteEventArgs(this));
}
public event EventHandler<KeyCommandExecuteEventArgs> Executed = null;
protected void OnExecute(KeyCommandExecuteEventArgs e)
{
if (Executed != null)
{
Executed(this, e);
}
}
}
// Execute 시 이벤트 아규먼트
public class KeyCommandExecuteEventArgs : EventArgs
{
public IKeyCommand keyCMD {get; private set;}
public KeyCommandExecuteEventArgs(IKeyCommand cmd)
{
keyCMD = cmd;
}
}
// Command 관리 도우미
// IList 구현 이외의 코드
public class KeyCommandHelper : IList<IKeyCommand>
{
public KeyCommandHelper()
{
KeyLst = new List<IKeyCommand>();
}
public List<IKeyCommand> KeyLst { get; private set; }
public void Execute(Keys key)
{
IKeyCommand cmd = this[key];
if (cmd != null )
{
cmd.Execute();
}
}
#region IList 구현
public int IndexOf(IKeyCommand item)
{
return KeyLst.IndexOf(item);
}
public void Insert(int index, IKeyCommand item)
{
KeyLst.Insert(index, item);
}
public void RemoveAt(int index)
{
KeyLst.RemoveAt(index);
}
public IKeyCommand this[Keys key]
{
get {
return this[KeyLst.FindIndex(c => c.Key == key)];
}
}
public IKeyCommand this[int index]
{
get
{
return KeyLst[index];
}
set
{
KeyLst[index] = value;
}
}
public void Add(IKeyCommand item)
{
if (KeyLst.FindIndex(k => k.Key == item.Key) >= 0) throw new Exception("등록된 키입니다.");
KeyLst.Add(item);
}
public void Add(Keys key , EventHandler<KeyCommandExecuteEventArgs> evt )
{
KeyCommand cmd = new KeyCommand(key, evt);
if (KeyLst.FindIndex(k => k.Key == key) >= 0)
{
cmd = this[key] as KeyCommand;
if( cmd != null ) cmd.Executed += evt;
}
else
{
KeyLst.Add(cmd);
}
}
public void Clear()
{
KeyLst.Clear();
}
public bool Contains(IKeyCommand item)
{
return KeyLst.Contains(item);
}
public void CopyTo(IKeyCommand[] array, int arrayIndex)
{
KeyLst.CopyTo(array, arrayIndex);
}
public int Count
{
get { return KeyLst.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(IKeyCommand item)
{
return KeyLst.Remove(item);
}
public IEnumerator<IKeyCommand> GetEnumerator()
{
return KeyLst.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
'# 4) .Net ( Vs 2010 ) > C#' 카테고리의 다른 글
WinForm] 도킹 패널! (0) | 2013.07.08 |
---|---|
WINFORM] 컨트롤 동적 사이즈 조절... (0) | 2013.03.26 |
GDI+] 이미지 회전! (0) | 2012.08.13 |
2010 WinForm ] 컨트롤 리사이징 예제... (0) | 2012.07.20 |
.Net] 버튼 Pressed Event!! (0) | 2012.05.09 |