我有一个名为“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);
}
}
如何做得更好?
答案 0 :(得分: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();