大智慧或者同花顺插件 EnumWindows keybd_event

来源:互联网 发布:网络三大奇书七大神书 编辑:程序博客网 时间:2024/04/29 17:11

 使用方法举例:

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. typedef  void __stdcall (*sthndl)(char*);
  10. sthndl StartSearch;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13.     : TForm(Owner)
  14. {
  15.     hDLL=LoadLibrary((LPCTSTR)"ZTSearch.dll");
  16.     if(hDLL==NULL){
  17.         exit(1);
  18.     }
  19.     StartSearch = (sthndl)GetProcAddress(hDLL, "StartSearch");
  20. }
  21. //---------------------------------------------------------------------------
  22. void __fastcall TForm1::Button1Click(TObject *Sender)
  23. {
  24.     StartSearch("522001");
  25. }
  26. //---------------------------------------------------------------------------

以下是dll的代码:

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <windows.h>
  4. #pragma hdrstop
  5. //---------------------------------------------------------------------------
  6. //   Important note about DLL memory management when your DLL uses the
  7. //   static version of the RunTime Library:
  8. //
  9. //   If your DLL exports any functions that pass String objects (or structs/
  10. //   classes containing nested Strings) as parameter or function results,
  11. //   you will need to add the library MEMMGR.LIB to both the DLL project and
  12. //   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
  13. //   if any other projects which use the DLL will be performing new or delete
  14. //   operations on any non-TObject-derived classes which are exported from the
  15. //   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
  16. //   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
  17. //   the file BORLNDMM.DLL should be deployed along with your DLL.
  18. //
  19. //   To avoid using BORLNDMM.DLL, pass string information using "char *" or
  20. //   ShortString parameters.
  21. //
  22. //   If your DLL uses the dynamic version of the RTL, you do not need to
  23. //   explicitly add MEMMGR.LIB as this will be done implicitly for you
  24. //---------------------------------------------------------------------------
  25. #pragma argsused
  26. extern "C" __declspec(dllexport)  void  __stdcall StartSearch(char * str);
  27. //---------------------------------------------------------------------------
  28. void __fastcall SearchInWin(HWND handle,char * str);
  29. void __fastcall SetWindowAlpha(HWND handle,bool flag);
  30. void __fastcall DoSearch(HWND mainhandle,HWND edithandle);
  31. BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam);
  32. BOOL CALLBACK FindTHSMainWinProc(HWND hWnd,LPARAM lParam);
  33. //---------------------------------------------------------------------------
  34. char * StockStr = NULL;
  35. bool Find = false;
  36. HWND HTHSMainWin = NULL;
  37. HWND HDZHWin = NULL;
  38. //---------------------------------------------------------------------------
  39. int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
  40. {
  41.     return 1;
  42. }
  43. //---------------------------------------------------------------------------
  44. void __fastcall SearchInWin(HWND handle,char * str)
  45. {
  46.     SetForegroundWindow(handle);
  47.     ShowWindow(handle,SW_SHOW);
  48.     keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY   |   0,0   );
  49.     keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY   |   KEYEVENTF_KEYUP,0);
  50.     char* charofstr = str;
  51.     while(*charofstr!=0){
  52.         keybd_event(*charofstr, MapVirtualKey(*charofstr, 0), 0, 0);
  53.         keybd_event(*charofstr, MapVirtualKey(*charofstr, 0), KEYEVENTF_KEYUP, 0);
  54.         charofstr++;
  55.     }
  56.     keybd_event(VK_RETURN   , 0, 0, 0);
  57.     keybd_event(VK_RETURN   , 0, KEYEVENTF_KEYUP, 0);
  58. }
  59. //---------------------------------------------------------------------------
  60. void __fastcall SetWindowAlpha(HWND handle,bool flag)
  61. {
  62.     long rtn = GetWindowLong(handle, GWL_EXSTYLE);
  63.     rtn = rtn|WS_EX_LAYERED;
  64.     if(flag){
  65.         SetWindowLong(handle, GWL_EXSTYLE, rtn);
  66.         SetLayeredWindowAttributes(handle, 0, 10, LWA_ALPHA);
  67.     }else{
  68.         SetWindowLong(handle, GWL_EXSTYLE, rtn);
  69.         SetLayeredWindowAttributes(handle, 0, 255, LWA_ALPHA);      
  70.     }
  71. }
  72. //---------------------------------------------------------------------------
  73. void __fastcall DoSearch(HWND mainhandle,HWND edithandle)
  74. {
  75.     SendMessage(mainhandle,WM_SETREDRAW,false,0);
  76.     SetWindowAlpha(edithandle,true);
  77.     SearchInWin(mainhandle,StockStr);
  78.     SendMessage(mainhandle,WM_SETREDRAW,true,0);
  79.     Sleep(1000);
  80.     SetWindowAlpha(edithandle,false);
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall ProduceCMDWin(HWND mainhandle)
  84. {
  85.     SendMessage(mainhandle,WM_SETREDRAW,false,0);
  86.     SetForegroundWindow(mainhandle);
  87.     ShowWindow(mainhandle,SW_SHOW);
  88.     keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY   |   0,0   );
  89.     keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY   |   KEYEVENTF_KEYUP,0);
  90.     Sleep(100);
  91.     keybd_event('1', MapVirtualKey('1', 0), 0, 0);
  92.     keybd_event('1', MapVirtualKey('1', 0), KEYEVENTF_KEYUP, 0);
  93.     Sleep(100);
  94.     keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY   |   0,0   );
  95.     keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY   |   KEYEVENTF_KEYUP,0);
  96.     SendMessage(mainhandle,WM_SETREDRAW,true,0);
  97. }
  98. //---------------------------------------------------------------------------
  99. BOOL CALLBACK FindTHSMainWinProc(HWND hWnd,LPARAM lParam)
  100. {
  101.     char windclassname[MAX_PATH];
  102.     GetWindowText(hWnd,windclassname,MAX_PATH);
  103.     if(NULL != strstr(windclassname , "同花顺")){
  104.         HTHSMainWin = hWnd;
  105.         return false;
  106.     }
  107.     return true;
  108. }
  109. //---------------------------------------------------------------------------
  110. BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam)
  111. {
  112.     char windclassname[MAX_PATH];
  113.     GetClassName(hWnd,windclassname,MAX_PATH);
  114.     if(StrComp(windclassname,"Afx:400000:0") == 0){
  115.         RECT wndrect;
  116.         GetWindowRect(hWnd,&wndrect);
  117.         if( (wndrect.right-wndrect.left == 236) && (wndrect.bottom-wndrect.top == 200) ){
  118.             if(HTHSMainWin){
  119.                 DoSearch(HTHSMainWin,hWnd);
  120.                 Find = true;
  121.             }
  122.         }
  123.     }
  124.     if(StrComp(windclassname,"TCommander") == 0){
  125.         if(NULL != FindWindowEx(hWnd, NULL,"TPanel", NULL) ){
  126.             if(HDZHWin){
  127.                 DoSearch(HDZHWin,hWnd);
  128.                 Find = true;
  129.             }
  130.         }
  131.     }
  132. }
  133. //---------------------------------------------------------------------------
  134. void __stdcall StartSearch(char * str)
  135. {
  136.     HTHSMainWin = NULL;
  137.     HDZHWin = NULL;
  138.     Find = false;
  139.     StockStr = str;
  140.     EnumWindows((WNDENUMPROC)FindTHSMainWinProc,0);
  141.     HDZHWin = FindWindow("THJ32Main", NULL);
  142.     if(HTHSMainWin){
  143.         ProduceCMDWin(HTHSMainWin);
  144.     }
  145.     if(HDZHWin){
  146.         ProduceCMDWin(HDZHWin);
  147.     }
  148.     EnumWindows((WNDENUMPROC)EnumWindowsProc,0);
  149.     if(!Find){
  150.         MessageBox(NULL,"您没有运行登录大智慧或者同花顺软件,请先启动并登录大智慧或者同花顺","涨停板敢死队",MB_OK);
  151.     }
  152. }