Knowledge Base Nr: 00071 wintv_einstellen.cpp - http://www.swe-kaiser.de

Win32: 'fernbedienen' der hauppauge win-tv dialogs
über den capDlgVideoSource() dialog von video for windows.

  
// tastendrücke an den eigenen prozess schicken.
// hilfreich um aus einer c-dll fremdapplikationen zu triggern
// (z.b. nach beeenden eines kommunikationthreads an forms)
// oder fremdapplikationen zu steuern
// (hier als beispiel: einstellen der hauppauge win-tv dialogs
// über den capDlgVideoSource() dialog von video for windows).

#define VS_SOURCE_TV "ALT+T#ENTER#"
#define VS_SOURCE_COMPOSITE "ALT+C#ENTER#"

void CVideoServerView::OnTv()
{
int nErr = g_video.SetVideoSource(VS_SOURCE_TV);
if (nErr)
Error(g_video.GetLastError());
}

void CVideoServerView::OnComposite()
{
int nErr = g_video.SetVideoSource(VS_SOURCE_COMPOSITE);
if (nErr)
Error(g_video.GetLastError());
}


int CVideoSupport::SetVideoSource(const char* szKeys)
{
s_strSetupDlg = szKeys;

HANDLE hnd = CreateThread(
0, // pointer to security attributes
0, // initial thread stack size
SetupDialog, // pointer to thread function
"Video Source", // argument for new thread
0, // creation flags
0 // pointer to receive thread ID
);

if (!hnd)
{
m_strLastError.Format("SetVideoSource: CreateThread failed!");
return -1;
}

BOOL bSucc = capDlgVideoSource (s_hWndC);
if (!bSucc)
{
m_strLastError.Format("SetVideoSource: capDlgVideoSource failed!");
return -2;
}

return 0;
}


DWORD WINAPI CVideoSupport::SetupDialog(LPVOID lpParameter)
{
HWND hwnd = NULL;

while (!hwnd) //auf dialog warten
{
Sleep(5);
hwnd = ::FindWindow(NULL, (const char*)lpParameter);
}

HWND orgHwnd = ::GetForegroundWindow();

::SetForegroundWindow(hwnd);
CProcessSupport::DoKeyPress(s_strSetupDlg, 200); //dialog einstellen

if (orgHwnd)
::SetForegroundWindow(orgHwnd);

return 0;
}


// int nErr = DoKeyPress("F8#", 100);
// int nErr = DoKeyPress("SHIFT+F8#", 100);
// int nErr = DoKeyPress("ALT+F8#", 100);
// int nErr = DoKeyPress("CTRL+F8#", 100);
// int nErr = DoKeyPress("F7#F7#F8#", 100);
int CProcessSupport::DoKeyPress(const char* szKeys, int nDelay)
{
int nKeysPressed = 0;

char szError[100] = {0};
char szTmp[100] = {0};
bool bWithAlt = false;
bool bWithShift = false;
bool bWithCtrl = false;

int i = 0;
for (int n=0; szKeys[n]; n++)
{
szTmp[i++] = szKeys[n];

if (szKeys[n] == '+')
{
szTmp[i-1] = 0;
i = 0;

if (stricmp(szTmp, "ALT") == 0)
bWithAlt = true;
else if (stricmp(szTmp, "SHIFT") == 0)
bWithShift = true;
else if (stricmp(szTmp, "CTRL") == 0)
bWithCtrl = true;
else
{
sprintf(szError, "<%s> muss sein ALT, SHIFT oder CTRL", szTmp);
MessageBox(NULL, szTmp, "Fehler in DoKeyPress()", MB_OK|MB_ICONERROR);
return -1;
}
}
else if (szKeys[n] == '#')
{
szTmp[i-1] = 0;
i = 0;

int nWert = -1;

/* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
/* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
if (stricmp(szTmp, "A") == 0)
nWert = 'A';
else if (stricmp(szTmp, "B") == 0)
nWert = 'B';
else if (stricmp(szTmp, "C") == 0)
nWert = 'C';
else if (stricmp(szTmp, "D") == 0)
nWert = 'D';
else if (stricmp(szTmp, "E") == 0)
nWert = 'E';
else if (stricmp(szTmp, "F") == 0)
nWert = 'F';
else if (stricmp(szTmp, "G") == 0)
nWert = 'G';
else if (stricmp(szTmp, "H") == 0)
nWert = 'H';
else if (stricmp(szTmp, "I") == 0)
nWert = 'I';
else if (stricmp(szTmp, "J") == 0)
nWert = 'J';
else if (stricmp(szTmp, "K") == 0)
nWert = 'K';
else if (stricmp(szTmp, "L") == 0)
nWert = 'L';
else if (stricmp(szTmp, "M") == 0)
nWert = 'M';
else if (stricmp(szTmp, "N") == 0)
nWert = 'N';
else if (stricmp(szTmp, "O") == 0)
nWert = 'O';
else if (stricmp(szTmp, "P") == 0)
nWert = 'P';
else if (stricmp(szTmp, "Q") == 0)
nWert = 'Q';
else if (stricmp(szTmp, "R") == 0)
nWert = 'R';
else if (stricmp(szTmp, "S") == 0)
nWert = 'S';
else if (stricmp(szTmp, "T") == 0)
nWert = 'T';
else if (stricmp(szTmp, "U") == 0)
nWert = 'U';
else if (stricmp(szTmp, "V") == 0)
nWert = 'V';
else if (stricmp(szTmp, "W") == 0)
nWert = 'W';
else if (stricmp(szTmp, "X") == 0)
nWert = 'X';
else if (stricmp(szTmp, "Y") == 0)
nWert = 'Y';
else if (stricmp(szTmp, "Z") == 0)
nWert = 'Z';

else if (stricmp(szTmp, "0") == 0)
nWert = '0';
else if (stricmp(szTmp, "1") == 0)
nWert = '1';
else if (stricmp(szTmp, "2") == 0)
nWert = '2';
else if (stricmp(szTmp, "3") == 0)
nWert = '3';
else if (stricmp(szTmp, "4") == 0)
nWert = '4';
else if (stricmp(szTmp, "5") == 0)
nWert = '5';
else if (stricmp(szTmp, "6") == 0)
nWert = '6';
else if (stricmp(szTmp, "7") == 0)
nWert = '7';
else if (stricmp(szTmp, "8") == 0)
nWert = '8';
else if (stricmp(szTmp, "9") == 0)
nWert = '9';

else if (stricmp(szTmp, "F1") == 0)
nWert = VK_F1;
else if (stricmp(szTmp, "F2") == 0)
nWert = VK_F2;
else if (stricmp(szTmp, "F3") == 0)
nWert = VK_F3;
else if (stricmp(szTmp, "F4") == 0)
nWert = VK_F4;
else if (stricmp(szTmp, "F5") == 0)
nWert = VK_F5;
else if (stricmp(szTmp, "F6") == 0)
nWert = VK_F6;
else if (stricmp(szTmp, "F7") == 0)
nWert = VK_F7;
else if (stricmp(szTmp, "F8") == 0)
nWert = VK_F8;
else if (stricmp(szTmp, "F9") == 0)
nWert = VK_F9;
else if (stricmp(szTmp, "F10") == 0)
nWert = VK_F10;
else if (stricmp(szTmp, "F11") == 0)
nWert = VK_F11;
else if (stricmp(szTmp, "F12") == 0)
nWert = VK_F12;

else if (stricmp(szTmp, "TAB") == 0)
nWert = VK_TAB;
else if (stricmp(szTmp, "SPACE") == 0)
nWert = VK_SPACE;
else if (stricmp(szTmp, "ESC") == 0)
nWert = VK_ESCAPE;
else if ((stricmp(szTmp, "ENTER") == 0) || (stricmp(szTmp, "RETURN") == 0))
nWert = VK_RETURN;
else if (stricmp(szTmp, "PGUP") == 0)
nWert = VK_PRIOR;
else if (stricmp(szTmp, "PGDN") == 0)
nWert = VK_NEXT;
else if (stricmp(szTmp, "LEFT") == 0)
nWert = VK_LEFT;
else if (stricmp(szTmp, "RIGHT") == 0)
nWert = VK_RIGHT;
else if (stricmp(szTmp, "UP") == 0)
nWert = VK_UP;
else if (stricmp(szTmp, "DOWN") == 0)
nWert = VK_DOWN;

else
{
sprintf(szError, "Unbekannter oder nicht implementierter VK_xxx Code <%s>", szTmp);
MessageBox(NULL, szError, "Fehler in DoKeyPress()", MB_OK|MB_ICONERROR);
return -2;
}

//event an process schicken
if (nWert > 0)
{
if (bWithAlt)
keybd_event(VK_MENU, 0, 0, 0);
if (bWithShift)
keybd_event(VK_SHIFT, 0, 0, 0);
if (bWithCtrl)
keybd_event(VK_CONTROL, 0, 0, 0);

keybd_event(nWert ,0,0,0);
keybd_event(nWert ,0,KEYEVENTF_KEYUP,0);

if (bWithAlt)
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
if (bWithShift)
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
if (bWithCtrl)
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
}

Sleep(nDelay);
bWithAlt = false;
bWithShift = false;
bWithCtrl = false;
i = 0;

nKeysPressed++;
}
}

return nKeysPressed;
}