自定義函數:id是MFC窗體上的PictureControl控件的ID,此函數返回halcon的HTuple類型。
HTuple initPicControl(int id);
HTuple CHalconMFC1219Dlg::initPicControl(int id)//初始化halcon窗體控件到pictureControl控件上 { HTuple hWindow; CRect Rect; HWND hWnd = GetDlgItem(id)->GetSafeHwnd();//獲取控件句柄 ::GetWindowRect(hWnd, &Rect);//獲取控件大小 OpenWindow(0, 0, Rect.Width(), Rect.Height(), (Hlong)hWnd, "visible", "", &hWindow); return hWindow; }
使用方法:以CHalconMFC1219Dlg工程為例
1、復制粘貼上述代碼到CHalconMFC1219Dlg.cpp中,作為類的成員函數。在頭文件里添加一行代碼
HTuple hWindow;
2、在OnInitDialog()函數中調用此函數。
BOOL CHalconMFC1219Dlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 設置此對話框的圖標。 當應用程序主窗口不是對話框時,框架將自動 // 執行此操作 SetIcon(m_hIcon, TRUE); // 設置大圖標 SetIcon(m_hIcon, FALSE); // 設置小圖標 // TODO: 在此添加額外的初始化代碼 hWindow=initPicControl(IDC_PictureControl); return TRUE; // 除非將焦點設置到控件,否則返回 TRUE }
按鈕事件函數
HImage Mandrill("monkey"); Hlong width, height; Mandrill.GetImageSize(&width, &height); SetPart(hWindow,0,0,height-1,width-1); //圖片適應窗體 DispObj(Mandrill,hWindow); //顯示在hWindow上