HWND控制視窗動作

摘要:HWND控制視窗動作

由於雷射機台參數檔案過於多,USER反應希望能

變成比較人性化一點的方式操作,討論後做一個小程式去刷參數檔案並COPY到廠商參數檔案的資料夾內

變成每次參數只會留一個,而原始程式必須按更新下拉combox才會去做更新LIST清單動作

如原設定參數檔不見(更新會跳出錯警告窗),所以就必須先透過外部程式把要參數檔案刷進去

刷完在下拉combox並載入後設定,知道大致上的操作就開始動手做半自動外掛小程式

幫助USER別再挑錯了

1.首先要有兩個好用的小幫手程式 

Microsoft_Spy++_14.0.22129.1

WinSpy17

可以方便看視窗的相關資訊

核心code

procedure TGAS29999_M.bt_sendClick(Sender: TObject);
var 
  LOG_STR,Ins_str: string;
  num,i,j,flag,flag2:integer;
  sqlarrayer: OleVariant;
  sr: TSearchRec;
  sPath,f,starget: String;
  FileList,s1 : TStringList;
  arr: array[0..254] of Char;
  var WHandle,WHandle2,wnd,wnd2,wnd3,wnd4,wchild,wchild2,wchild3,wchild3t,wchild4:HWND;
begin

    clientdataset2.Close;
    clientdataset2.CommandText := 'SELECT * FROM PARAM_POOL WHERE PARAM_CATEGORY=''LS_Machine'' AND PARAM_NAME=''LS_Machine01''';
    clientdataset2.Open;
    if clientdataset2.FieldByName('PARAM_VALUE').AsString <> p_ver then
    begin
      messageDlg('程式版本需變更請先執行CIM,再執行此程式!!', mtError, [mbOk], 0);
      close;              //20150923001
    end
    else if t_recipes.Text <>'' then
    begin
      FileList:=TStringList.Create;
      sPath:=T_Source.text+t_recipes.Text+t_subfile.text;
      starget:=T_Target.text+t_recipes.Text+t_subfile.text;

      f := T_Target.text+'*.*';
      if FindFirst(f, faAnyFile, sr) = 0 then
      begin
         repeat
            if ((sr.Attr and faArchive)>0)then
            begin
             DeleteFile(T_Target.text+sr.Name)
            end;
         until FindNext(sr) <> 0;
        end;


      if FindFirst(sPath, faAnyFile, sr) = 0 then
      begin
         repeat
            if ((sr.Attr and faArchive)>0)then
            begin
            FileList.Add(T_Source.text+sr.Name);
            if CopyFile(PChar(sPath), PChar(starget), False) then
            ShowMessage('檔案複製成功!')
            else
            ShowMessage('檔案複製失敗!');
            end;

         until FindNext(sr) <> 0;
      end
      else
      begin
        messageDlg('資料夾內無此RECIPE', mtError, [mbOk], 0);
      end;
      FindClose(sr);
    end
    else
   messageDlg('請刷入RECIPE資料!!', mtError, [mbOk], 0);

   //第一個面板 start
      s1:=tstringlist.Create;
      flag :=0;
      j:=0 ;
      wnd := FindWindow('ThunderRT6FormDC',nil);
      while flag = 0 do
      begin

        wnd2:= FindWindowEx(wnd,0,'ThunderRT6Frame',nil);
        For i := 1 to 30 do
        begin
         if wnd2 <> 0 then
         begin
         wnd3:=FindWindowEx(wnd2,0,'Button','參數設定');
         if wnd3 <> 0 then flag:=1;
         if wnd3 <> 0 then begin SendMessage(wnd3, BM_CLICK, 0,0); break; end;
         end;
         wnd2 := GetWindow(wnd2, GW_HWNDNEXT);
        end;

        inc(j);
        wnd := GetWindow(wnd, GW_HWNDNEXT);
       if j=200 then begin   messageDlg('Error:line65', mtError, [mbOk], 0); break;  end;
      end;
//第一個面板 end

//參數設定面板      強制更新     start
      flag :=0;
      j:=0 ;
      flag2 :=0;
       while flag2 = 0 do
       begin
       wchild := FindWindow('ThunderRT6FormDC',nil);
       num := GetWindowText(wchild, arr, SizeOf(arr))  ;
       // s1.Add(inttostr(j)+'-'+inttostr(i)+':'+arr+':'+IntToHex(wchild,2)) ;
       //  s1.Add( copy(arr,1,12)) ;   //3059c

        if copy(arr,1,12)='使用中參數檔' then
        begin
         flag2:=1
        end;
         if  flag2<>1 then     wchild := GetWindow(wchild, GW_HWNDNEXT);
         inc(j);
         if j=100 then begin   messageDlg('last_ErrorUP', mtError, [mbOk], 0); break;end;
       end;
      j:=0 ;
      while flag = 0 do
      begin
        wchild2:= FindWindowEx(wchild,0,'ThunderRT6Frame',nil);
        For i := 1 to 10 do
        begin

         if wchild2 <> 0 then
         begin
         wchild3:= FindWindowEx(wchild2,0,'ThunderRT6Frame','');
         if wchild3 <> 0 then
          begin
           wchild4:=FindWindowEx(wchild3,0,'Button','更新');
           if wchild4 <> 0 then
           begin
            PostMessage(wchild4, BM_CLICK, 0,0);
            flag:=1;
            break;
           end;
          end;

         end;
         wchild2 := GetWindow(wchild2, GW_HWNDNEXT);
        end;
        inc(j);
       if j=10 then begin   messageDlg('Error:line113', mtError, [mbOk], 0); break;  end;
      end;

//  messagebox click
        For i := 1 to 3 do
        begin
        sleep(600);
        WHandle:=FindWindow('#32770','NaviScriber');
        WHandle2 := FindWindowEx(WHandle,0,'Button','確定');
        if WHandle <> 0 then  PostMessage(WHandle2, BM_CLICK, 0,0);
        end;
//參數設定面板      強制更新     end

//下拉combox  Start
    flag2 :=0;
       while flag2 = 0 do
       begin
       wchild := FindWindow('ThunderRT6FormDC','');
       wchild2:= FindWindowEx(wchild,0,'ThunderRT6Frame','');
          For i := 1 to 3 do
          begin
           if wchild2<>0 then   wchild3:=FindWindowEx(wchild2,0,'ThunderRT6Frame',nil);
            if wchild3 <> 0 then
            begin
             wchild4:=FindWindowEx(wchild3,0,'ThunderRT6ComboBox',nil);
             if wchild4 <> 0 then
             begin
              SendMessage(wchild4,WM_KEYDOWN,VK_DOWN,0);
              Sleep(300);
              PostMessage(wchild4,WM_KEYUP,VK_DOWN,0);
              flag2:=2;
              break;
             end;
            end;
           wchild2 := GetWindow(wchild2, GW_HWNDNEXT);
          end;
       wchild := GetWindow(wchild, GW_HWNDNEXT);
       inc(j);
       if j=100 then begin   messageDlg('Error:line151', mtError, [mbOk], 0); break;end;
       end;
  // s1.SaveToFile('.\a.txt');
//下拉combox  End

//參數設定面板     正常重新更新 Start
      flag :=0;
      j:=0 ;
      flag2 :=0;
       while flag2 = 0 do
       begin
       wchild := FindWindow('ThunderRT6FormDC',nil);
       num := GetWindowText(wchild, arr, SizeOf(arr))  ;

        if copy(arr,1,12)='使用中參數檔' then
        begin
         flag2:=1
        end;
         if  flag2<>1 then     wchild := GetWindow(wchild, GW_HWNDNEXT);
         inc(j);
         if j=100 then begin   messageDlg('Error:line171', mtError, [mbOk], 0); break;end;
       end;
      j:=0 ;

      while flag = 0 do
      begin
        wchild2:= FindWindowEx(wchild,0,'ThunderRT6Frame',nil);
        For i := 1 to 10 do
        begin

         if wchild2 <> 0 then
         begin
         wchild3:= FindWindowEx(wchild2,0,'ThunderRT6Frame','');
         if wchild3 <> 0 then
          begin
           wchild4:=FindWindowEx(wchild3,0,'Button','更新');
           if wchild4 <> 0 then
           begin
            PostMessage(wchild4, BM_CLICK, 0,0);
            flag:=1;
            break;
           end;
          end;

         end;
         wchild2 := GetWindow(wchild2, GW_HWNDNEXT);
        end;
        inc(j);
       if j=10 then begin   messageDlg('Error:line199', mtError, [mbOk], 0); break;  end;
      end;

比較該注意的地方就是

WHandle:=FindWindow('#32770','XXX');  <Messagebox問題

PostMessage  常說的射後不理(送出去之後馬上就return)  

SendMessage 反之送出後會停住等待處理完回應才會return

不過廠商的程式滿多沒寫視窗標題所以用nil去做全部比對

FindWindowEx(wchild,0,'ThunderRT6Frame',nil); 

 

 

 

 

大家一起加入blogads 賺零用錢!!