0

我有以下代码。当我执行代码时,我的鼠标指针移动到 0 0 坐标。我需要将光标移动到 x1 y1 位置。x1 y1 的值是整数。

int x1,y1;
                for(int i=0; i<nomdef; i++)  
                {
                    if(defectArray[i].depth > 40 )
                    {
                        con=con+1;
                        if(con==1)
                        {
                            x1=(defectArray[i].depth_point)->x;
                y1=(defectArray[i].depth_point)->y;
                        }
                        cvLine(src, *(defectArray[i].start), *(defectArray[i].depth_point),CV_RGB(255,255,0),1, CV_AA, 0 );  
                        cvCircle( src, *(defectArray[i].depth_point), 5, CV_RGB(0,0,255), 2, 8,0);                              cvDrawContours(src,defects,CV_RGB(0,0,0),CV_RGB(255,0,0),-1,CV_FILLED,8);

                    }
                }system("xdotool mousemove x1 y1");
4

1 回答 1

0

这是一个 C++ 程序(不是 bash 或任何类似的高级语言)。C/C++ 字符串常量中没有变量调用/替换。

因此,系统调用执行您所写的操作:调用"xdotool mousemove x1 y1"(没有像您期望的那样替换 x1 和 y1)。

相反,您必须格式化字符串,例如使用std::string, std::ostringstream

将这些包含添加到您的文件开始:

#include <string>
#include <sstream>

将代码的最后一行更改为:

std::ostringstream ossCmd;
ossCmd << "xdotool mousemove " << x1 << ' ' << y1;
#if 1 // EXPLICIT:
std::string cmd = ossCmd.str();
system(cmd.c_str());
#else // COMBINED:
system(ossCmd.str().c_str());
#endif // 1

这应该有效。

笔记:

#if 1件事可能看起来很奇怪,但在 C/C++ 中,有一个常用的方法是拥有活动和非活动代码替代方案,开发人员可以根据需要在它们之间进行更改。

于 2017-04-21T08:27:09.433 回答