100分求人帮我把下面这段C#代码翻译成C++

阿两sama 2013-05-11 04:41:52
 
private static int _yesCode = 0;

[DllImport("user32.dll")]
public static extern bool EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack lpfn, int lParam);
private static bool TestForMessageBox()
{
try
{
var k = MyFindWindow();
return true;
}
catch (Exception)
{
return false;
throw;
}
}


private static System.IntPtr MyFindWindow()
{
const int myMaxParentWinCount = 2;
// 父窗口类名数组
string[] aSzClassName = { "#32770", "Button" };
// 父窗口标题数组
string[] aSzWinName = { "询问", "是(&Y)" };

// 首先求得顶级父窗口
System.IntPtr hLastWin = Win32.FindWindow(aSzClassName[0], aSzWinName[0]);
var parent = hLastWin;

// 逐次用FindWindowEx函数求出各级子窗口
for (int i = 1; i < myMaxParentWinCount; i++)
{
hLastWin = Win32.FindWindowEx(hLastWin, IntPtr.Zero, aSzClassName[i], aSzWinName[i]);
}

if (hLastWin != IntPtr.Zero)
EnumChildWindows(parent.ToInt32(), CallBackEnumChildWindows, 0);
return hLastWin;
}

//然后必须针对每个API函数定义代理的实例函数:
/// <summary>
/// 进程回调处理函数
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public static bool ThreadWindowProcess(int hwnd, int lParam)
{
EnumChildWindows(hwnd, CallBackEnumChildWindows, 0);
return true;
}

/// <summary>
/// 窗口回调处理函数
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public static bool WindowProcess(int hwnd, int lParam)
{
EnumChildWindows(hwnd, CallBackEnumChildWindows, 0);
return true;
}

/// <summary>
/// 子窗口回调处理函数
/// </summary>
/// <param name="hwnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public static bool ChildWindowProcess(int hWnd, int lParam)
{
var title = new StringBuilder(200);
Win32.GetWindowText(hWnd, title, 200);

var t = title.ToString().Trim();
var isMatch1 = System.Text.RegularExpressions.Regex.IsMatch(t, @"货品\[(?<val>.*?)\]的单价小于成本价!单据是否保存?");
var isMatch2 = System.Text.RegularExpressions.Regex.IsMatch(t, @"货品\[(?<val>.*?)\]的单价小于最低销售价\((?<val2>.*?)\),是否继续?");

//if (t != null)
//{
// MessageBox.Show(t);
//}

if (t == "是(&Y)")
{
_yesCode = hWnd;
}
if (isMatch1 || isMatch2)
{
var k = new IntPtr(_yesCode);
Win32.EnableWindow(k, false);
}

return true;
}

//最后要定义回调代理的实例
/// <summary>
/// 进程窗口回调函数代理
/// </summary>
public static CallBack CallBackEnumThreadWindows = new CallBack(ThreadWindowProcess);
/// <summary>
/// 窗口回调函数代理
/// </summary>
public static CallBack CallBackEnumWindows = new CallBack(WindowProcess);
/// <summary>
/// 子窗口回调函数代理
/// </summary>
public static CallBack CallBackEnumChildWindows = new CallBack(ChildWindowProcess);

//使用的例子:
/// <summary>
/// 客户端是否弹出对话框
/// </summary>
/// <returns></returns>
public bool IsClientPopupWindows()
{
bool FindError = false;
EnumWindows(CallBackEnumWindows, 0);
return FindError;
}
...全文
209 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿两sama 2013-05-13
  • 打赏
  • 举报
回复
万分感谢你a784063999你代码给了我很好的思路。我终于全部完成了。分给你了
赵4老师 2013-05-13
  • 打赏
  • 举报
回复
不要做A语言代码修改为B语言代码的无用功。 也不要做用A语言代码直接调用B语言代码库这样复杂、这样容易出错的傻事。 只需让A、B语言代码的输入输出重定向到文本文件,或修改A、B语言代码让其通过文本文件输入输出。 即可很方便地让A、B两种语言之间协调工作。
阿两sama 2013-05-13
  • 打赏
  • 举报
回复
我的子窗口不是MessageBox
阿两sama 2013-05-13
  • 打赏
  • 举报
回复
引用 5 楼 a784063999 的回复:

#include <Windows.h>
#include <string>
#include <stdio.h>

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam) 
{
	char t[200];
	GetWindowText(hwnd, t, 200);
	OutputDebugString(t);
	OutputDebugString("\n");

	if (!strcmp(t, "是(&Y)"))
	{
		MessageBox(hwnd, NULL, NULL, NULL);
		OutputDebugString("EnableWindow\n");
		EnableWindow(hwnd, false);
		return false;
	}
	return true;
}
 
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)  
{  
	char title[200];
	GetWindowText(hwnd, title, 200);
	OutputDebugString(title);
	OutputDebugString("\n");
	const char *pattern="minimum";
	//const char *patter="小于最低销售价";
	char *isMatch1= strstr(title, pattern);
	//char *isMatch2= strstr(title, patter);
	if (isMatch1!=NULL)// || isMatch2!=NULL)
	{
		OutputDebugString("isMatch1\n");
		EnumChildWindows(hwnd, EnumChildProc,lParam);
		return false;
	}
	return true;  
}

int main()
{
	HWND hLastWin=FindWindow(NULL, "Win32TestProject2");
	if (hLastWin)
	{
		OutputDebugString("Find succeed\n");
		EnumChildWindows(hLastWin,EnumWindowsProc,0);
	}
	return 0;
}
我用這代碼調試沒有問題
你能实现禁用按钮吗?我的"小于最低销售价"的控件和"是(&Y)"是同一级的,这么写禁用不了啊
a784063999 2013-05-12
  • 打赏
  • 举报
回复
你的子窗口是MessageBox?
a784063999 2013-05-12
  • 打赏
  • 举报
回复
這代碼的功能好像下面的 http://bbs.csdn.net/topics/390449697
a784063999 2013-05-12
  • 打赏
  • 举报
回复

#include <Windows.h>
#include <string>
#include <stdio.h>

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam) 
{
	char t[200];
	GetWindowText(hwnd, t, 200);
	OutputDebugString(t);
	OutputDebugString("\n");

	if (!strcmp(t, "是(&Y)"))
	{
		MessageBox(hwnd, NULL, NULL, NULL);
		OutputDebugString("EnableWindow\n");
		EnableWindow(hwnd, false);
		return false;
	}
	return true;
}
 
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)  
{  
	char title[200];
	GetWindowText(hwnd, title, 200);
	OutputDebugString(title);
	OutputDebugString("\n");
	const char *pattern="minimum";
	//const char *patter="小于最低销售价";
	char *isMatch1= strstr(title, pattern);
	//char *isMatch2= strstr(title, patter);
	if (isMatch1!=NULL)// || isMatch2!=NULL)
	{
		OutputDebugString("isMatch1\n");
		EnumChildWindows(hwnd, EnumChildProc,lParam);
		return false;
	}
	return true;  
}

int main()
{
	HWND hLastWin=FindWindow(NULL, "Win32TestProject2");
	if (hLastWin)
	{
		OutputDebugString("Find succeed\n");
		EnumChildWindows(hLastWin,EnumWindowsProc,0);
	}
	return 0;
}
我用這代碼調試沒有問題
阿两sama 2013-05-12
  • 打赏
  • 举报
回复
自己顶顶别沉了
阿两sama 2013-05-12
  • 打赏
  • 举报
回复
引用 1 楼 a784063999 的回复:
這代碼的功能好像下面的 http://bbs.csdn.net/topics/390449697
怎么又是你那个帖子就是我发的,我用C#把消息冲突的问题解决了,可是我自己把C#翻译成C++后代码不能运行,所以找人翻译

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧