使用方法举例:
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Unit1.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- typedef void __stdcall (*sthndl)(char*);
- sthndl StartSearch;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- hDLL=LoadLibrary((LPCTSTR)"ZTSearch.dll");
- if(hDLL==NULL){
- exit(1);
- }
- StartSearch = (sthndl)GetProcAddress(hDLL, "StartSearch");
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button1Click(TObject *Sender)
- {
- StartSearch("522001");
- }
- //---------------------------------------------------------------------------
以下是dll的代码:
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <windows.h>
- #pragma hdrstop
- //---------------------------------------------------------------------------
- // Important note about DLL memory management when your DLL uses the
- // static version of the RunTime Library:
- //
- // If your DLL exports any functions that pass String objects (or structs/
- // classes containing nested Strings) as parameter or function results,
- // you will need to add the library MEMMGR.LIB to both the DLL project and
- // any other projects that use the DLL. You will also need to use MEMMGR.LIB
- // if any other projects which use the DLL will be performing new or delete
- // operations on any non-TObject-derived classes which are exported from the
- // DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
- // EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
- // the file BORLNDMM.DLL should be deployed along with your DLL.
- //
- // To avoid using BORLNDMM.DLL, pass string information using "char *" or
- // ShortString parameters.
- //
- // If your DLL uses the dynamic version of the RTL, you do not need to
- // explicitly add MEMMGR.LIB as this will be done implicitly for you
- //---------------------------------------------------------------------------
- #pragma argsused
- extern "C" __declspec(dllexport) void __stdcall StartSearch(char * str);
- //---------------------------------------------------------------------------
- void __fastcall SearchInWin(HWND handle,char * str);
- void __fastcall SetWindowAlpha(HWND handle,bool flag);
- void __fastcall DoSearch(HWND mainhandle,HWND edithandle);
- BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam);
- BOOL CALLBACK FindTHSMainWinProc(HWND hWnd,LPARAM lParam);
- //---------------------------------------------------------------------------
- char * StockStr = NULL;
- bool Find = false;
- HWND HTHSMainWin = NULL;
- HWND HDZHWin = NULL;
- //---------------------------------------------------------------------------
- int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
- {
- return 1;
- }
- //---------------------------------------------------------------------------
- void __fastcall SearchInWin(HWND handle,char * str)
- {
- SetForegroundWindow(handle);
- ShowWindow(handle,SW_SHOW);
- keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY | 0,0 );
- keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
- char* charofstr = str;
- while(*charofstr!=0){
- keybd_event(*charofstr, MapVirtualKey(*charofstr, 0), 0, 0);
- keybd_event(*charofstr, MapVirtualKey(*charofstr, 0), KEYEVENTF_KEYUP, 0);
- charofstr++;
- }
- keybd_event(VK_RETURN , 0, 0, 0);
- keybd_event(VK_RETURN , 0, KEYEVENTF_KEYUP, 0);
- }
- //---------------------------------------------------------------------------
- void __fastcall SetWindowAlpha(HWND handle,bool flag)
- {
- long rtn = GetWindowLong(handle, GWL_EXSTYLE);
- rtn = rtn|WS_EX_LAYERED;
- if(flag){
- SetWindowLong(handle, GWL_EXSTYLE, rtn);
- SetLayeredWindowAttributes(handle, 0, 10, LWA_ALPHA);
- }else{
- SetWindowLong(handle, GWL_EXSTYLE, rtn);
- SetLayeredWindowAttributes(handle, 0, 255, LWA_ALPHA);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall DoSearch(HWND mainhandle,HWND edithandle)
- {
- SendMessage(mainhandle,WM_SETREDRAW,false,0);
- SetWindowAlpha(edithandle,true);
- SearchInWin(mainhandle,StockStr);
- SendMessage(mainhandle,WM_SETREDRAW,true,0);
- Sleep(1000);
- SetWindowAlpha(edithandle,false);
- }
- //---------------------------------------------------------------------------
- void __fastcall ProduceCMDWin(HWND mainhandle)
- {
- SendMessage(mainhandle,WM_SETREDRAW,false,0);
- SetForegroundWindow(mainhandle);
- ShowWindow(mainhandle,SW_SHOW);
- keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY | 0,0 );
- keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
- Sleep(100);
- keybd_event('1', MapVirtualKey('1', 0), 0, 0);
- keybd_event('1', MapVirtualKey('1', 0), KEYEVENTF_KEYUP, 0);
- Sleep(100);
- keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY | 0,0 );
- keybd_event(VK_ESCAPE ,0,KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,0);
- SendMessage(mainhandle,WM_SETREDRAW,true,0);
- }
- //---------------------------------------------------------------------------
- BOOL CALLBACK FindTHSMainWinProc(HWND hWnd,LPARAM lParam)
- {
- char windclassname[MAX_PATH];
- GetWindowText(hWnd,windclassname,MAX_PATH);
- if(NULL != strstr(windclassname , "同花顺")){
- HTHSMainWin = hWnd;
- return false;
- }
- return true;
- }
- //---------------------------------------------------------------------------
- BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM lParam)
- {
- char windclassname[MAX_PATH];
- GetClassName(hWnd,windclassname,MAX_PATH);
- if(StrComp(windclassname,"Afx:400000:0") == 0){
- RECT wndrect;
- GetWindowRect(hWnd,&wndrect);
- if( (wndrect.right-wndrect.left == 236) && (wndrect.bottom-wndrect.top == 200) ){
- if(HTHSMainWin){
- DoSearch(HTHSMainWin,hWnd);
- Find = true;
- }
- }
- }
- if(StrComp(windclassname,"TCommander") == 0){
- if(NULL != FindWindowEx(hWnd, NULL,"TPanel", NULL) ){
- if(HDZHWin){
- DoSearch(HDZHWin,hWnd);
- Find = true;
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void __stdcall StartSearch(char * str)
- {
- HTHSMainWin = NULL;
- HDZHWin = NULL;
- Find = false;
- StockStr = str;
- EnumWindows((WNDENUMPROC)FindTHSMainWinProc,0);
- HDZHWin = FindWindow("THJ32Main", NULL);
- if(HTHSMainWin){
- ProduceCMDWin(HTHSMainWin);
- }
- if(HDZHWin){
- ProduceCMDWin(HDZHWin);
- }
- EnumWindows((WNDENUMPROC)EnumWindowsProc,0);
- if(!Find){
- MessageBox(NULL,"您没有运行登录大智慧或者同花顺软件,请先启动并登录大智慧或者同花顺","涨停板敢死队",MB_OK);
- }
- }