HDC hdc; HWND hWnd; TCHAR sz[] = L"大家好"; // 查找记事本程序的窗口句柄 hWnd = ::FindWindow(L"Notepad", NULL); // 如果记事本程序在运行,就向其客户区绘制文本 int nCount=0; while(::IsWindow(hWnd)&&nCount<=10) // IsWindow函数用于判断一个窗口句柄是否有效 { hdc = ::GetDC(hWnd); ::TextOut(hdc, 10, 10, sz, wcslen(sz)); ::ReleaseDC(hWnd, NULL); ::Sleep(1000); /*INPUT minput[2]; ZeroMemory( &minput, sizeof(INPUT)*2); minput[0].type=INPUT_MOUSE; minput[0].mi.dx=100; minput[0].mi.dy=100; minput[0].mi.mouseData=0; minput[0].mi.dwFlags=MOUSEEVENTF_RIGHTDOWN; minput[1].type=INPUT_MOUSE; minput[1].mi.dx=100; minput[1].mi.dy=100; minput[1].mi.mouseData=0; minput[1].mi.dwFlags=MOUSEEVENTF_RIGHTUP; UINT uRet=SendInput( 2, minput, sizeof(INPUT) );*/ INPUT kbinput[5]; ZeroMemory( &kbinput, sizeof(INPUT)*5 ); kbinput[0].type = INPUT_KEYBOARD; kbinput[0].ki.wVk = 'Z'; kbinput[1].type = INPUT_KEYBOARD; kbinput[1].ki.wVk = 'W'; kbinput[2].type = INPUT_KEYBOARD; kbinput[2].ki.wVk = 'J'; //kbinput[2].ki.dwFlags = KEYEVENTF_KEYUP; kbinput[3].type=INPUT_MOUSE; kbinput[3].mi.dx=100; kbinput[3].mi.dy=100; kbinput[3].mi.mouseData=0; kbinput[3].mi.dwFlags=MOUSEEVENTF_RIGHTDOWN; kbinput[4].type=INPUT_MOUSE; kbinput[4].mi.dx=100; kbinput[4].mi.dy=100; kbinput[4].mi.mouseData=0; kbinput[4].mi.dwFlags=MOUSEEVENTF_RIGHTUP; UINT uRet = SendInput( 5, kbinput, sizeof(INPUT) ); cout<<uRet; if(uRet<=0) { DWORD dwErr=::GetLastError(); cout<<dwErr; } nCount++; } ::MessageBox(NULL, L"记事本程序已经退出", L"04UseDC", MB_OK);