2016-03-01 56 views
1

我试图打开不同的网址,当点击图像(每个图像将打开不同的网址)不幸的是,它不断打开第一个网址。图像的方向和调用url的代码放在鼠标点击的回调函数下,如下所示。用鼠标点击两个不同的图像打开不同的网址C++

|  |  | 
| 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(我运行该程序的C++与OpenCV的3.0)

+1

你为什么调度image.data?如果方向一致,可能只是检查'x yuyoyuppe

回答

0

从图像的方向来看,可以选择使用点击事件的x协调URL。例如,如果图像宽度相同,则可以使用x < window_width/2条件。

例子:

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

为了得到窗口宽度,您可以得到cvGetWindowHandle窗机句柄,然后特定于平台的功能就可以了,或只是计算与影像的大小宽度。

+0

图像大小相当......但我很抱歉,我没有真正明白你的意思。 (C++中的新功能) – user1714742

+0

在您的CallBackFunc中,您正在接受鼠标单击位置,因此您可以根据“x”坐标确定已经点击了哪个图像 – yuyoyuppe

+0

因此,这意味着我只需要声明一个window_width,该坐标由坐标的形象? – user1714742

相关问题