Halcon MFC混合编程-图像显示随鼠标滚动放大缩小

MFC Halcon配置详见博客 点击打开链接

1.新建基于对话框工程,工程名取为image
2.在类中定义如下变量和函数:
public:

	HTuple   m_hWnd; //显示窗口句柄
	HObject  m_hImage;//图像对象
	HTuple   m_hWidth;//图像宽
	HTuple   m_hHeight;

	CRect    m_rtImage;
	CString  m_strImagePath;

	double  m_dDispImagePartRow0;
	double  m_dDispImagePartCol0;
	double  m_dDispImagePartRow1;
	double  m_dDispImagePartCol1;
public:
	BOOL InitHalconWindow();
	void ShowImage();

3.函数实现如下:

初始化窗口的函数

BOOL CimageDlg::InitHalconWindow()
{
	CWnd *pWnd = GetDlgItem(IDC_STATIC_IMAGE);
	pWnd->GetClientRect(&m_rtImage);

	if (m_hWnd != 0)
	{
		HalconCpp::CloseWindow(m_hWnd);
	}
	SetWindowAttr("background_color", "black");
	OpenWindow(0, 0, m_rtImage.Width(), m_rtImage.Height(), (INT)pWnd->m_hWnd, "visible", "", &m_hWnd);

	return 0;
}


显示图片函数

void CimageDlg::ShowImage()
{
	if (m_hWnd != 0)
	{
		SetSystem("flush_graphic", "false");
		ClearWindow(m_hWnd);
		//显示
		if (m_hImage.IsInitialized())
		{
			SetPart(m_hWnd, m_dDispImagePartRow0, m_dDispImagePartCol0, m_dDispImagePartRow1 - 1, m_dDispImagePartCol1 - 1);
			DispObj(m_hImage, m_hWnd);
		}

		SetSystem("flush_graphic", "true");
		HObject emptyObject;
		emptyObject.GenEmptyObj();
		DispObj(emptyObject, m_hWnd);
	}
}

3.初始化变量,并在类初始化中调用初始化窗口函数

(1)在构造函数中初始化变量:
CimageDlg::CimageDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CimageDlg::IDD, pParent)
	,m_hWnd(0)
(2)在初始化函数中调用窗口初始化函数:

BOOL CimageDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO:  在此添加额外的初始化代码

	InitHalconWindow();//初始化窗口

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

4.添加鼠标滚动函数:
在类向导或对话框属性中选择消息,选择WM_MOUSEWHEEL,并添加相应函数:

BOOL CimageDlg::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// 此功能要求 Windows Vista 或更高版本。
	// _WIN32_WINNT 符号必须 >= 0x0600。
	// TODO:  在此添加消息处理程序代码和/或调用默认值
	CRect rtImage;
	GetDlgItem(IDC_STATIC_IMAGE)->GetWindowRect(&rtImage);
	if (rtImage.PtInRect(pt) && m_hImage.IsInitialized())
	{
		Hlong  ImagePtX, ImagePtY;
		Hlong  Row0, Col0, Row1, Col1;
		double Scale = 0.1;

		if (zDelta>0)
		{
			ImagePtX = m_dDispImagePartCol0 + (pt.x - rtImage.left) / (rtImage.Width() - 1.0)*(m_dDispImagePartCol1 - m_dDispImagePartCol0);
			ImagePtY = m_dDispImagePartRow0 + (pt.y - rtImage.top) / (rtImage.Height() - 1.0)*(m_dDispImagePartRow1 - m_dDispImagePartRow0);
			Row0 = ImagePtY - 1 / (1 - Scale)*(ImagePtY - m_dDispImagePartRow0);
			Row1 = ImagePtY - 1 / (1 - Scale)*(ImagePtY - m_dDispImagePartRow1);
			Col0 = ImagePtX - 1 / (1 - Scale)*(ImagePtX - m_dDispImagePartCol0);
			Col1 = ImagePtX - 1 / (1 - Scale)*(ImagePtX - m_dDispImagePartCol1);

			m_dDispImagePartRow0 = Row0;
			m_dDispImagePartCol0 = Col0;
			m_dDispImagePartRow1 = Row1;
			m_dDispImagePartCol1 = Col1;
		}
		else
		{
			ImagePtX = m_dDispImagePartCol0 + (pt.x - rtImage.left) / (rtImage.Width() - 1.0)*(m_dDispImagePartCol1 - m_dDispImagePartCol0);
			ImagePtY = m_dDispImagePartRow0 + (pt.y - rtImage.top) / (rtImage.Height() - 1.0)*(m_dDispImagePartRow1 - m_dDispImagePartRow0);
			Row0 = ImagePtY - 1 / (1 + Scale)*(ImagePtY - m_dDispImagePartRow0);
			Row1 = ImagePtY - 1 / (1 + Scale)*(ImagePtY - m_dDispImagePartRow1);
			Col0 = ImagePtX - 1 / (1 + Scale)*(ImagePtX - m_dDispImagePartCol0);
			Col1 = ImagePtX - 1 / (1 + Scale)*(ImagePtX - m_dDispImagePartCol1);

			m_dDispImagePartRow0 = Row0;
			m_dDispImagePartCol0 = Col0;
			m_dDispImagePartRow1 = Row1;
			m_dDispImagePartCol1 = Col1;
		}
		ShowImage();
	}
	return CDialogEx::OnMouseWheel(nFlags, zDelta, pt);

}




5.添加控件:
(1)picture control,ID号:IDC_STATIC_IMAGE,
(2)打开按钮,ID号:IDC_BT_OPEN_IMAGE,caption:OpenImage,响应函数如下:

void CimageDlg::OnBnClickedBtOpenImage()
{
	// TODO:  在此添加控件通知处理程序代码

	CFileDialog dlg(TRUE, "jpg", NULL,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		"jpg文件 (*.jpg)|*.jpg||", NULL);
	if (dlg.DoModal() == IDCANCEL)
	{
		return;
	}

	m_strImagePath = dlg.GetPathName();
	ReadImage(&m_hImage, m_strImagePath.GetBuffer(0));

	////test show
	//DispObj(m_hImage, m_hWnd);


	GetImageSize(m_hImage, &m_hWidth, &m_hHeight);
	//设置窗口
	float fImage = m_hWidth.D() / m_hHeight.D();
	float fWindow = (float)m_rtImage.Width() / m_rtImage.Height();
	float Row0 = 0, Col0 = 0, Row1 = m_hHeight - 1, Col1 = m_hWidth - 1;
	if (fWindow > fImage)
	{
		float w = fWindow * m_hHeight;
		Row0 = 0;
		Col0 = -(w - m_hWidth) / 2;
		Row1 = m_hHeight - 1;
		Col1 = m_hWidth + (w - m_hWidth) / 2;
	}
	else
	{
		float h = m_hWidth / fWindow;
		Row0 = -(h - m_hHeight) / 2;
		Col0 = 0;
		Row1 = m_hHeight + (h - m_hHeight) / 2;
		Col1 = m_hWidth - 1;
	}

	m_dDispImagePartRow0 = Row0;
	m_dDispImagePartCol0 = Col0;
	m_dDispImagePartRow1 = Row1;
	m_dDispImagePartCol1 = Col1;

	ShowImage();

}


运行,打开图像,便能显示图像了,图像的大小随鼠标滚动变化,效果如下
Halcon MFC混合编程-图像显示随鼠标滚动放大缩小_第1张图片

演示程序工程文件见资源:

http://download.csdn.net/detail/bettyshasha/9567311




你可能感兴趣的:(MFC,图像显示,鼠标滚动,放大缩小,h,MFC,Halcon混合编程)