今天想给大家分享一下我在学习windows.h时写的一段代码,可以给大家参考。
话不多说,上代码
#include <iostream>
#include <cstring>
#include <conio.h>
#include <windows.h>
using namespace std;
void findwin()
{
HWND window;
string wname="";
cout << "窗口名称:";
cin >> wname;
cout << "发送关闭指令...";
LPCTSTR lpc=wname.c_str();
window=FindWindow(NULL,lpc);
SendMessage(window,WM_CLOSE,0,0);
cout << "完成。";
getch();
}
void vkeyboard()
{
HWND window;
string wname="";
char wchar;
cout << "窗口名称:";
cin >> wname;
cout << "\n输入字符:";
getchar();getchar();
wchar=getch();
cout << "\n发送字符...";
LPCTSTR lpc=wname.c_str();
window=FindWindow(NULL,lpc);
SendMessage(window,WM_CHAR,WPARAM(wchar),0);
cout << "完成。";
getch();
}
void alttest()
{
cout << "Alt键侦测\n当检测到Alt键被按下输出提示\n侦测时长:10s\nEnter开始:";
getch();
cout << "\n============侦测开始============\n\n";
for(int i=1;i<=100;i++)
{
if(i%10==0)
{
cout << "\n第" << i/10 << "秒,还剩" << 10-i/10 << "秒。\n" << endl;
}
if((int)GetAsyncKeyState(VK_MENU)<0)
{
//MessageBox(NULL, "Alt按下","侦测程序",MB_ICONEXCLAMATION|MB_OK);
cout << "侦测到Alt按下。";
}
Sleep(100);
}
cout << "\n============侦测结束============\n";
getch();
}
int main()
{
char choice;
cout << "系统操作测试\n1.寻找窗口并关闭它\n2.虚拟键盘输入\n3.alt键侦测\n";
// getchar();
choice=getch();
cout << endl;
if(choice=='1')
{
findwin();
}
else if(choice=='2')
{
vkeyboard();
}
else if(choice=='3')
{
alttest();
}
else
{
cout << "未知命令。";
getch();
}
return 0;
}
好了,没什么好说的了,希望对各位有帮助
拜拜~