C++ MFC

Moderators: Lundin
Number of threads: 3354
Number of posts: 9032

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
need help : opencv with MFC Posted by jawadali477 on 1 Jul 2012 at 9:27 PM
hi,
i'm developing a code, which is given below, using opencv with MFC in vs2008. the code builds fine and after debugging two separate windows open, one containing image while other containing static boxes. now the problem is when i double click on the image window (which is the mouse event) i don't get the (x,y) coordinates of image in my static boxes (which is in the separate window).

#include "stdafx.h"
#include "opencv01.h"
#include "opencv01Dlg.h"
#include "highgui.h"
#include "afxwin.h"
#include "cv.h"
#include "math.h"
#include<stdlib.h>
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 
void on_mouse( int evt, int x, int y, int flags, void* param );    //defining mouse event
HWND hwnd;   //defining handle
 
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
 
	enum { IDD = IDD_ABOUTBOX };
 
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 
protected:
	DECLARE_MESSAGE_MAP()
};
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
 
Copencv01Dlg::Copencv01Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(Copencv01Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
 
void Copencv01Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}
 
BEGIN_MESSAGE_MAP(Copencv01Dlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
 

// Copencv01Dlg message handlers
 
BOOL Copencv01Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
 
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
 
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
 
	
	AfxBeginThread(MyThreadProc, this); //calling thread
 
	return TRUE;  // return TRUE  unless you set the focus to a control
}
 

UINT Copencv01Dlg::MyThreadProc(LPVOID pParam)
    {
     Copencv01Dlg * me = (Copencv01Dlg *)pParam;
     me->MyThreadProc();
     return TRUE;
    }
 

void Copencv01Dlg::MyThreadProc()
{ 
	//image initialization
	
	IplImage* img = cvLoadImage("box.png", CV_WINDOW_AUTOSIZE);
		cvNamedWindow("map", 0);
		cvShowImage("map", img);
 
                cvSetMouseCallback("map", on_mouse, NULL);     //calling mouse function
 
                cvWaitKey(0);
                cvReleaseImage(&img);
		cvDestroyWindow("map");
	
}
 

void Copencv01Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}
void Copencv01Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
 
		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
 
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
 
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}
 
HCURSOR Copencv01Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
 
void on_mouse( int evt, int x, int y, int flags, void* param )
{
	CString xaxis, yaxis;
 
		if (evt == CV_EVENT_LBUTTONDBLCLK)
	{
		
			xaxis.Format(_T("%d"), x);
			yaxis.Format(_T("%d"), y);
			SetDlgItemText(hwnd, IDC_Blue, xaxis);
			SetDlgItemText(hwnd, IDC_Green, yaxis);
	}
 
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.