2016-03-01 79 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

相關問題