Knowledge Base Nr: 00223 clipboard.cpp - http://www.swe-kaiser.de

Win32: clipboard auslesen am beispiel brockhaus lexikon cd recherche

  
#include "../cpp_classes/ProcessSupport.h"

void CBrockhausabfrageDlg::OnButton1()
{
CString strSearch, strResult;

GetDlgItem(IDC_SEARCH)->GetWindowText(strSearch);

//lexikon starten falls noch nicht gestartet
CWnd* pWnd = FindWindow(NULL, "PC-Bibliothek");
if (!pWnd)
{
::SetCurrentDirectory("c:\\pc-bib");
::WinExec("C:\\pc-bib\\pcbib_bi.exe", SW_SHOW);
}

//warten bis lexikon gestartet
for (int r=0; r<30; r++)
{
Sleep(100);
pWnd = FindWindow(NULL, "PC-Bibliothek");
if (pWnd)
break;
}

if (pWnd)
{
CString strKeys;

//suchen in lexikon
for (int c=0; c<strSearch.GetLength(); c++)
{
strKeys += strSearch[c];
strKeys += '#';
}

strKeys += "ENTER#";

int nErr = CProcessSupport::SendKeysToWindow("", "PC-Bibliothek"
, strKeys
, 10, 1000
, false, false);

CString strLast;

//alle suchergebnisse sammeln (mit max. begrenzung)
for (int i=0; i<20; i++)
{
//umschalten auf text
strKeys = "TAB#ALT+C";

int nErr = CProcessSupport::SendKeysToWindow("", "PC-Bibliothek"
, strKeys
, 10, 100
, false, false);

//text auslesen
if (::IsClipboardFormatAvailable(CF_TEXT))
{
if (::OpenClipboard(GetDlgItem(IDC_RESULT)->GetSafeHwnd()))
{
HGLOBAL hglb = ::GetClipboardData(CF_TEXT);
if (hglb != NULL)
{
LPTSTR lptstr = (char*)::GlobalLock(hglb);
if (lptstr != NULL)
{
//abbruch wenn kein weiterer eintrag
if (strLast == lptstr)
{
::CloseClipboard();
break;
}

strLast = lptstr;

strResult += lptstr;
::GlobalUnlock(hglb);
}
}

::CloseClipboard();
}
}

//umschalten auf liste und nächsten eintrag auswählen
strKeys = "TAB#TAB#DOWN#ENTER#";

nErr = CProcessSupport::SendKeysToWindow("", "PC-Bibliothek"
, strKeys
, 10, 100
, false, false);

//break;
}


//lexikon beenden
nErr = CProcessSupport::SendKeysToWindow("", "PC-Bibliothek"
, "ALT+F4#"
, 10, 100
, false, false);
}

m_result.SetSel(0,-1);
m_result.ReplaceSel(strResult);

/////////////////
/////////////////

CString strFile;

strFile = CProcessSupport::GetCommandlineParam("file");
if (!strSearch.IsEmpty())
{
FILE* fp = fopen(strSearch, "w+t");
if (fp)
{
fputs(strResult, fp);
fclose(fp);
}
else
{
AfxMessageBox(strSearch + " kann nicht geschrieben werden!");
}
}
}