// demoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "demo.h"
#include "demoDlg.h"
#include <Windows.h>
#include <Psapi.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#pragma comment(lib, "Psapi.lib")
DWORD FindProcess(char *strProcessName)
{
DWORD aProcesses[1024], cbNeeded, cbMNeeded;
HMODULE hMods[1024];
HANDLE hProcess;
char szProcessName[MAX_PATH];
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 0;
for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)
{
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);
GetModuleFileNameEx( hProcess, hMods[0], szProcessName,sizeof(szProcessName));
if(strstr(szProcessName, strProcessName))
{
return(aProcesses[i]);
}
}
return 0;
}
DWORD KillProcess(char *strProcessName)
{
HANDLE TargetProcess = OpenProcess(PROCESS_TERMINATE, FALSE, FindProcess(strProcessName));
if(TargetProcess == NULL)
{
return 0;
}
TerminateProcess(TargetProcess, 0);
CloseHandle(TargetProcess);
return 1;
}
BOOL CALLBACK EnumChildProc(HWND hWnd,LPARAM lParam)
{
char temp1[256],temp2[256];
::GetWindowText(hWnd,temp1,255);
wsprintf(temp2,"hwnd:%x text: %s",hWnd,temp1);
MessageBox(NULL,temp2,"cwnd",MB_OK);
return true;
}
HWND FindControlWnd(HWND ParentWnd,DWORD ControlID)
{
HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);
for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))
{
//判断是否为需要的控件
if ( GetDlgCtrlID(hChild) ==(int) ControlID )
return hChild;
HWND FindWnd=FindControlWnd(hChild,ControlID);
if (FindWnd)
return FindWnd;
}
return NULL;
}
BOOL SaveHwndToBmpFile(HWND hWnd, LPCTSTR lpszPath,int initx,int inity,int height,int width)//对被控窗口截图
{
HWND hDesktop = ::GetDesktopWindow();
ASSERT(hDesktop);
if(NULL == hWnd)
{
hWnd = hDesktop;
}
RECT rect;
::GetWindowRect(hWnd, &rect);
//int nWidht = rect.right - rect.left;
//int nHeight = rect.bottom - rect.top;
int nWidht = width;
int nHeight = height;
HDC hSrcDC = ::GetWindowDC(hWnd);
ASSERT(hSrcDC);
HDC hMemDC = ::CreateCompatibleDC(hSrcDC);
ASSERT(hMemDC);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hSrcDC, nWidht, nHeight);
ASSERT(hBitmap);
HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC, hBitmap);
::BitBlt(hMemDC, 0, 0, nWidht, nHeight, hSrcDC, initx, inity, SRCCOPY);
BITMAP bitmap = {0};
::GetObject(hBitmap, sizeof(BITMAP), &bitmap);
BITMAPINFOHEADER bi = {0};
BITMAPFILEHEADER bf = {0};
CONST int nBitCount = 24;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bitmap.bmWidth;
bi.biHeight = bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = nBitCount;
bi.biCompression = BI_RGB;
DWORD dwSize = ((bitmap.bmWidth * nBitCount + 31) / 32) * 4 * bitmap.bmHeight;
HANDLE hDib = GlobalAlloc(GHND, dwSize + sizeof(BITMAPINFOHEADER));
LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
::GetDIBits(hMemDC, hBitmap, 0, bitmap.bmHeight, (BYTE*)lpbi + sizeof(BITMAPINFOHEADER), (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
try
{
CFile file;
file.Open(lpszPath, CFile::modeCreate | CFile::modeWrite);
bf.bfType = 0x4d42;
dwSize += sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bf.bfSize = dwSize;
bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
file.Write((BYTE*)&bf, sizeof(BITMAPFILEHEADER));
file.Write((BYTE*)lpbi, dwSize);
file.Close();
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}
GlobalUnlock(hDib);
GlobalFree(hDib);
::SelectObject(hMemDC, hOldBitmap);
::DeleteObject(hBitmap);
::DeleteDC(hMemDC);
::ReleaseDC(hWnd, hSrcDC);
return TRUE;
}
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static int count_bmp=0;
/
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CDemoDlg dialog
CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDemoDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
//{{AFX_MSG_MAP(CDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_RUN, OnRun)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CDemoDlg message handlers
BOOL CDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDemoDlg::OnRun()
{
// TODO: Add your control notification handler code here
HANDLE all_child[30];
TCHAR szExe[] = _T("C:\\new_ztzq_v6\\Tc.exe"); //启动
TCHAR szCmd[16] = {0};
TCHAR mes[100]={0};
TCHAR pas[100]={0};
TCHAR vcode[100]={0};
CString acount_id;
CString acount_pas;
CString acount_vcode;
CString save_name;//保存图片名称
int base_template[9][10]={{2,14,1},{5,5,3,3,3,3,3,6,4,2},{4,6,3,3,3,3,3,14,9},{2,7,6,1,1,12,12,1,1,1},{6,7,3,3,4,3,5,2},{3,4,4,5,6,6,5,2},{1,1,1,1,5,5,5,5,4},{8,12,3,3,3,3,3,12,8},{5,4,7,7,5,5,5}};
int temp[50]={0};//当前分割的字符的累积直方
double dist=0;//计算该直方与模版库直方的欧氏距离
//int count=0;//记录登录窗口后要处理的控件数目
int state=0;
int ex_code=0;
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
state=CreateProcess(szExe, szCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, & pi);
while(!state)
{
state=CreateProcess(szExe, szCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, & pi);
}
HWND test=NULL;
test=::FindWindow(NULL,"中投证券超强版独立委托v6.23");
while(!test)
{
test=::FindWindow(NULL,"中投证券超强版独立委托v6.23");
}
/*if(test)
{
::EnumChildWindows(test,EnumChildProc,0);
}
*/
//test=::FindWindow(NULL,"中投证券超强版独立委托v6.23");
//开始寻找登录窗口的各个控件的句柄
all_child[0]=FindControlWnd(test,0x0000044B);
all_child[1]=FindControlWnd(test,0x0000044F);
//all_child[0]=FindControlWnd(test,0x0000044B);
all_child[2]=FindControlWnd(test,0x000003E9);//此处只取编辑框,资金账号
all_child[3]=FindControlWnd(test,0x00000450);//交易密码
all_child[4]=FindControlWnd(test,0x00000458);//验证码
all_child[5]=FindControlWnd(test,0x00000001);//确定
all_child[6]=FindControlWnd(test,0x00000002);//取消
all_child[7]=FindControlWnd(test,0x0000000C);//通讯设置
DWORD dwStyle=::GetWindowLong(test,GWL_EXSTYLE);
::SetWindowPos(test,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
//::SetWindowPos(test,HWND_TOP,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
//::SetFocus(::FindWindow(NULL,"中投证券超强版-新一代v6.23"));//这一句不是必须的
//ShowWindow(SW_HIDE); //隐藏父窗口
//ShowWindow(SW_SHOW); //显示父窗口 _T("F://vcode.bmp")
//::BringWindowToTop(test);
Sleep(2000);
//save_name.Format("%d", count_bmp);
save_name="vode.bmp";
if(test)
{
SaveHwndToBmpFile(test, save_name,329,124,18,60);//截取验证码位置的图片并保存
}
if(!((dwStyle&WS_EX_TOPMOST)==WS_EX_TOPMOST))
{
//保证主框架前置,然后再恢复到正常状态 328,123,20,62
::SetWindowPos(test,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
}
GetDlgItemText(IDC_acount_id,acount_id);
GetDlgItemText(IDC_acount_pas,acount_pas);
//GetDlgItemText(IDC_acount_vcode,acount_vcode);
//从保存的图片中计算验证码
//首先转成黑白二值图片
int seg_num=0;//简单的自动交易验证码的分段数
int hst[100]={0};//x轴上投影的直方图,黑色像素个数
int vode[10]={0};//计算出的验证码
CFile file;
BYTE *bmpPtr=NULL; //指针初始化
BITMAPFILEHEADER head;
BITMAPINFOHEADER infhead;
if(file.Open(save_name,CFile::modeRead)==0)
{
AfxMessageBox("不能读文件");
return;
}
int h=0,w=0;
//float gray;
file.Read(&head,sizeof(head));
file.Read(&infhead,sizeof(infhead));
if(head.bfType!=0x4d42)
{
AfxMessageBox("非bmp文件");
return;
}
if(infhead.biBitCount!=24)
{
AfxMessageBox("非24位bmp文件"); //是24位位图的话,R G B位数各为一个字节
return;
}
h=infhead.biHeight;
w=infhead.biWidth;
bmpPtr=new BYTE[h*w*3]; //每像素3字节
if(NULL==bmpPtr)
{
AfxMessageBox("内存不够");
return;
}
file.Seek(head.bfOffBits,CFile::begin);
DWORD len=file.ReadHuge(bmpPtr,h*w*3);
if(len!=(DWORD)(h*w*3))
{
AfxMessageBox("文件长度不够");
return;
}
for(int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
bmpPtr[3*x+y*w*3]=int((float)(0.114*bmpPtr[3*x+y*w*3]+0.587*bmpPtr[3*x+y*w*3+1]+0.299*bmpPtr[3*x+y*w*3+2]));
if(bmpPtr[3*x+y*w*3]<189)
{
bmpPtr[3*x+y*w*3]=0;
bmpPtr[3*x+y*w*3+1]=0;
bmpPtr[3*x+y*w*3+2]=0;
}
else
{
bmpPtr[3*x+y*w*3]=255;
bmpPtr[3*x+y*w*3+1]=255;
bmpPtr[3*x+y*w*3+2]=255;
}
if(bmpPtr[3*x+y*w*3]<=124)//统计黑色像素
{
hst[x+1]++;
}
}
}
int temp_seg_x=0;//当前分割字符占x轴的长度
/*
分情况,分当前x轴和下一个的黑白情况,所以有四种
*/
for(x=0;x<w+1;x++)
{
//temp_seg_x=x;
if(hst[x]<1&&hst[x+1]>=1)
{
seg_num++;
dist=0;
for(int x_width=0;x_width<10;x_width++)
{
temp[x_width]=0;
}
temp_seg_x=0;
continue;
}
if(hst[x]<1&&hst[x+1]<1)
{
continue;
}
if(hst[x]>=1&&hst[x+1]>=1)
{
temp[temp_seg_x]=hst[x];
temp_seg_x++;
continue;
}
if(hst[x]>=1&&hst[x+1]<1)//当前黑下一个白时需要计算欧氏距离并判断属于哪个字符
{
temp[temp_seg_x]=hst[x];
temp_seg_x++;
for(int i=0;i<9;i++)
{
dist=0;
for(int j=0;j<10;j++)
{
dist+=(temp[j]-base_template[i][j])*(temp[j]-base_template[i][j]);//平方和
}
dist=sqrt(dist);
if(dist<3)//欧氏距离准确?一般为2.83
{
vode[seg_num]=i+1;
break;
}
else//本次未找到,继续
continue;
}
continue;
}
}
file.Close();
delete []bmpPtr;
CString strTemp;
strTemp.Format("%d%d%d%d",vode[1],vode[2],vode[3],vode[4]);
SetDlgItemText(IDC_acount_vcode,strTemp);
::SendMessage((HWND)all_child[2],WM_SETTEXT,0,(LPARAM)(acount_id.GetBuffer(acount_id.GetLength())));
::SendMessage((HWND)all_child[3],WM_SETTEXT,0,(LPARAM)(acount_pas.GetBuffer(acount_pas.GetLength())));
::SendMessage((HWND)all_child[4],WM_SETTEXT,0,(LPARAM)(strTemp.GetBuffer(strTemp.GetLength())));
acount_id.ReleaseBuffer();
acount_pas.ReleaseBuffer();
strTemp.ReleaseBuffer();
::SendMessage((HWND)all_child[5],WM_LBUTTONDOWN,0,0);
::SendMessage((HWND)all_child[5],WM_LBUTTONUP,0,0);
/*::strncpy(mes,"61009347",8);
::strncpy(pas,"136589",6);
::strncpy(vcode,"5772",4);
::SendMessage((HWND)all_child[2],WM_SETTEXT,0,(LPARAM)mes);
::SendMessage((HWND)all_child[3],WM_SETTEXT,0,(LPARAM)pas);
::SendMessage((HWND)all_child[4],WM_SETTEXT,0,(LPARAM)vcode);
::SendMessage((HWND)all_child[5],WM_LBUTTONDOWN,0,0);
::SendMessage((HWND)all_child[5],WM_LBUTTONUP,0,0);
*/
//all_child[0]=FindControlWnd(test,0x0000044B);BN_CLICKEDWM_COMMAND
//all_child[0]=FindControlWnd(test,0x0000044B);WM_ENABLE
//ex_code=KillProcess(szExe);
/*if(ex_code)
{
MessageBox("Exit suc");
}
HDC dc1;
dc1=::GetDC(test);
CDC* pdc=this->GetDC();
//::BitBlt(pdc->m_hDC,0,0,123,123,dc1,0,0,SRCCOPY);
//::SendMessage(test,WM_PAINT,(WPARAM)(pdc->m_hDC),0);
//::printwin
this->ReleaseDC(pdc);
::ReleaseDC(test,dc1);*/
}
void CDemoDlg::OnButton1()
{
int base_template[9][10]={{2,14,1},{5,5,3,3,3,3,3,6,4,2},{4,6,3,3,3,3,3,14,9},{2,7,6,1,1,12,12,1,1,1},{6,7,3,3,4,3,5,2},{3,4,4,5,6,6,5,2},{1,1,1,1,5,5,5,5,4},{8,12,3,3,3,3,3,12,8},{5,4,7,7,5,5,5}};
int temp[50]={0};//当前分割的字符的累积直方
double dist=0;//计算该直方与模版库直方的欧氏距离
CString bmp_name="";
CString bmp_name1=_T("D:\\自动交易\\新起点\\复件zdjy正在修改\\转换后黑白图片\\");
for(int task_num=1;task_num<=101;task_num++)
{
bmp_name.Format("%d", task_num);
bmp_name=bmp_name1+bmp_name+"黑白.bmp";
int seg_num=0;//简单的自动交易验证码的分段数
int hst[1000]={0};//x轴上投影的直方图,黑色像素个数
int vode[10]={0};//计算出的验证码
CFile file;
BYTE *bmpPtr=NULL; //指针初始化
BITMAPFILEHEADER head;
BITMAPINFOHEADER infhead;
if(file.Open(bmp_name,CFile::modeRead)==0)
{
AfxMessageBox("不能读文件");
return;
}
int h=0,w=0;
file.Read(&head,sizeof(head));
file.Read(&infhead,sizeof(infhead));
if(head.bfType!=0x4d42)
{
AfxMessageBox("非bmp文件");
return;
}
if(infhead.biBitCount!=24)
{
AfxMessageBox("非24位bmp文件"); //是24位位图的话,R G B位数各为一个字节
return;
}
h=infhead.biHeight;
w=infhead.biWidth;
bmpPtr=new BYTE[h*w*3]; //每像素3字节
if(NULL==bmpPtr)
{
AfxMessageBox("内存不够");
return;
}
file.Seek(head.bfOffBits,CFile::begin);
DWORD len=file.ReadHuge(bmpPtr,h*w*3);
if(len!=(DWORD)(h*w*3))
{
AfxMessageBox("文件长度不够");
return;
}
for(int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
if(bmpPtr[3*x+y*w*3]<=124)//统计黑色像素
{
hst[x+1]++;
}
}
}
int temp_seg_x=0;//当前分割字符占x轴的长度
/*
分情况,分当前x轴和下一个的黑白情况,所以有四种
*/
for(x=0;x<w+1;x++)
{
//temp_seg_x=x;
if(hst[x]<1&&hst[x+1]>=1)
{
seg_num++;
dist=0;
for(int x_width=0;x_width<10;x_width++)
{
temp[x_width]=0;
}
temp_seg_x=0;
continue;
}
if(hst[x]<1&&hst[x+1]<1)
{
continue;
}
if(hst[x]>=1&&hst[x+1]>=1)
{
temp[temp_seg_x]=hst[x];
temp_seg_x++;
continue;
}
if(hst[x]>=1&&hst[x+1]<1)//当前黑下一个白时需要计算欧氏距离并判断属于哪个字符
{
temp[temp_seg_x]=hst[x];
temp_seg_x++;
for(int i=0;i<9;i++)
{
dist=0;
for(int j=0;j<10;j++)
{
dist+=(temp[j]-base_template[i][j])*(temp[j]-base_template[i][j]);//平方和
}
dist=sqrt(dist);
if(dist<3)//欧氏距离准确?一般为2.83
{
vode[seg_num]=i+1;
break;
}
else//本次未找到,继续
continue;
}
continue;
}
}
delete []bmpPtr;
file.Close();
CString strTemp;
strTemp.Format("%d %d %d %d %d\r\n",task_num,vode[1],vode[2],vode[3],vode[4]);
CFile fe;
fe.Open( _T("vode_3-3.txt"), CFile::modeCreate |CFile::modeNoTruncate|CFile::modeWrite );
fe.SeekToEnd();
fe.Write( strTemp, strTemp.GetLength() );
fe.Close();
//AfxMessageBox("成功分段");
}
}