本人刚刚学习delphi,借鉴网上资料自己写了个QQ2009的登录器。用如下的代码在QQ经典模式能正确获取窗口句柄并输入QQ账号密码。但是如果选择2009模式的话问题就出现了。 
if ComboBox1.ItemIndex <>-1 then 
begin 
ShellExecute(0, 'open',PChar(dir),nil,nil, SW_SHOWNORMAL); 
Sleep(1000); 
main:=findwindow(nil,'QQ2009 正式版'); 
ShowMessage(IntToStr(main)); <-2009模式必须加这句才能获取窗口和控件句柄,程序才能正确运行。 
if main>0 then 
begin 
getpassword; 
combo:=GetWindow(main,GW_CHILD); 
edt:=GetWindow(combo,GW_HWNDNEXT); 
Sleep(200); 
SetForegroundWindow(combo); 
Sleep(200); 
SendKeys(login,False); 
SetForegroundWindow(edt); 
Sleep(200); 
SendKeys(psw,False); 
SetForegroundWindow(main); 
Sleep(200); 
SendKeys('{ENTER}',False); 
end; 
Application.Minimize; 
end 
else 
Application.MessageBox('请选择QQ账号!', 'QQ提醒您:', MB_OK + MB_ICONWARNING); 
end; 
请问下为什么会这样?该怎么解决? 

解决方案 »

  1.   

    你的意思是加上这句就正常了?
    那还有什么需要解决的?
    不过edit.text属性就是一个String,用整形接受字符串是不行的,所以要强制类型转换
      

  2.   

    我的意思是能不能把这句showmessage去掉直接执行程序,弹对话框出来很烦。
      

  3.   

    但是,取消掉showmessage就取不到句柄了(在2009模式下,就是一个竖条的登录界面那种)。请教下怎么解决?
      

  4.   

    >>ShowMessage(IntToStr(main));
    换成
    PostMessage ???
    这句起到的作用,似乎就是延长时间而已不过,最好再测试下,觉得不大好理解,因为findwindow已经执行了,main已经确定值,不存在用showmessage能再改变main的值了。
      

  5.   

    我把showmessage这句换成sleep也不行。只有出了对话框才能取到句柄。
      

  6.   

    这个倒有些奇怪了,加不加ShowMessage一句取到的句柄值不一样?
      

  7.   

    奇怪的问题
    是不是DELPHI版本的原因
      

  8.   

    做一下尝试:把shellexecute换成WinExecAndWait32Visibility 参数为0即可.function WinExecAndWait32(FileName:String; Visibility : 
    integer):integer;
    var
      zAppName:array[0..512] of char;
      zCurDir:array[0..255] of char;
      WorkDir:String;
      StartupInfo:TStartupInfo;
      ProcessInfo:TProcessInformation;
    begin
      StrPCopy(zAppName,FileName);
      GetDir(0,WorkDir);
      StrPCopy(zCurDir,WorkDir);
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := Visibility;
      if not CreateProcess(nil,
        zAppName,                      { pointer to command line string }
        nil,                           { pointer to process security 
    attributes }
        nil,                           { pointer to thread security 
    attributes }
        false,                         { handle inheritance flag }
        CREATE_NEW_CONSOLE or          { creation flags }
        NORMAL_PRIORITY_CLASS,
        nil,                           { pointer to new environment block }
        nil,                           { pointer to current directory name }
        StartupInfo,                   { pointer to STARTUPINFO }
        ProcessInfo) then Result := -1 { pointer to PROCESS_INF }  else begin
        WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
        GetExitCodeProcess(ProcessInfo.hProcess,Result);
      end;
    end;