📄 keydefdlg.cpp
字号:
// KeyDefDlg.cpp: implementation of the CKeyDefDlg class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "mixer.h"
#include "KeyDefDlg.h"
#include "MixerKeyTable.h"
#include "resource.h"
#include "KeyWaitWin.h"
#include <sstream>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CKeyDefDlg::CKeyDefDlg(CMixerKeyTable *pKeyTable,const char *szMod)
: CDlg(szMod),m_pcKeyTable(pKeyTable),m_strKey(),m_strSelect(),
m_strEdit(),m_pcKeyWin(NULL)
{
}
CKeyDefDlg::~CKeyDefDlg()
{
if(m_pcKeyWin) {
delete m_pcKeyWin;
m_pcKeyWin=NULL;
}
}
void CKeyDefDlg::PostCreate()
{
CDlg::PostCreate();
InitChildren();
}
LRESULT CKeyDefDlg::DlgProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg) {
case WM_USER:
if(m_pcKeyWin && (m_pcKeyWin==(CKeyWaitWin*)lParam)) {
AssignKey();
return true;
}
break;
}
return CDlg::DlgProc(hWnd,uMsg,wParam,lParam);
}
bool CKeyDefDlg::OnWMCommand(int id,HWND h,int wNotifyCode)
{
if(CBN_SELCHANGE == wNotifyCode) {
switch(id)
{
case IDC_KEYDEFLIST:
UpdateKeys();
return true;
break;
case IDC_KEYS:
UpdateEdit();
return true;
break;
}
}
if(BN_CLICKED == wNotifyCode) {
switch(id)
{
case IDC_ASSIGNEDIT:
AssignEdit();
return true;
break;
case IDC_NEWKEYDEF:
NewKeyDef();
return true;
break;
case IDC_RENAMEKEYDEF:
RenameKeyDef();
return true;
break;
case IDC_ASSIGNKEY:
if(!m_pcKeyWin) {
AssignKey();
}
return true;
break;
case IDC_NEWKEY:
if(!m_pcKeyWin) {
NewKey();
return true;
}
break;
case IDC_DELETEKEY:
DeleteKey();
return true;
break;
}
}
return CDlg::OnWMCommand(id,h,wNotifyCode);
}
void CKeyDefDlg::RenameKeyDef()
{
CMixerKeyDef *pKeyDef;
if((pKeyDef=m_pcKeyTable->GetKeyDef(m_strSelect)) == NULL)
NewKeyDef();
string strSel=GetControlText(IDC_KEYDEFLIST);
stringstream stm;
stm << strSel;
while((m_pcKeyTable->GetKeyDef(stm.str())) != NULL) {
stm << "(Renamed)";
}
stm << ends;
strSel=stm.str();
pKeyDef->SetName(strSel);
m_strPreSelect=strSel;
InitChildren();
}
void CKeyDefDlg::NewKeyDef()
{
string strSel=GetControlText(IDC_KEYDEFLIST); //GetSelection(IDC_KEYDEFLIST);
stringstream stm;
stm << strSel;
while((m_pcKeyTable->GetKeyDef(stm.str())) != NULL) {
stm << "(New)";
}
stm << ends;
strSel=stm.str();
m_pcKeyTable->AddKeyDef(CMixerKeyDef(strSel));
m_strPreSelect=strSel;
InitChildren();
}
void CKeyDefDlg::UpdateKeys()
{
ClearKeys();
ClearEdit();
m_strSelect=GetSelection(IDC_KEYDEFLIST);
if(m_strSelect.length() == 0) {
return;
}
SendDlgItemMessage(GetWndHandle(),IDC_KEYS,CB_RESETCONTENT,0,0);
const char *szKey=m_strKey.c_str();
CMixerKeyDef *pKeyDef=m_pcKeyTable->GetKeyDef(m_strSelect);
int iKeySel=-1;
if(pKeyDef) {
for(int i=0;i<pKeyDef->GetSize();i++) {
CKey *pKey=pKeyDef->GetKey(i);
if(pKey) {
const string str=pKey->GetDispKey();
const char *sz=str.c_str();
if(strcmp(sz,szKey) == 0)
iKeySel=i;
SendDlgItemMessage(GetWndHandle(),IDC_KEYS,CB_ADDSTRING,0,long(str.c_str()));
}
}
if(pKeyDef->GetSize() > 0) {
if(iKeySel == -1)
iKeySel=0;
SendDlgItemMessage(GetWndHandle(),IDC_KEYS,CB_SETCURSEL,iKeySel,0);
UpdateEdit();
}
}
// ClearKeys();
}
void CKeyDefDlg::ClearKeys()
{
SendDlgItemMessage(GetWndHandle(),IDC_KEYS,CB_RESETCONTENT,0,0);
}
void CKeyDefDlg::ClearEdit()
{
SendDlgItemMessage(GetWndHandle(),IDC_EDITCMD,WM_SETTEXT,0,NULL);
}
void CKeyDefDlg::InitChildren()
{
assert(m_pcKeyTable);
SendDlgItemMessage(GetWndHandle(),IDC_KEYDEFLIST,CB_RESETCONTENT,0,0);
int iSel=-1;
const char *szPre=m_strPreSelect.c_str();
for(int i=0;i<m_pcKeyTable->GetSize();i++) {
const string str=m_pcKeyTable->GetNameOf(i);
int iIdx=SendDlgItemMessage(GetWndHandle(),IDC_KEYDEFLIST,CB_ADDSTRING,0,long(str.c_str()));
const char *sz=str.c_str();
if(strcmp(szPre,sz) == 0)
iSel=iIdx;
}
if(iSel >= 0)
SendDlgItemMessage(GetWndHandle(),IDC_KEYDEFLIST,CB_SETCURSEL,iSel,0);
UpdateKeys();
}
string CKeyDefDlg::GetSelection(int id)
{
int iSel=SendDlgItemMessage(GetWndHandle(),id,CB_GETCURSEL,0,0);
if(iSel != CB_ERR) {
int iLen=SendDlgItemMessage(GetWndHandle(),id,CB_GETLBTEXTLEN,iSel,0);
if(iLen == CB_ERR)
return string("");
char *pszBuf=new char[iLen+1];
SendDlgItemMessage(GetWndHandle(),id,CB_GETLBTEXT,iSel,long(pszBuf));
string strSel=string(pszBuf);
delete [] pszBuf;
return strSel;
}
return string("");
}
CKey *CKeyDefDlg::GetCurrentKey()
{
m_strSelect=GetSelection(IDC_KEYDEFLIST);
m_strKey=GetSelection(IDC_KEYS);
const char *szKey=m_strKey.c_str();
CMixerKeyDef *pKeyDef=m_pcKeyTable->GetKeyDef(m_strSelect);
if(pKeyDef) {
CKey *pKey=NULL;
for(int i=0;i<pKeyDef->GetSize();i++) {
pKey=pKeyDef->GetKey(i);
if(pKey) {
const string str=pKey->GetDispKey();
const char *sz=str.c_str();
if(strcmp(sz,szKey) == 0)
return pKey;
}
}
return NULL;
}
return NULL;
}
void CKeyDefDlg::UpdateEdit()
{
ClearEdit();
CKey *pKey;
if((pKey=GetCurrentKey()))
DisplayEdit(pKey);
else
ClearEdit();
}
void CKeyDefDlg::DisplayEdit(CKey *pKey)
{
stringstream stm;
float f;
for(int i=0; i<pKey->GetCmd().GetSize(); i++) {
cmd::TCmd c=pKey->GetCmd().Get(i);
if(c == 0x0)
break;
int iNumPara=0;
stm << pKey->GetCmd().GetDispText(c,iNumPara);
if(iNumPara) {
char *pc=(char*)&f;
for(int j=0;j<sizeof(float)/sizeof(char);j++)
pc[j]=pKey->GetCmd().Get(i+j+1);
i+=j;
stm << pKey->GetCmd().GetDispText(cmd::equals,iNumPara) << f;
}
stm << pKey->GetCmd().GetDispText(cmd::seperator,iNumPara);
}
stm << ends;
m_strEdit=stm.str();
SendDlgItemMessage(GetWndHandle(),IDC_EDITCMD,WM_SETTEXT,0,long(m_strEdit.c_str()));
}
void CKeyDefDlg::AssignEdit()
{
CKey *pKey=GetCurrentKey();
CompileKey(pKey);
NotifyMixer();
}
void CKeyDefDlg::CompileKey(CKey*pKey)
{
if(pKey) {
m_strEdit=GetControlText(IDC_EDITCMD);
pKey->Compile(m_strEdit);
UpdateEdit();
m_pcKeyTable->SetChanged();
}
}
void CKeyDefDlg::StartKeyGetDlg()
{
if(!m_pcKeyWin) {
m_pcKeyWin=new CKeyWaitWin(GetWndHandle(),GetModuleName());
m_pcKeyWin->Init(GetParHandle());
POINT ptCursor;
::GetCursorPos(&ptCursor);
m_pcKeyWin->SetWindowClientRect(CWinRect(ptCursor.x,ptCursor.y,ptCursor.x+80,ptCursor.y+40));
m_pcKeyWin->Create();
::SetWindowPos(m_pcKeyWin->GetWndHandle(),HWND_TOPMOST,0,0,10,10,SWP_NOMOVE|SWP_NOSIZE);
}
}
void CKeyDefDlg::StopKeyGetDlg()
{
if(m_pcKeyWin) {
delete m_pcKeyWin;
m_pcKeyWin=NULL;
::SetWindowPos(GetWndHandle(),HWND_TOP,0,0,10,10,SWP_NOMOVE|SWP_NOSIZE);
}
}
void CKeyDefDlg::DeleteKey()
{
CKey *pKey=GetCurrentKey();
if(pKey) {
CMixerKeyDef *pKeyDef=m_pcKeyTable->GetKeyDef(m_strSelect);
if(pKeyDef) {
pKeyDef->DeleteKey(pKey);
}
}
UpdateKeys();
NotifyMixer();
}
void CKeyDefDlg::NewKey()
{
if(!m_pcKeyWin) {
SendDlgItemMessage(GetWndHandle(),IDC_KEYS,CB_SETCURSEL,-1,0);
StartKeyGetDlg();
return;
}
}
void CKeyDefDlg::AssignKey()
{
if(!m_pcKeyWin) {
StartKeyGetDlg();
return;
}else{
int iVkScan=m_pcKeyWin->GetKey();
long lKeyData=m_pcKeyWin->GetKeyData();
StopKeyGetDlg();
CKey *pKey=GetCurrentKey();
if(pKey) {
pKey->SetVirtKey(iVkScan,lKeyData);
m_strKey=pKey->GetDispKey();
UpdateKeys();
}else{
pKey=AddKeyToDef(iVkScan,lKeyData);
}
InitNewKey(pKey);
UpdateKeys();
NotifyMixer();
}
}
void CKeyDefDlg::InitNewKey(CKey*pKey)
{
if(pKey) {
CompileKey(pKey);
m_strKey=pKey->GetDispKey();
}
}
CKey *CKeyDefDlg::AddKeyToDef(long iVkScan,long lKeyData)
{
CKey *pKey=NULL;
CMixerKeyDef *pKeyDef=m_pcKeyTable->GetKeyDef(m_strSelect);
if(pKeyDef) {
CKey cKey;
cKey.SetVirtKey(iVkScan,lKeyData);
pKeyDef->AddKey(cKey);
pKey=pKeyDef->GetKeyClassByKey(iVkScan);
}
return pKey;
}
string CKeyDefDlg::GetControlText(int id)
{
int iLen=SendDlgItemMessage(GetWndHandle(),id,WM_GETTEXTLENGTH,0,0);
char *pszBuf=new char[iLen+1];
SendDlgItemMessage(GetWndHandle(),id,WM_GETTEXT,iLen+1,long(pszBuf));
string str=pszBuf;
delete [] pszBuf;
return str;
}
void CKeyDefDlg::NotifyMixer()
{
::PostMessage(GetParHandle(),WM_USER,0x1,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -