用鼠标点击两个不同的图像C ++打开不同的Urls

时间:2016-03-01 05:27:37

标签: c++ visual-studio opencv3.0

我试图在点击图片时打开不同的网址(每个图片都会打开不同的网址),不幸的是,它仍然只打开第一个网址。图像的方向和调用url的代码放在mouseclick的callbackfuction下,如下所示。

|        |        |
| Image 1|Image 2 |
|________|________|

void CallBackFunc(int event, int x, int y, int flags, void * userdata)
{

  if(event == EVENT_LBUTTONDOWN && image1.data)
  {

    //cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    const char * urlA = "http://www.google.com";
    wchar_t      urlW[MAX_PATH];
    std::copy(urlA, urlA + lstrlenA(urlA) + 1, urlW);
    if((int)ShellExecuteW(NULL, L"open", urlW, NULL, NULL, SW_SHOW) < 32)
      ;
  }

  else if(event == EVENT_LBUTTONDOWN && image2.data)
  {

    cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")"
         << endl;
    const char * urlA = "http://www.yahoo.com";
    wchar_t      urlW[MAX_PATH];
    std::copy(urlA, urlA + lstrlenA(urlA) + 1, urlW);
    if((int)ShellExecuteW(NULL, L"open", urlW, NULL, NULL, SW_SHOW) < 32)
      ;
  }

  else if(event == EVENT_RBUTTONDOWN)
  {
    cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")"
         << endl;
  }
  else if(event == EVENT_MBUTTONDOWN)
  {
    cout << "Middle button of the mouse is clicked - position (" << x << ", " << y
         << ")" << endl;
  }
  else if(event == EVENT_MOUSEMOVE)
  {
    cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
  }
}

我真的不知道我哪里出错了,因为我尝试将第二个url声明为urlB,但它仍然无法正常工作。我希望有一个人可以帮助我。谢谢!

PS(我使用opencv 3.0在C ++上运行程序)

1 个答案:

答案 0 :(得分:0)

从图像判断&#39;方向,您可以使用click事件的x坐标选择URL。例如,如果您的图片宽度相等,则可以使用x < window_width/2条件。

示例:

if(event == EVENT_LBUTTONDOWN && x < window_width / 2)

要获得窗口宽度,您可以获得cvGetWindowHandle窗口原生句柄,然后获取平台特定的功能,或者只计算图像的宽度&#39;尺寸。