一個魔獸世界的簡易自動登錄器


效果圖如下:

具體代碼如下:

   1 // WowlauDlg.cpp : implementation file
   2 //
   3 
   4 #include "stdafx.h"
   5 #include "Wowlau.h"
   6 #include "WowlauDlg.h"
   7 #include <locale>//為了使用setlocale(LC_CTYPE, "chs");便於輸入中文到txt中
   8 #include <cstring>
   9 #include "windows.h"
  10 #include "winuser.h"
  11 #include "Shlwapi.h"
  12 #include <time.h>
  13 #include <conio.h>
  14 #include <stdio.h>
  15 #include "XSleep.h"
  16 
  17 #ifdef _DEBUG
  18 #define new DEBUG_NEW
  19 #define WM_SENDSTRING WM_USER+100
  20 #undef THIS_FILE
  21 static char THIS_FILE[] = __FILE__;
  22 #endif
  23 int x = 0;
  24 DWORD WINAPI ClientThread(LPVOID lpParam){
  25     int i = 0;
  26     while (i < 10){
  27     }
  28     return 0;
  29 }
  30 void ShowCount1()
  31 {
  32     CString str;
  33     int i = 0;
  34     HWND hwnd = AfxGetApp()->GetMainWnd()->GetSafeHwnd();
  35     while(i < 10)
  36     {
  37         Sleep(20);
  38  //       str.Format(_T("已耗時%d秒,請稍候..."),i+1);
  39   //      ::SetWindowText(hwnd,str);
  40         i ++;
  41     }
  42 
  43 }
  44 void ShowCount2()
  45 {
  46     CString str;
  47     int i = 0;
  48     HWND hwnd = AfxGetApp()->GetMainWnd()->GetSafeHwnd();
  49     while(i < 10)
  50     {
  51         Sleep(100);
  52  //       str.Format(_T("已耗時%d秒,請稍候..."),i+1);
  53   //      ::SetWindowText(hwnd,str);
  54         i ++;
  55     }
  56 
  57 }
  58 void ShowCount3()
  59 {
  60     CString str;
  61     int i = 0;
  62     HWND hwnd = AfxGetApp()->GetMainWnd()->GetSafeHwnd();
  63     while(i < 10)
  64     {
  65         Sleep(50);
  66  //       str.Format(_T("已耗時%d秒,請稍候..."),i+1);
  67   //      ::SetWindowText(hwnd,str);
  68         i ++;
  69     }
  70 
  71 }
  72 
  73 struct XSleep_Structure
  74 {
  75     int duration;
  76     HANDLE eventHandle;
  77 };
  78  
  79 
  80 DWORD WINAPI XSleepThread(LPVOID pWaitTime)
  81 {
  82     XSleep_Structure *sleep = (XSleep_Structure *)pWaitTime;
  83  
  84     Sleep(sleep->duration);
  85     SetEvent(sleep->eventHandle);
  86  
  87     return 0;
  88 }
  89 
  90 void XSleep(int nWaitInMSecs)
  91 {
  92     XSleep_Structure sleep;
  93      
  94     sleep.duration      = nWaitInMSecs;
  95     sleep.eventHandle   = CreateEvent(NULL, TRUE, FALSE, NULL);
  96  
  97     DWORD threadId;
  98     CreateThread(NULL, 0, &XSleepThread, &sleep, 0, &threadId);
  99  
 100     MSG msg;
 101      
 102     while(::WaitForSingleObject(sleep.eventHandle, 0) == WAIT_TIMEOUT)
 103     {
 104         //get and dispatch messages
 105         if(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
 106         {
 107             ::TranslateMessage(&msg);
 108             ::DispatchMessage(&msg);
 109         }
 110     }
 111  
 112     CloseHandle(sleep.eventHandle);
 113 }
 114 
 115 //https://bbs.csdn.net/topics/300057427
 116 struct SInfo
 117 {
 118 public:
 119     SInfo()
 120     {
 121         strInfo = "";
 122     };
 123     CString strInfo;
 124 };
 125 
 126 /////////////////////////////////////////////////////////////////////////////
 127 // CAboutDlg dialog used for App About
 128 
 129 class CAboutDlg : public CDialog
 130 {
 131 public:
 132     CAboutDlg();
 133     DWORD m_dwPos;
 134     afx_msg void OnBnClickedOk();
 135     static void WINAPI ShowCount();
 136 
 137 
 138 // Dialog Data
 139     //{{AFX_DATA(CAboutDlg)
 140     enum { IDD = IDD_ABOUTBOX };
 141     //}}AFX_DATA
 142 
 143     // ClassWizard generated virtual function overrides
 144     //{{AFX_VIRTUAL(CAboutDlg)
 145     protected:
 146     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 147     //}}AFX_VIRTUAL
 148 
 149 // Implementation
 150 protected:
 151     //{{AFX_MSG(CAboutDlg)
 152     //}}AFX_MSG
 153     DECLARE_MESSAGE_MAP()
 154 
 155 };
 156 
 157 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
 158 {
 159     //{{AFX_DATA_INIT(CAboutDlg)
 160     //}}AFX_DATA_INIT
 161 }
 162 
 163 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
 164 {
 165     CDialog::DoDataExchange(pDX);
 166     //{{AFX_DATA_MAP(CAboutDlg)
 167     //}}AFX_DATA_MAP
 168 }
 169 
 170 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 171     //{{AFX_MSG_MAP(CAboutDlg)
 172         // No message handlers
 173     //}}AFX_MSG_MAP
 174 END_MESSAGE_MAP()
 175 
 176 /////////////////////////////////////////////////////////////////////////////
 177 // CWowlauDlg dialog
 178 
 179 CWowlauDlg::CWowlauDlg(CWnd* pParent /*=NULL*/)
 180     : CDialog(CWowlauDlg::IDD, pParent)
 181 {
 182     //{{AFX_DATA_INIT(CWowlauDlg)
 183 
 184     //}}AFX_DATA_INIT
 185     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 186     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 187     m_dwPos = 0;
 188 }
 189 
 190 void CWowlauDlg::DoDataExchange(CDataExchange* pDX)
 191 {
 192     CDialog::DoDataExchange(pDX);
 193     //{{AFX_DATA_MAP(CWowlauDlg)
 194     DDX_Control(pDX, IDC_COMBO3, m_Combo);
 195     //}}AFX_DATA_MAP
 196 }
 197 
 198 BEGIN_MESSAGE_MAP(CWowlauDlg, CDialog)
 199     //{{AFX_MSG_MAP(CWowlauDlg)
 200     ON_WM_SYSCOMMAND()
 201     ON_WM_PAINT()
 202     ON_WM_QUERYDRAGICON()
 203     ON_BN_CLICKED(IDWRITE, OnWrite)
 204     ON_BN_CLICKED(IDREAD, OnRead)
 205     ON_BN_CLICKED(IDTEST, OnTest)
 206     ON_BN_CLICKED(IDADD, OnAdd)
 207     ON_BN_CLICKED(IDDEL, OnDel)
 208     ON_BN_CLICKED(IDWRITE2, OnWrite2)
 209     ON_BN_CLICKED(IDRUN, OnRun)
 210     ON_BN_CLICKED(ID_wowmyfan, Onwowmyfan)
 211     ON_BN_CLICKED(ID_189, On189)
 212     ON_WM_TIMER()
 213 //    ON_BN_CLICKED(ID_AUTOLOGIN, OnAutologin)
 214     //}}AFX_MSG_MAP
 215 END_MESSAGE_MAP()
 216 
 217 /////////////////////////////////////////////////////////////////////////////
 218 // CWowlauDlg message handlers
 219 
 220 BOOL CWowlauDlg::OnInitDialog()
 221 {
 222     CDialog::OnInitDialog();
 223 
 224     // Add "About..." menu item to system menu.
 225 
 226     // IDM_ABOUTBOX must be in the system command range.
 227     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 228     ASSERT(IDM_ABOUTBOX < 0xF000);
 229 
 230     CMenu* pSysMenu = GetSystemMenu(FALSE);
 231     if (pSysMenu != NULL)
 232     {
 233         CString strAboutMenu;
 234         strAboutMenu.LoadString(IDS_ABOUTBOX);
 235         if (!strAboutMenu.IsEmpty())
 236         {
 237             pSysMenu->AppendMenu(MF_SEPARATOR);
 238             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
 239         }
 240     }
 241 
 242     // Set the icon for this dialog.  The framework does this automatically
 243     //  when the application's main window is not a dialog
 244     SetIcon(m_hIcon, TRUE);            // Set big icon
 245     SetIcon(m_hIcon, FALSE);        // Set small icon
 246     
 247     // TODO: Add extra initialization here
 248 //    GetDlgItem(IDC_COMBO2) -> SetWindowText("請選擇"); 
 249         
 250 //    m_COMBO1.AddString("StringData1");
 251 //    ((CComboBox*)GetDlgItem(IDC_COMBO2)) -> AddString("COM1");
 252 //    ((CComboBox*)GetDlgItem(IDC_COMBO2)) -> AddString("COM2");
 253 
 254     
 255     return TRUE;  // return TRUE  unless you set the focus to a control
 256 }
 257 
 258 void CWowlauDlg::OnSysCommand(UINT nID, LPARAM lParam)
 259 {
 260     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 261     {
 262         CAboutDlg dlgAbout;
 263         dlgAbout.DoModal();
 264     }
 265     else
 266     {
 267         CDialog::OnSysCommand(nID, lParam);
 268     }
 269 }
 270 
 271 // If you add a minimize button to your dialog, you will need the code below
 272 //  to draw the icon.  For MFC applications using the document/view model,
 273 //  this is automatically done for you by the framework.
 274 
 275 void CWowlauDlg::OnPaint() 
 276 {
 277     if (IsIconic())
 278     {
 279         CPaintDC dc(this); // device context for painting
 280 
 281         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
 282 
 283         // Center icon in client rectangle
 284         int cxIcon = GetSystemMetrics(SM_CXICON);
 285         int cyIcon = GetSystemMetrics(SM_CYICON);
 286         CRect rect;
 287         GetClientRect(&rect);
 288         int x = (rect.Width() - cxIcon + 1) / 2;
 289         int y = (rect.Height() - cyIcon + 1) / 2;
 290 
 291         // Draw the icon
 292         dc.DrawIcon(x, y, m_hIcon);
 293     }
 294     else
 295     {
 296         CDialog::OnPaint();
 297     }
 298 }
 299 
 300 // The system calls this to obtain the cursor to display while the user drags
 301 //  the minimized window.
 302 HCURSOR CWowlauDlg::OnQueryDragIcon()
 303 {
 304     return (HCURSOR) m_hIcon;
 305 }
 306 
 307 void CWowlauDlg::OnWrite() 
 308 {
 309 // TODO: 在此添加控件通知處理程序代碼
 310     /*CString filename = _T("I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf");//在本例中路徑為:E:\work\BruceXu\CodeRelevant\mytestcode\讀寫txt\ReadWriteTxt\ReadWriteTxt\Test.txt
 311     CString strWriteData, str, str1 = "SET realmlist ", str2;
 312     ((CEdit*)GetDlgItem(IDC_EDIT1)) -> GetWindowText(str2);
 313     //((CEdit*)GetDlgItem(IDC_EDIT1)) -> SetWindowText("lalalala");
 314 
 315     CStdioFile myFile;
 316     myFile.ReadString(str);
 317     int nPos = str.Find(' ');
 318     nPos = str.Find(' ', nPos+1);
 319     CString strRight = str.Right(str.GetLength()-nPos-1);
 320 
 321     strWriteData.Format(strRight);
 322     CStdioFile csFile;
 323     CFileException cfException;
 324     if (csFile.Open(filename, CFile::typeText | CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate, &cfException))//以txt方式讀取|若沒有文件則創建該文件|文件打開時不清除
 325     {
 326         csFile.SeekToEnd();
 327         setlocale(LC_CTYPE, "chs");//為了能用csFile.WriteString(strWriteData)寫入中文
 328         csFile.WriteString(strWriteData);
 329     }
 330     
 331     csFile.Close();    */
 332     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 333     CString strWriteData, str = "SET realmlist ", str1, str2;
 334     CStdioFile myFile;
 335     ((CEdit*)GetDlgItem(IDC_EDIT1)) -> GetWindowText(str1);
 336     str2 = str + str1;
 337     strWriteData.Format(str2);
 338     CFileException fileException;
 339     if(myFile.Open(pszFileName,CFile::typeText | CFile::modeCreate | CFile::modeReadWrite),&fileException)
 340     {
 341         myFile.SeekToEnd();
 342         setlocale(LC_CTYPE, "chs");//為了能用csFile.WriteString(strWriteData)寫入中文
 343         myFile.WriteString(strWriteData);
 344     }
 345 
 346     myFile.Close();
 347 }
 348 /*{
 349 
 350 CString strFilter,fileName;
 351 
 352 strFilter="Text Files(*.txt)|*.txt||";
 353 
 354 CFileDialog dlg(TRUE, NULL, NULL, OFN_EXPLORER|OFN_HIDEREADONLY|OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
 355 
 356 if(dlg.DoModal() == IDOK )//顯示打開文件對話框
 357 
 358 {
 359 
 360 fileName=dlg.GetPathName();
 361 
 362 CFile openfile(fileName,CFile::modeRead);//構造CFile對象
 363 
 364 int length=openfile.GetLength();//獲取文件長度
 365 
 366 char *strText;
 367 
 368 strText=new char[length];
 369 
 370 openfile.Read(strText,length);
 371 
 372 openfile.Close();//關閉文件
 373 
 374 m_strRead=(CString)strText;
 375 
 376 delete [] strText;
 377 
 378 UpdateData(false);
 379 
 380 }8*/
 381 void CWowlauDlg::OnRead() 
 382 {
 383     // TODO: Add your control notification handler code here
 384     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 385     CString filename = _T("I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf");//在本例中路徑為:E:\work\BruceXu\CodeRelevant\mytestcode\讀寫txt\ReadWriteTxt\ReadWriteTxt\Test.txt
 386     CStdioFile myFile;
 387     CString strWriteData, str2, MyStr;
 388     strWriteData.Format(str2);
 389     CFileException fileException;
 390     if(myFile.Open(pszFileName,CFile::typeText|CFile::modeReadWrite),&fileException)
 391     {
 392         CString str2;
 393         myFile.ReadString(str2);
 394         //AfxMessageBox(str1+str2);
 395         //SetDlgItemText(IDC_STATIC, str1 + str2);
 396         SetDlgItemText(IDC_EDIT1, str2);    // 這里str2的作用域只在這對大括號里,不能在大括號外
 397         MyStr.Format(str2);        // 格式字符串,並賦值給 MyStr,這樣,str2的作用域就在這個::OnRead()整個片區,這樣下面的 AfxMessageBox(MyStr); 就可以訪問 讀出的字符串了,不加這句,讀出的是亂碼
 398     }
 399     else
 400     {
 401         TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
 402     }
 403     
 404     myFile.Close();     
 405     
 406 }
 407 
 408 void CWowlauDlg::OnTest() 
 409 {
 410     // TODO: Add your control notification handler code here
 411     CString str1, str2, m_str = "123456";
 412     ((CEdit*)GetDlgItem(IDC_EDIT1)) -> GetWindowText(str1);
 413     ((CEdit*)GetDlgItem(IDC_EDIT1)) -> SetWindowText("SET realmlist ptr.darrowshire.com");
 414     ((CComboBox*)GetDlgItem(IDC_COMBO3))->GetWindowText(str2);
 415 
 416 //    AfxMessageBox(str2);
 417     //能使用str==NULL來判斷,NULL一般只拿和指針做比較或者賦給指針,string是類,傳參進函數時str調用默認的構造函數已經初始化了,並且str都已經是對象了,它不可能為NULL,也不能和NULL比較。
 418     if (TRUE == str2.IsEmpty()) // 判斷字符串是否為空,不能用 str == NULL ,寫法是錯誤的,可能會報空指針異常。可以用 str = "",或者sizeof(str2)==0或者 0 == str2.GetLength()
 419         {
 420         AfxMessageBox("請選擇服務器...");
 421         }
 422     else
 423         AfxMessageBox(str2);
 424     CWnd *pwnd = FindWindow(NULL, _T("魔獸世界")); 
 425 //    CWnd *pwnd = FindWindow(_T("Notepad"),_T("無標題 - 記事本"));
 426     HWND hwnd =pwnd->GetSafeHwnd(); //得到它的HWND
 427     char str_id[] = "myfanwow";
 428     char str_psw[] = "123456";
 429     m_str = "123456";
 430     CString *msg = new CString("wowmyfan");
 431     CString cstr = _T("Helloo");
 432     
 433 //    Sleep(5000);
 434 
 435 //    keybd_event('F',0,0,0);
 436 //    keybd_event('F',0,2,0);
 437     if(hwnd)
 438     {
 439 //        HWND hEdit = FindWindowEx(hwnd, NULL, "Edit", NULL);
 440 //        if(hEdit)//have found!
 441         //use it !for example:
 442 //            ::PostMessage(hEdit, WM_SENDSTRING, (LPARAM)msg, 0);
 443         for (int i =0; i < sizeof(str_id); i++)
 444         {
 445             ::SendMessage(hwnd, WM_CHAR, str_id[i], 0);;//發送‘A’到notepad
 446 //                ::SendMessage(hEdit, WM_SETTEXT, 0, (WPARAM)(LPCTSTR)cstr);//這個可用
 447 //            ::SendMessage(hEdit, WM_SETTEXT, 0, (WPARAM)(LPCTSTR)cstr);//這個可用
 448             Sleep(500);
 449         }
 450         ::PostMessage(hwnd, WM_CHAR, 9, 0);
 451         
 452     /*    for (int j = 0; j < sizeof(str_psw); j ++)
 453         {
 454             ::PostMessage(hEdit, WM_CHAR, str_psw[j], 0);
 455             Sleep(500);
 456         }*/
 457         
 458     }
 459     
 460 
 461 
 462 //    ::SendMessage(hwnd, WM_CLOSE, 0, 0);  //這個有效
 463 
 464 
 465 
 466 }
 467 
 468 
 469 
 470 void  CWowlauDlg::OnAdd() 
 471 {
 472     // TODO: Add your control notification handler code here
 473     /*
 474     TCHAR ws[256];
 475     CString temp, strTemp;
 476     CComboBox* pcombo = ((CComboBox*)GetDlgItem(IDC_COMBO3));
 477     ((CComboBox*)GetDlgItem(IDC_COMBO3)) -> GetWindowText((LPTSTR)ws, 256);//可以不帶 LPTSTR
 478 //    ((CComboBox*)GetDlgItem(IDC_COMBO3))->GetLBText(IDC_COMBO3, strTemp);
 479 //    pcombo->GetWindowText(strTemp);
 480     int pos = pcombo -> GetCount();
 481     int flag;
 482     char str[25];
 483     for (int i =0; i < pos; i ++)
 484     {
 485         pcombo->GetLBText(i, strTemp); 
 486         if ( ws == strTemp)
 487         {
 488             flag = 1;
 489             break;
 490         }
 491         else
 492         {
 493             flag = 0;
 494         }
 495     }
 496 
 497     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 498     if (flag == 0)
 499     {
 500         AfxMessageBox(ws);
 501         pcombo->AddString(ws);
 502         AfxMessageBox(itoa(flag, (LPSTR)(LPCTSTR)temp, 10));    
 503         pos++;
 504         UpdateData(TRUE);
 505     }
 506     else
 507     {
 508         AfxMessageBox("數據重復");
 509     }
 510     */
 511 
 512     TCHAR ws[256];
 513     CString temp, strTemp, strWriteData, str = "SET realmlist ", str1, str2;;
 514 
 515     CStdioFile myFile;
 516     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 517     CComboBox* pcombo = ((CComboBox*)GetDlgItem(IDC_COMBO3));
 518     ((CComboBox*)GetDlgItem(IDC_COMBO3)) -> GetWindowText((LPTSTR)ws, 256);//可以不帶 LPTSTR
 519 //    ((CComboBox*)GetDlgItem(IDC_COMBO3))->GetLBText(IDC_COMBO3, strTemp);
 520 //    pcombo->GetWindowText(strTemp);
 521     int pos = pcombo -> GetCount();
 522     int flag;
 523 //    char str[25];
 524     for (int i =0; i < pos; i ++)
 525     {
 526         pcombo->GetLBText(i, strTemp); 
 527         if ( ws == strTemp)
 528         {
 529             flag = 1;
 530             break;
 531         }
 532         else
 533         {
 534             flag = 0;
 535         }
 536     }
 537 
 538     
 539     if (flag == 0)
 540     {
 541         AfxMessageBox(ws);
 542         pcombo->AddString(ws);
 543         AfxMessageBox(itoa(flag, (LPSTR)(LPCTSTR)temp, 10));    
 544         pos++;
 545         UpdateData(TRUE);
 546     }
 547     else
 548     {
 549         AfxMessageBox("數據重復");
 550     }
 551     SetDlgItemText(IDC_STATIC, (itoa(pos, (LPSTR)(LPCTSTR)strTemp, 10)));
 552                 
 553 //                    AfxMessageBox(ws);
 554         
 555 //    ((CComboBox*)GetDlgItem(IDC_COMBO3)) -> AddString("login.ymwow.com");
 556 //    AfxMessageBox(ws);
 557 
 558     
 559 }
 560 
 561 void CWowlauDlg::OnDel() 
 562 {
 563     // TODO: Add your control notification handler code here
 564     UpdateData(true);
 565     TCHAR ws[256];
 566     
 567     CComboBox* pcombo = ((CComboBox*)GetDlgItem(IDC_COMBO3));
 568     int pos = pcombo -> GetCurSel();
 569     ((CComboBox*)GetDlgItem(IDC_COMBO3)) -> GetWindowText(ws, 256);//可以不帶 LPTSTR
 570     ((CComboBox*)GetDlgItem(IDC_COMBO3)) -> DeleteString(pos);
 571     
 572 }
 573 
 574 void CWowlauDlg::OnWrite2() 
 575 {
 576     // TODO: Add your control notification handler code here
 577 
 578     TCHAR ws[256];
 579     CString temp, strTemp, strWriteData, str = "SET realmlist ", str1, str2;;
 580 
 581     CStdioFile myFile;
 582     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 583     CComboBox* pcombo = ((CComboBox*)GetDlgItem(IDC_COMBO3));
 584     ((CComboBox*)GetDlgItem(IDC_COMBO3)) -> GetWindowText((LPTSTR)ws, 256);//可以不帶 LPTSTR
 585 //    ((CComboBox*)GetDlgItem(IDC_COMBO3))->GetLBText(IDC_COMBO3, strTemp);
 586 //    pcombo->GetWindowText(strTemp);
 587     int pos = pcombo -> GetCount();
 588 //    int flag;
 589 //    char str[25];
 590 /*    for (int i =0; i < pos; i ++)
 591     {
 592         pcombo->GetLBText(i, strTemp); 
 593         if ( ws == strTemp)
 594         {
 595             flag = 1;
 596             break;
 597         }
 598         else
 599         {
 600             flag = 0;
 601         }
 602     }
 603 
 604     
 605     if (flag == 0)
 606     {
 607         AfxMessageBox(ws);
 608         pcombo->AddString(ws);
 609         AfxMessageBox(itoa(flag, (LPSTR)(LPCTSTR)temp, 10));    
 610         pos++;
 611         UpdateData(TRUE);*/
 612 
 613         strWriteData.Format(ws);
 614         CFileException fileException;
 615 
 616         if(myFile.Open(pszFileName,CFile::typeText | CFile::modeCreate | CFile::modeReadWrite),&fileException)
 617         {
 618             myFile.SeekToEnd();
 619             setlocale(LC_CTYPE, "chs");//為了能用csFile.WriteString(strWriteData)寫入中文
 620             myFile.WriteString(str + strWriteData);
 621         }
 622 
 623         myFile.Close();
 624 /*    }
 625     else
 626     {
 627         AfxMessageBox("數據重復");
 628     }
 629     */
 630 }
 631 
 632 void CWowlauDlg::OnRun() 
 633 {
 634     // TODO: Add your control notification handler code here
 635     TCHAR ws[256];
 636     CString temp, strTemp, strWriteData, str = "SET realmlist ", str1, str2;;
 637 
 638     CStdioFile myFile;
 639     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 640     CComboBox* pcombo = ((CComboBox*)GetDlgItem(IDC_COMBO3));
 641     ((CComboBox*)GetDlgItem(IDC_COMBO3)) -> GetWindowText((LPTSTR)ws, 256);//可以不帶 LPTSTR/*(1)取當前內容*/
 642     strTemp.Format(ws);
 643     if ("" == strTemp)
 644     {
 645         AfxMessageBox("請選擇服務器...");
 646     }
 647     else
 648     {
 649     strWriteData.Format(ws);
 650         CFileException fileException;
 651 
 652         if((myFile.Open(pszFileName,CFile::typeText | CFile::modeCreate | CFile::modeReadWrite),&fileException) && ws != NULL)
 653         {
 654             myFile.SeekToEnd();
 655             setlocale(LC_CTYPE, "chs");//為了能用csFile.WriteString(strWriteData)寫入中文
 656             myFile.WriteString(str + strWriteData);
 657         }
 658         CWnd *pwnd = FindWindowA(NULL, _T("魔獸世界")); 
 659 //        CWnd *pwnd = FindWindow(_T("Notepad"),NULL);
 660         if (!pwnd) 
 661         { 
 662             PROCESS_INFORMATION lpProcessInformation; 
 663             STARTUPINFO startupinfo; 
 664             memset(&startupinfo,0,sizeof(STARTUPINFO)); 
 665             memset(&lpProcessInformation,0,sizeof(PROCESS_INFORMATION)); 
 666             startupinfo.cb = sizeof(STARTUPINFO); 
 667             startupinfo.dwFlags = STARTF_USESHOWWINDOW; 
 668             startupinfo.wShowWindow = SW_SHOWMAXIMIZED;
 669             startupinfo.dwX = 100; 
 670             startupinfo.dwY = 100; 
 671             startupinfo.lpDesktop = NULL; 
 672             startupinfo.lpReserved = NULL; 
 673             startupinfo.lpReserved2 = NULL; 
 674             startupinfo.lpTitle = NULL; 
 675 //            CreateProcess(_T("C:\\Windows\\System32\\notepad.exe"), NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupinfo, &lpProcessInformation); 
 676             CreateProcess(_T("I:\\BaiduYunDownload\\wowold.net\\wowold.net\\wow.exe"), NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupinfo, &lpProcessInformation); 
 677         }
 678                 
 679         myFile.Close();
 680     }
 681 //    ShellExecute(NULL, "open", "WoW.exe", NULL, "I:\\BaiduYunDownload\\wowold.net\\wowold.net", SW_SHOWMAXIMIZED);
 682 //    ShellExecute(NULL, "open", "I:\\BaiduYunDownload\\wowold.net\\wowold.net\\WoW.exe", NULL, NULL, SW_SHOWMAXIMIZED);
 683       //打開指定exe程序,並傳遞命令行參數
 684 /*    SHELLEXECUTEINFO ShExecInfo;  
 685     ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);  
 686     ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;  
 687     ShExecInfo.hwnd = this -> m_hWnd;  
 688     ShExecInfo.lpVerb = _T("open");  
 689     //輸入要調用的exe文件路徑  
 690     ShExecInfo.lpFile = _T("WoW.exe");   
 691     //傳入命令行參數數據  
 692     ShExecInfo.lpParameters = NULL; //若沒有命令行參數,可為NULL  */
 693   
 694     /*/------------------exe文件在當前路徑下(可忽略)--------------  
 695     //獲取當前程序路徑  
 696     TCHAR exeFullPath[200];  
 697     GetModuleFileName(NULL,exeFullPath,100);  
 698     //獲取程序運行當前路徑  
 699     TCHAR drive[100],dir[100],fname[100],ext[20];  
 700     _wsplitpath(exeFullPath,drive,dir,fname,ext);  
 701     CString dirPath;  
 702     dirPath.Format(_T("%s%s"),drive,dir);  */
 703     //------------------------------------------------------------  
 704   
 705   /*  ShExecInfo.lpDirectory = "I:\\BaiduYunDownload\\wowold.net\\wowold.net";//這里exe的目錄可忽略,寫為NULL  
 706     ShExecInfo.nShow = SW_SHOWMAXIMIZED;//這里設置為顯示exe界面,若設置為SW_SHOW,則可以顯示exe界面  
 707     ShExecInfo.hInstApp = NULL;   
 708     CWowlauDlg::ShowWindow(SW_MINIMIZE);//點擊運行后該窗口最小化到任何欄
 709     //調用exe程序  
 710     ShellExecuteEx(&ShExecInfo);  */
 711     //關閉該exe程序  
 712    /* if( ShExecInfo.hProcess != NULL)  
 713     {  
 714         //等待程序運行完畢  
 715         WaitForSingleObject(ShExecInfo.hProcess, INFINITE);  
 716         //關閉程序  
 717         TerminateProcess(ShExecInfo.hProcess,0);  
 718         ShExecInfo.hProcess = NULL;  
 719     }  
 720 */
 721 
 722 
 723 }
 724 
 725 void CWowlauDlg::Onwowmyfan() 
 726 {
 727     // TODO: Add your control notification handler code here
 728     CWnd *pwnd = FindWindow(NULL, _T("魔獸世界")); 
 729 //    CWnd *pwnd = FindWindow(_T("Notepad"),_T("無標題 - 記事本"));
 730     HWND hwnd =pwnd->GetSafeHwnd(); //得到它的HWND
 731     char str_id[] = "wowmyfan";
 732     char str_psw[] = "123456";
 733     CString *msg = new CString("wowmyfan");
 734     CString cstr = _T("Helloo");
 735     char str_id_h[] = "wowmyfan";
 736     char *p;
 737     p = &str_id_h[0];
 738     char *p2 = new char[6];
 739     p2[6] = ('h', 's', 'o', 'f', 't', 'k');
 740 //    Sleep(5000);
 741 
 742 //    keybd_event('F',0,0,0);
 743 //    keybd_event('F',0,2,0);
 744     if(hwnd)
 745     {
 746         for (int i =0; i < sizeof(str_id_h); i++)
 747         {
 748             ::SendMessage(hwnd, WM_CHAR, p[i], 0);;//發送‘A’到notepad
 749 //                ::SendMessage(hEdit, WM_SETTEXT, 0, (WPARAM)(LPCTSTR)cstr);//這個可用
 750 
 751             Sleep(250);
 752         }
 753         //按下TAB
 754         ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
 755         Sleep(250);
 756         delete []p;
 757         for (int j = 0; j < sizeof(str_psw); j ++)
 758             {
 759                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
 760                 Sleep(250);
 761             }
 762     }
 763     ::PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
 764 }
 765 
 766 void CWowlauDlg::On189() 
 767 {
 768     // TODO: Add your control notification handler code here
 769     int flag = 1, a = 0, o = 0, sum = 0, i = 1;
 770     CString temp;
 771     CWnd *pwnd = FindWindow(NULL, _T("魔獸世界")); 
 772 
 773 //    CWnd *pwnd = FindWindow(_T("Notepad"),_T("無標題 - 記事本"));
 774     HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND
 775     char str_id[] = "myfanwow";
 776     char str_psw[] = "123456";
 777     CString *msg = new CString("wowmyfan");
 778     CString cstr = _T("Helloo");
 779 //    bool twnd = ::IsWindowVisible(hwnd);
 780 
 781 
 782 /*    keybd_event('F',0,0,0);
 783 //    keybd_event('F',0,2,0);
 784     if(::IsWindowVisible(hwnd))
 785     {
 786         for (int i =0; i < sizeof(str_id); i++)
 787         {
 788             ::SendMessage(hwnd, WM_CHAR, str_id[i], 0);;//發送‘A’到notepad
 789 //                ::SendMessage(hEdit, WM_SETTEXT, 0, (WPARAM)(LPCTSTR)cstr);//這個可用
 790             fflush(stdout);
 791             Sleep(250);
 792         }
 793         ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
 794         Sleep(500);
 795         
 796         for (int j = 0; j < sizeof(str_psw); j ++)
 797             {
 798                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
 799                 Sleep(250);
 800             }
 801     }
 802     ::PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);*/
 803 
 804 
 805 //    SetDlgItemText(IDC_STATIC, (itoa(sum, (LPSTR)(LPCTSTR)temp, 10)));
 806     SetTimer(1, 1000, NULL);    
 807 //    SetTimer(2, 1000, NULL);
 808 
 809 }
 810 
 811 /*/可用的
 812 
 813 void CWowlauDlg::OnTimer(UINT nIDEvent) 
 814 {
 815     // TODO: Add your message handler code here and/or call default
 816 
 817     /* 這里一個簡單的計時器代碼
 818     static int nTimer=0;    
 819     CString str="";    
 820     str.Format("Timer:    %d",nTimer++);    
 821     CWnd *pWnd=GetDlgItem(IDC_STATIC);    //獲取靜態文本的指針
 822     pWnd->SetWindowText(str); //在Lable中設置新值,表明定時器已經工作。
 823     
 824 
 825     HANDLE g_event = CreateEvent(NULL,TRUE,FALSE,NULL);
 826     CString MyStr;
 827     char str_id[] = "myfanwow";
 828     char str_psw[] = "123456";
 829     CString *msg = new CString("wowmyfan");
 830     CString cstr = _T("Helloo"), my_str;
 831     
 832 
 833     CWnd *pwnd = FindWindow(NULL, _T("魔獸世界")); 
 834     HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND
 835     int flag = 0;
 836     if(::IsWindowVisible(hwnd))
 837     {
 838         flag = 1;
 839         MyStr.Format("%d  窗口啟動成功", flag);
 840         KillTimer(1);  
 841     }
 842     else
 843     {
 844         flag = 0;
 845         MyStr.Format("%d  窗口未啟動,請稍候...", flag);        
 846     }
 847 
 848 //    SetDlgItemInt(IDC_STATIC, flag);
 849     SetDlgItemText(IDC_STATIC, MyStr);
 850 //    DWORD id;
 851     
 852 //SendMessage(hwnd, WM_CHAR, (WPARAM)&str,(LPARAM)0 );
 853     if(flag == 1)
 854         {
 855             for (int i =0; i < sizeof(str_id); i++)
 856             {
 857                 ::SendMessage(hwnd, WM_CHAR, str_id[i], 0);;//發送‘A’到notepad
 858     //                ::SendMessage(hEdit, WM_SETTEXT, 0, (WPARAM)(LPCTSTR)cstr);//這個可用
 859     //            fflush(stdout);
 860                 XSleep(250);    //用 XSleep(); 不會阻塞線程,其它,多線程、用定時器等,都會阻塞
 861             //    ShowCount3();
 862     //            this -> EnableWindow(TRUE);
 863             }
 864                 ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
 865 
 866                 ShowCount2();
 867                 //HANDLE hTread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)ShowCount1,0,0,&id);
 868                 //CloseHandle(hTread);
 869             //    WaitForSingleObject(g_event,1000);// 和上面的 HANDLE g_event = CreateEvent(NULL,TRUE,FALSE,NULL); 代碼一起等同於 Sleep(1000);
 870 
 871             for (int j = 0; j < sizeof(str_psw); j ++)
 872             {
 873                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
 874                 XSleep(250);
 875                 //ShowCount3();
 876             }
 877         }
 878     ::PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
 879     
 880     CDialog::OnTimer(nIDEvent);
 881 }*/
 882 /////////////////////////////////////////
 883 
 884 void CWowlauDlg::OnTimer(UINT nIDEvent) 
 885 {
 886     // TODO: Add your message handler code here and/or call default
 887     CWnd *pwnd = FindWindow(NULL, _T("魔獸世界")); 
 888     HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND
 889     int flag = 0;
 890     CString MyStr;
 891     char str_id1[] = "wowmyfan", str_id2[] ="myfanwow" , str_id3[] = "mywowfan", str_psw[] = "123456";
 892 
 893     SInfo* info = new SInfo();
 894     info->strInfo = "wowmyfan";
 895 
 896     if(::IsWindowVisible(hwnd))
 897     {
 898         flag = 1;
 899         MyStr.Format("%d  窗口啟動成功", flag);
 900         KillTimer(1);  
 901     }
 902     else
 903     {
 904         flag = 0;
 905         MyStr.Format("%d  窗口未啟動,請稍候...", flag);        
 906     }    
 907 
 908     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 909     CString filename = _T("I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf");//在本例中路徑為:E:\work\BruceXu\CodeRelevant\mytestcode\讀寫txt\ReadWriteTxt\ReadWriteTxt\Test.txt
 910     CStdioFile myFile;
 911     CString strWriteData, str2, WtfStr;
 912     strWriteData.Format(str2);
 913     CFileException fileException;
 914     if(myFile.Open(pszFileName,CFile::typeText|CFile::modeReadWrite),&fileException)
 915     {
 916         CString str2;
 917         myFile.ReadString(str2);
 918         //AfxMessageBox(str1+str2);
 919         //SetDlgItemText(IDC_STATIC, str1 + str2);
 920         SetDlgItemText(IDC_EDIT1, str2);    // 這里str2的作用域只在這對大括號里,不能在大括號外
 921         WtfStr.Format(str2);        // 格式字符串,並賦值給 MyStr,這樣,str2的作用域就在這個::OnRead()整個片區,這樣下面的 AfxMessageBox(MyStr); 就可以訪問 讀出的字符串了,不加這句,讀出的是亂碼
 922     }
 923     else
 924     {
 925         TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
 926     }
 927     myFile.Close();
 928     if (flag == 1)
 929     {
 930         if (strcmp(WtfStr, "SET realmlist login.ymwow.com") == 0)
 931         {
 932             for (int i = 0; i < sizeof(str_id1); i ++)
 933             {
 934                 ::SendMessage(hwnd, WM_CHAR, str_id1[i], 0);
 935                 XSleep(250);    //用 XSleep(); 不會阻塞線程,其它,多線程、用定時器等,都會阻塞
 936             }
 937             ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
 938             ShowCount2();
 939             for (int j = 0; j < sizeof(str_psw); j ++)
 940             {
 941                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
 942                 XSleep(250);
 943             }
 944         }
 945         else if (strcmp(WtfStr, "SET realmlist wow.newow.org") == 0)
 946         {
 947             for (int i = 0; i < sizeof(str_id2); i ++)
 948             {
 949                 ::SendMessage(hwnd, WM_CHAR, str_id2[i], 0);
 950                 XSleep(250);    //用 XSleep(); 不會阻塞線程,其它,多線程、用定時器等,都會阻塞
 951             }
 952             ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
 953             ShowCount2();
 954             for (int j = 0; j < sizeof(str_psw); j ++)
 955             {
 956                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
 957                 XSleep(250);
 958             }
 959         }
 960         else if (strstr(WtfStr, "1.14.76.54")!= NULL)
 961         {
 962             for (int i = 0; i < sizeof(str_id3); i ++)
 963             {
 964                 ::SendMessage(hwnd, WM_CHAR, str_id3[i], 0);
 965                 XSleep(250);    //用 XSleep(); 不會阻塞線程,其它,多線程、用定時器等,都會阻塞
 966             }
 967             ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
 968             ShowCount2();
 969             for (int j = 0; j < sizeof(str_psw); j ++)
 970             {
 971                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
 972                 XSleep(250);
 973             }
 974         }
 975     }
 976     XSleep(250);
 977     ::PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
 978 //    AfxMessageBox(WtfStr);
 979     SetDlgItemText(IDC_STATIC, MyStr);
 980     CDialog::OnTimer(nIDEvent);
 981 }
 982 
 983 
 984 void CWowlauDlg::OnAutologin() 
 985 {
 986     // TODO: Add your control notification handler code here
 987     char* pszFileName="I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf";
 988     CString filename = _T("I:\\BaiduYunDownload\\wowold.net\\wowold.net\\realmlist.wtf");//在本例中路徑為:E:\work\BruceXu\CodeRelevant\mytestcode\讀寫txt\ReadWriteTxt\ReadWriteTxt\Test.txt
 989     CStdioFile myFile;
 990     CString strWriteData, str2, MyStr;
 991 
 992     strWriteData.Format(str2);
 993     CFileException fileException;
 994     if(myFile.Open(pszFileName,CFile::typeText|CFile::modeReadWrite),&fileException)
 995     {
 996         CString str2;
 997         myFile.ReadString(str2);
 998         SetDlgItemText(IDC_EDIT1, str2);
 999     }
1000     else
1001     {
1002         TRACE("Can't open file %s,error=%u\n",pszFileName,fileException.m_cause);
1003     }
1004     myFile.Close();       
1005     
1006     char str_id[] = "myfanwow";
1007     char str_psw[] = "123456";
1008     SInfo* msg = new SInfo();
1009     msg -> strInfo = "wowmyfan";
1010     char MyId[] = "wowmyfan";
1011     CWnd *pwnd = FindWindow(NULL, _T("魔獸世界")); 
1012     HWND hwnd = pwnd->GetSafeHwnd(); //得到它的HWND
1013     int flag = 0;
1014     if(::IsWindowVisible(hwnd))
1015     {
1016         flag = 1;
1017         MyStr.Format("%d  窗口啟動成功", flag);
1018         KillTimer(1);  
1019     }
1020     else
1021     {
1022         flag = 0;
1023         MyStr.Format("%d  窗口未啟動,請稍候...", flag);        
1024     }
1025 
1026     if (flag == 1)
1027     {
1028         if ("login.ymwow.com" == str2)
1029         {
1030             for (int i =0; i < sizeof(MyId); i++)
1031             {
1032                 ::SendMessage(hwnd, WM_CHAR, MyId[i], 0);;//發送‘A’到notepad  (LPARAM)&str
1033                 XSleep(250);    //用 XSleep(); 不會阻塞線程,其它,多線程、用定時器等,都會阻塞
1034             }
1035                 ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
1036                 ShowCount2();
1037 
1038             for (int j = 0; j < sizeof(str_psw); j ++)
1039             {
1040                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
1041                 XSleep(250);
1042             }
1043             XSleep(250);
1044             ::PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
1045         }
1046         if ("wow.newow.org" == str2)
1047         {
1048             for (int i =0; i < sizeof(str_id); i++)
1049             {
1050                 ::SendMessage(hwnd, WM_CHAR, str_id[i], 0);;//發送‘A’到notepad  (LPARAM)&str
1051                 XSleep(250);    //用 XSleep(); 不會阻塞線程,其它,多線程、用定時器等,都會阻塞
1052             }
1053                 ::PostMessage(hwnd, WM_KEYDOWN, VK_TAB, 0);
1054                 ShowCount2();
1055 
1056             for (int j = 0; j < sizeof(str_psw); j ++)
1057             {
1058                 ::PostMessage(hwnd, WM_CHAR, str_psw[j], 0);
1059                 XSleep(250);
1060             }
1061             XSleep(250);
1062             ::PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
1063         }
1064     }
1065 }
View Code

 左邊的控件,是我先拿寫個代碼讀出寫入,本來我的意思是,直接修改SET realmlist 后面的代碼,因為服務器IP前面是一樣的,是SET realmlist,SET realmlist 后面字符串長度也不盡相等,而找了半天網上資料,沒找到有替換文件里的部分字符串的寫入方法,有的只是,讀取文件里所有字符串,或者寫入字符串到文件中。對文件讀寫就用到 myFile.Open(pszFileName,CFile::typeText | CFile::modeCreate | CFile::modeReadWrite),&fileException),基本上就是清空文件然后重新寫入,可能由於我是個剛剛入門,哪位要是有前面替換文件里的基本段字符串的方法,望告之,不盡感謝。

然后左邊實現讀出寫入  realmlist 文件后,就開始使用MFC 的組合框控件開始實現添加刪除服務器IP,簡單的實現添加或刪除下拉列表功能還是比較簡單的,在一個問題上花費了點時間,就是添加IP的時候判斷現在列表中是否已經存在的時候,原代碼為:

 1 for (int i =0; i < pos; i ++)                // 使用 for 循環判斷列是否有重復
 2 {
 3     pcombo->GetLBText(i, strTemp);         // 讀取下拉框的數據保存到 strTemp 中
 4     if ( ws == strTemp)                    // ws 是 需要添加的數據 判斷 需要添加的數據是否和下拉框現有的數據相同,
 5     {
 6         break;                 // 數據相同,退出循環 
 7     }
 8     else
 9     {
10         continne;              // 否則 繼續對比數據
11     }
12 }
13 AfxMessageBox("數據重復");
14 pcombo->AddString(ws);

結果可想而知,有無重復都會寫入數據,於是我想是不是不能用 for 循環,改用 while 、do while,都是在判斷條件的時候出了問題,就是網上找答案,搜索條件循環,終於找到有一條信息,在循環條件里設置一個 flag,於是再修改就得到正確的代碼:

 1 for (int i =0; i < pos; i ++)
 2 {
 3     pcombo->GetLBText(i, strTemp); 
 4     if ( ws == strTemp)
 5     {
 6         flag = 1;        // 判斷 如果有相同的數據,則 flag = 1,
 7         break;
 8     }
 9     else
10     {
11         flag = 0;        // 否則 flag = 0
12     }
13 }
14 
15 // 然后在這里 判斷 flag = 0, 即無重復數據時才寫入數據
16 if (flag == 0)
17 {
18     AfxMessageBox(ws);
19     pcombo->AddString(ws);
20     AfxMessageBox(itoa(flag, (LPSTR)(LPCTSTR)temp, 10));    
21     pos++;                                      // 這里每加一條,列表數就加1
22     UpdateData(TRUE);
23 }
24 else
25 {
26     AfxMessageBox("數據重復");
27 }
28 SetDlgItemText(IDC_STATIC, (itoa(pos, (LPSTR)(LPCTSTR)strTemp, 10)));

添加,刪除,寫入,然后就是運行山口山可執行文件,運行了一段時間都沒有問題。

可是每次運行后都是輸入帳號和密碼,有點煩,於是就想設置一個自動登陸按扭,就不用每次都輸入帳號和密碼了,說干就干,添加一個按扭,自動登陸,思路如下,先找到進程,然后找到輸入帳號和密碼的窗口ID,輸入相應的帳號和密碼回車OK。

想法就好,實現起來又發現了問題,山口山的登陸界面的帳號和密碼框和界面同進程,同ID,這就不好辦了,在網上找資料,找到有的人說山口山的界面是畫上去,不是控件,於是用VC++的 spy來查找進程和 ID,發覺真是這么回事,這就不好辦了,

想想另外辦法,發現  山口山啟動后輸入提示符直接就在帳號輸入框里了,有辦法了,啟動窗口后先寫入帳號字符串,然后模擬鍵盤按下 TAB 鍵, 再輸入密碼字符串,再模擬鍵盤按下回車鍵,OK。於是就找向一個編輯框里輸入字符串的方法,先用記事本試試, 有 SendMessage 方法可以實現,但實驗好多方法,SendMessage方法向編輯框發送字符串,可以用WM_SENDSTRING,WM_SETTEXT這兩個方法,,但怎么用都不能往記事本里發送字符串,倒是能夠改寫窗口的標題。看來是不能用 SendMessage 實現字符串的輸入了,不過發現 SendMessage 的WM_CHAR方法可以輸入字符,這樣也行,大不了就用 for 循環來實現字符串輸入。於是就有了下面兩個按扭,每個按扭對應一個帳號。                                                                                                運行了一兩天沒什么問題,也能夠實現自動登陸了,雖然這個自動登陸缺陷很多,但我這個入門級的能湊合用也不錯了;現在還有需要修改的地方,每次都要等山口山窗口出現后才能點自動登陸,這回要實現自動判斷窗口是否出現,出現則輸入帳號密碼,否則就等待山口山窗口出現,於是開始用了 Sleep();來實現,不過問題又來了, Sleep()老是會阻塞進程,這個真的很煩,有時候字符串輸到一半卡住了,導致另一半不輸入,又到網上找解決辦法,有的說用線程,有的說用 Delay()來實現,說的線程暫時還不大理解,我把輸入帳號和密碼和SLEEP()代碼放到頭文件處,又發現 C++ 沒有 Delay(), 這個命令,不過這都解決不了進程阻塞,還有用

 

 1 void ShowCount1()
 2 {
 3     CString str;
 4     int i = 0;
 5     HWND hwnd = AfxGetApp()->GetMainWnd()->GetSafeHwnd();
 6     while(i < 10)
 7     {
 8         Sleep(20);
 9  //       str.Format(_T("已耗時%d秒,請稍候..."),i+1);
10   //      ::SetWindowText(hwnd,str);
11         i ++;
12   }

來代替 Sleep(),不過都不能解決阻塞的問題,正當以為這個問題無法實現的時候,發現一大神自己寫的,http://www.newxing.com/Code/VC/other/1460.html ,叫“C++ 非阻塞式延時函數XSleep”,用這個代替 Sleep()是完全解決阻塞的問題,完了用一個計時器來實時監控山口山窗口,再用XSleep()實現延時,基本上就實現在自動登陸了,效果圖如下:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM