ZjlAj.h
#pragma once #include"stdafx.h" #define ZJL_TEXT _T("RF Setting V3.47") enum { NOS_COM = 0X20000, NOS_HWND, NOS_TEXT, YES_COM, YES_HWND, YES_TEXT }; class ZjlAJ { private: HWND m_hwnd; CWnd *m_pCwnd; public: ZjlAJ(HWND hwnd); ZjlAJ(); /* 作用:将句柄转换成字符串 参数: 返回值:返回句柄的字符串 */ CString HwndToStr(); /* 作用:获取窗口句柄 参数:窗口名字 返回值:该窗口句柄 */ HWND GetHwnd(CString str); /* 作用:获取控件句柄,使用内部句柄 参数:*str 句柄字符串的缓冲区 返回值:>0 句柄个数 -1 是不存父句柄 */ BOOL StrGetChildCtrlID(CString *str); /* 作用:获取控件句柄,使用外部句柄 参数:*szHandle 句柄字符串的缓冲区 *szpText 控件的标题缓冲区 *szCtrlID 控件ID的缓冲区 Zjlhwnd 窗口句柄或者控件句柄 返回值:>0 句柄个数 -1 是不存父句柄 */ BOOL GetChildCtrlInfo(HWND Zjlhwnd, CString * szHandle, CString *szpText, CString *szCtrlID); BOOL GetChildHwnd(CString szStr, HWND *ChildHwnd, int nCtrlID); /* 作用:将字符串转换成句柄 参数:str 对应数字的字符串 返回值:返回句柄 */ HWND aToHwnd(CString str); /* 作用:获取窗口类CWnd 参数: 返回值: */ CWnd *GetCWnd(); /* 作用:获取窗口类CWnd 参数:str 窗口标题 返回值: */ CWnd *GetCWnd(CString str); ~ZjlAJ(); public: /* 作用:获取控件位置 参数:hwnd 控件句柄 返回值: */ CRect GetCtrlRect(HWND hwnd); /* 作用:模拟点击按钮 参数:x、y 是按钮位置 GetCtrlRect(HWND hwnd)可以获取 返回值: */ void ClickButtom(int x, int y); /* 作用:模拟点击按钮 参数:rect 是按钮矩形位置 GetCtrlRect(HWND hwnd)可以获取 返回值: */ void ClickButtom(CRect rect); /* 作用:设置下拉框的数值 参数:num 是选择第几个 hwnd 控件句柄 返回值:NOS_COM 设置失败 YES_COM 设置成功 */ BOOL SetComText(HWND hwnd, int num); /* 作用:给文本框发送信息 参数:hwnd 是文本框的句柄 str 是要设置的文字 返回值: */ BOOL SetText(HWND hwnd, CString str); };
ZjlAj.cpp
#include"stdafx.h" #include"ZjlAJ.h" ZjlAJ::ZjlAJ(HWND hwnd) { m_hwnd = hwnd; m_pCwnd = NULL; } ZjlAJ::ZjlAJ() { } CString ZjlAJ::HwndToStr() { CString str; str.Format(_T("%d"), m_hwnd); return str; } HWND ZjlAJ::GetHwnd(CString str) { m_pCwnd = CWnd::FindWindow(NULL, str); m_hwnd = m_pCwnd->GetSafeHwnd(); return m_hwnd; } /* //获取窗口句柄 CWnd *pCwnd = FindWindow(NULL, ZJL_TEXT); CWnd *pChild = pCwnd->GetWindow(GW_CHILD); int nCtrlID = 0; if (!pCwnd) AfxMessageBox(_T("打开失败")); //获取控件ID else { while (pChild != NULL) { CString str; nCtrlID = pChild->GetDlgCtrlID(); str.Format(_T("%d"), nCtrlID); AfxMessageBox(str); pChild = pChild->GetWindow(GW_HWNDNEXT); } } */ BOOL ZjlAJ::StrGetChildCtrlID(CString *str) { if (!m_hwnd) return -1; HWND hwnd = GetWindow(m_hwnd, GW_CHILD); int nCtrlID = 0, i = 0; do { CString s; nCtrlID = GetDlgCtrlID(hwnd); s.Format(_T("%x"), nCtrlID); str[i] = s; i++; hwnd = GetWindow(hwnd, GW_HWNDNEXT); } while (hwnd); return i; } //输入句柄 BOOL ZjlAJ::GetChildCtrlInfo(HWND Zjlhwnd,CString * szHandle, CString *szpText, CString *szCtrlID) { if (!Zjlhwnd) return -1; HWND hwnd = GetWindow(Zjlhwnd, GW_CHILD); int nCtrlID = 0, i = 0; do { TCHAR szText[255]; CString s, id; nCtrlID = GetDlgCtrlID(hwnd); GetWindowText(hwnd, szText, 255); s.Format(_T("%d"), hwnd); id.Format(_T("%x"), nCtrlID); szHandle[i] = s; szpText[i] = szText; szCtrlID[i] = id; i++; hwnd = GetWindow(hwnd, GW_HWNDNEXT); } while (hwnd); return i; } BOOL ZjlAJ::GetChildHwnd(CString szStr, HWND *ChildHwnd, int nCtrlID) { if (!m_hwnd) return -1; HWND hwnd = GetWindow(m_hwnd, GW_CHILD); do { TCHAR *szText = new TCHAR[255]; ::GetWindowText(hwnd, szText, 255); if (szStr.Compare(szText) == 0) { nCtrlID = GetDlgCtrlID(hwnd); ChildHwnd = &hwnd; return 1; } hwnd = GetWindow(hwnd, GW_HWNDNEXT); } while (hwnd); return 0; } HWND ZjlAJ::aToHwnd(CString str) { HWND hwnd = (HWND)_tcstoul(str, NULL, 16); return hwnd; } CWnd * ZjlAJ::GetCWnd() { return m_pCwnd; } CWnd * ZjlAJ::GetCWnd(CString str) { GetHwnd(str); return m_pCwnd; } ZjlAJ::~ZjlAJ() { } CRect ZjlAJ::GetCtrlRect(HWND hwnd) { CRect rect; ::GetWindowRect(hwnd, rect); return rect; } void ZjlAJ::ClickButtom(int x, int y) { //设置鼠标位置 SetCursorPos(x, y); //模拟左键点击 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } void ZjlAJ::ClickButtom(CRect rect) { int x = rect.left + (rect.right - rect.left) / 2; int y = rect.top + (rect.bottom - rect.top) / 2; //设置鼠标位置 SetCursorPos(x, y); //模拟左键点击 mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } BOOL ZjlAJ::SetComText(HWND hwnd, int num) { int Res; Res = ::SendMessage(hwnd, CB_SETCURSEL, num, 0); if (0 == Res) return NOS_COM; return YES_COM; } BOOL ZjlAJ::SetText(HWND hwnd, CString str) { int res = ::SendMessage(hwnd, WM_SETTEXT/*向文本框发送消息*/, 0, (LPARAM)(LPCTSTR)str); if (!res) return NOS_TEXT; return YES_TEXT; }
标签:CString,HWND,hwnd,按键,ZjlAJ,C++,str,句柄,发送数据 From: https://www.cnblogs.com/lidabo/p/16922296.html