1

我有一个名为“Skylines”的窗口,我想在这个窗口内获取鼠标点击的坐标。我想将它们写在窗口的标题上,但我不明白在释放鼠标按钮后如何更改窗口的名称。

现在我已经这样做了:

void mouseOnSkylines(int evt, int x, int y, int flags, void* param)
{
    char sWindowName[100];
    HWND hWnd, hPar;

    switch(evt)
    {
    case CV_EVENT_LBUTTONDOWN:
        hWnd = (HWND)cvGetWindowHandle("Skylines");
        hPar = GetParent(hWnd);

        sprintf(sWindowName, "(%d, %d)", x, y);
        SetWindowText(hPar, sWindowName);
        break;
    case CV_EVENT_LBUTTONUP:
        hWnd = (HWND)cvGetWindowHandle("Skylines");
        hPar = GetParent(hWnd);

        sprintf(sWindowName, "Skylines");
        SetWindowText(hPar, sWindowName);
    }
}

怎样才能做得更好?

4

1 回答 1

1

在 windows 中,您可以设置和重置 OpenCV 窗口的标题,如下所示:

#include<afxwin.h>
void mouse(int _event, int x, int y,int flags, void* param)
{
    static char old_name[256];

    if(_event==CV_EVENT_LBUTTONDOWN)
    {
        char* old_name_ptr = (char*)param;
        memcpy(old_name,old_name_ptr,256);

        char new_name[256];
        sprintf(new_name,"%d, %d\0",x,y);

        HWND hWnd = (HWND)cvGetWindowHandle(old_name);
        CWnd *wnd = CWnd::FromHandle(hWnd);
        CWnd *wndP = wnd->GetParent();
        wndP->SetWindowText(new_name);      
    }

    if(_event==CV_EVENT_LBUTTONUP)
    {
        char* old_name_ptr = (char*)param;
        HWND hWnd = (HWND)cvGetWindowHandle(old_name_ptr);
        CWnd *wnd = CWnd::FromHandle(hWnd);
        CWnd *wndP = wnd->GetParent();
        wndP->SetWindowText(old_name);      
    }
}

在主函数中:

IplImage* img = cvLoadImage(path);

char winTitle[256] = "Skylines";

cvNamedWindow(winTitle);
cvSetMouseCallback(winTitle,mouse,winTitle);
cvShowImage(winTitle,img);
cvWaitKey();
于 2012-11-27T16:25:04.210 回答