MFC 窗体的调用

9 篇文章 0 订阅
6 篇文章 0 订阅
MFC中DoModal()的返回值并不简单等同于OK或CANCEL键的ID,而是由EndDialog指定的nRetCode参数决定。正常情况下,OnOK()返回IDOK,OnCancel()返回 IDCANCEL。对于自定义关闭方式,可以使用EndDialog(int nRetCode)设置。当点击模态对话框右上角的关闭按钮时,如果没有特别处理,会默认调用OnCancel()。解决这个问题需要为关闭按钮响应WM_CLOSE消息并设置适当的关闭方式。
摘要由CSDN通过智能技术生成

查了很多资料,都说DoModal()返回值为OK或CANCEL键的ID

 

  其实,这种说法是错误的

 

  MSDN是这样说明其返回值的

 

        If successful, the value of the nRetCode parameter specified in the call to EndDialog; otherwise, -1.

 

        如果操作成功,其返回值为由EndDialog指定的nRetCode的值,而此参数nRetCode的含义为关闭对话框所采用的方式

  

     也就是说,在关闭此模态对话框时,其返回值为关闭对话框时所采用的方式

     因此它只在对话框关闭时才返回相关参数值

 

     默认对话框关闭方式有2种:OnOK();    OnCancel()

 

     当使用OnOK()函数关闭对话框时,返回值为IDOK

 

     当使用OnCancel()函数关闭对话框时,返回值为IDCANCEL

 

     返回值与ID无关 

 

     比如一个按钮的ID为IDC_BUTTON1

     在此按钮的处理函数中添加关闭对话框方式: OnOk()

     则 点击此按钮时,对话框返回值为IDOK

 

 

除此两种默认关闭方式外,还可用EndDialog(int nRetCode)设定自定义的关闭方式

 

如下例子:


void EndDialog(
   int nResult 
);

Parameters

nResult

    Contains the value to be returned from the dialog box to the caller of DoModal.

 Remarks

This member function returns nResult as the return value of DoModal. You must use the EndDialog function to complete processing whenever a modal dialog box is created.

You can call EndDialog at any time, even in OnInitDialog, in which case you should close the dialog box before it is shown or before the input focus is set.

EndDialog does not close the dialog box immediately. Instead, it sets a flag that directs the dialog box to close as soon as the current message handler returns.





 Example

/* MyWnd.cpp */
#include "MyDialog.h"

void CMyWnd::ShowDialog()
{
   CMyDialog myDlg;
   int nRet = myDlg.DoModal();

   if ( nRet == IDOK || nRet == 5  )
      AfxMessageBox("Dialog closed successfully");
}

/* MyDialog.cpp */
void CMyDialog::OnSomeAction()
{
   // Do something

   int nRet = 5; // Just any value would do!
   EndDialog(nRet); // This value is returned by DoModal!

   // Do something

   return; // Dialog closed and DoModal returns only here!
}



在工程中遇到这样的问题

 

一个模态对话框 有三个按钮

 

分别是ONOK()   ONCANCEL() EndDialog(0XFF)

 

   

但是发现 当按模态对话框右上角的叉号关闭对话框时

 

总是跳到ONCANCEL()关闭对话框时相同的处理方法

 

推断:   点击叉后 它默认也是ONCANCEL关闭窗口

 

 

解决方法:  因为叉响应WM_COLOSE消息 因此为它创建一个关闭方式



void WeiKuangKe::OnClose()
{
	// TODO: Add your message handler code here and/or call default   
	
		int nRet=5;
		EndDialog(nRet);

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC窗体中显示OpenCV的图像,可以通过以下步骤实现: 1. 在MFC项目中添加OpenCV库。可以通过NuGet包管理器添加OpenCV的核心组件和高级计算机视觉组件。 2. 创建一个MFC Dialog或者MFC窗体应用程序。 3. 在窗体类的头文件中包含OpenCV的头文件。 4. 在窗体类的成员变量中声明一个用于存储图像数据的Mat对象。例如:cv::Mat m_image; 5. 在OnInitDialog或者OnInitialUpdate函数中初始化OpenCV并加载图像。例如: ``` cv::Mat img = cv::imread("image.jpg"); if (img.empty()) { AfxMessageBox(_T("Failed to load image")); } else { m_image = img.clone(); } ``` 6. 在窗体类中重写OnPaint函数,通过CDC对象绘制图像。例如: ``` void CMyDialog::OnPaint() { CPaintDC dc(this); // device context for painting CRect rect; GetClientRect(&rect); if (!m_image.empty()) { cv::Mat imgToShow; cv::cvtColor(m_image, imgToShow, CV_BGR2RGB); // OpenCV颜色通道顺序为BGR,转换为RGB // 创建一个位图并加载图像数据 BITMAPINFO bmInfo; memset(&bmInfo, 0, sizeof(BITMAPINFO)); bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmInfo.bmiHeader.biWidth = imgToShow.cols; bmInfo.bmiHeader.biHeight = -imgToShow.rows; // 负值表示图像是从顶部开始绘制的 bmInfo.bmiHeader.biPlanes = 1; bmInfo.bmiHeader.biBitCount = 24; bmInfo.bmiHeader.biCompression = BI_RGB; bmInfo.bmiHeader.biSizeImage = imgToShow.cols * imgToShow.rows * 3; SetDIBitsToDevice(dc.m_hDC, rect.left, rect.top, rect.Width(), rect.Height(), 0, 0, 0, imgToShow.rows, imgToShow.data, &bmInfo, DIB_RGB_COLORS); } } ``` 7. 调用Invalidate函数更新窗体内容。例如,在加载图像后调用Invalidate();。 以上是在MFC窗体中显示OpenCV图像的基本步骤,你需要根据具体的应用场景和需求进行适当调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值