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;
}
...全文
206 9 打赏 收藏 转发到动态 举报
写回复
用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++后代码不能运行,所以找人翻译
1 单元 每个单元只包含一个类的声明。类中不永许写类的实现,类成员函数不超过20个,需要被外界调用的成员函数放在Public外,其余的全部放在Proteced,明确的不想让继承者修改的话放在Private中。 包含的头文件 也就是#include 在头文件应该包含该类所需要的类型的。如果cpp也包含了,那么头文件就不要在包含,使用Class vcl; 告诉头文件有些需要的已经在CPP文件中包含啦!另外对不再使用的包含文件要及时的清除出去,否则可能碰到连接出错的问题,bcb的连接提示少的可怜啊!就有苦头吃拉!还有要记得给自己加进的包含写注解啊!! 2 实现文件中 千万不要定义全局变量和全局函数,至于会发什么问题啦,只有碰到了几十万的代码,五六个项目集合,一两百个单元所可能发生的问题。 那该怎么办了? 简单把它归到所在单元的类声明中去。函数的实现代码行不要超过200行 包括之间的空格.因为人的暂时记忆能力不强啊! 为什么文章要段落啊! 就是人暂存上下文的关联的内存是有限的呀 3 组件的属性设置和数据库的字段表名: 一般下都会在对象检查器中的设置。当调试过不去而你又忘记了设置的属性时的哭相,是多么地可气啊!而且BCB没有组件之间的比较,否则的话可以拖个同样的新组件来比一比到底哪里设置的不同! 所以没有办法只好麻烦些直接在代码中设置吧! 虽然很麻烦 却可以节省比较可观的时间呀! 要不就像 kateboy (老公)由于设置了ADOConnection 的Attrib里中的自动事务处理。每当退出程序时就报错!从早上跑到各大BCB的QQ群中求人助,只到最后吃晚饭才惊奇发现了毛病所在! 还有一次不知那各BCB老大自报家丑 说自己吸了5包烟抓改BUG直到凌晨时,方发现了它,原来是书写错误。

64,662

社区成员

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

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