咨询一下CDialog::DoModal()

flyskytoday 2010-04-27 09:16:09
以前一直认为DoModal()是完全阻塞的,但发现自己的理解有错
如下测试
CDialogMsg MyClose; //在退出时,加提示,显示上有倒计时,自动关闭
MyClose.DoModal();//不完全阻塞
MessageBox("a");//这个是完全阻塞
上面三行代码,测试了几次
第二行很不稳定,有时能显示一下就关闭,有时一闪就自动转到第三行
第三行必须点击确认

想请教一下大家原因?
还有我想把第二行做成与第三行一样效果的东东,请问如何做?
...全文
320 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
finder_zhang 2010-04-29
  • 打赏
  • 举报
回复
就是因为你的定时器调用了 CDialog::OnOK() ,关窗的是这个函数.
flyskytoday 2010-04-29
  • 打赏
  • 举报
回复
感谢大家回复,是我错了,DoModal是阻塞模式
12楼说得对,是定时器造成的
gg606 2010-04-27
  • 打赏
  • 举报
回复
应该是定时器造成的
kydef 2010-04-27
  • 打赏
  • 举报
回复
参考MSDN,单步跟踪一下程序,看看流程...
Eleven 2010-04-27
  • 打赏
  • 举报
回复
CDilaog DoModal()阻塞当前的消息loop,MSDN:
Dialog boxes are of two types: modal and modeless. A modal dialog box must be closed by the user before the application continues. A modeless dialog box allows the user to display the dialog box and return to another task without canceling or removing the dialog box.

finder_zhang 2010-04-27
  • 打赏
  • 举报
回复
原来你调用了CDialog::OnOK() 这个函数就是用来关窗的,然后返回一个 IDOK 给调用 DoModal() 的代码
尹成 2010-04-27
  • 打赏
  • 举报
回复
DoModal()阻塞了,看看CDialog::OnOK()里面的具体内容
快乐鹦鹉 2010-04-27
  • 打赏
  • 举报
回复
盲目崇拜可不好。
flyskytoday 2010-04-27
  • 打赏
  • 举报
回复
^&^
[Quote=引用 4 楼 happyparrot 的回复:]
你原来的理解没有错,现在终于理解错了。
[/Quote]

在CDialogMsg中代码非常简单,只有一个定时器(倒计时),时间一到就调用CDialog::OnOK()
既然happyparrot都说DoModal是完全阻塞的,那就是完全阻塞,偶比较崇拜权威 呵呵
那现在更不知道原因了,,,也没什么大的问题,只是找不出原因,心里感觉不爽~~
[Quote=引用 5 楼 finder_zhang 的回复:]
DoModal 阻塞整个进程.你闪一下就到了第三行,是不是那个窗因为什么原因而关了?
[/Quote]
finder_zhang 2010-04-27
  • 打赏
  • 举报
回复
DoModal 阻塞整个进程.你闪一下就到了第三行,是不是那个窗因为什么原因而关了?
快乐鹦鹉 2010-04-27
  • 打赏
  • 举报
回复
你原来的理解没有错,现在终于理解错了。
快乐鹦鹉 2010-04-27
  • 打赏
  • 举报
回复
是你的CDialogMsg对话框类做的有问题......
flyskytoday 2010-04-27
  • 打赏
  • 举报
回复
就这三行代码,有问题吗?
对于DoModal的理解很浅
liang_lq 2010-04-27
  • 打赏
  • 举报
回复
DoModal()是阻塞的
“第二行很不稳定,有时能显示一下就关闭,有时一闪就自动转到第三行”,阻塞就阻塞,不阻塞就不阻塞,时而这样,时而那样,这很有可能是你代码的问题
// shujukuDlg.cpp : implementation file // #include "stdafx.h" #include "shujuku.h" #include "shujukuDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CShujukuDlg dialog CShujukuDlg::CShujukuDlg(CWnd* pParent /*=NULL*/) : CDialog(CShujukuDlg::IDD, pParent) { //{{AFX_DATA_INIT(CShujukuDlg) m_shuju = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CShujukuDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CShujukuDlg) DDX_Text(pDX, IDC_EDIT1, m_shuju); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CShujukuDlg, CDialog) //{{AFX_MSG_MAP(CShujukuDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CShujukuDlg message handlers BOOL CShujukuDlg::OnInitDialog() { CDial
词法分析// TranslationDlg.cpp : 实现文件 // #include "stdafx.h" #include "Translation.h" #include "TranslationDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 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() // CTranslationDlg 对话框 CTranslationDlg::CTranslationDlg(CWnd* pParent /*=NULL*/) : CDialog(CTranslationDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CTranslationDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT2, content); DDX_Control(pDX, IDC_EDIT1, result); } BEGIN_MESSAGE_MAP(CTranslationDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_BUTTON1, &CTranslationDlg::OnBnClickedButton1) END_MESSAGE_MAP() // CTranslationDlg 消息处理程序 BOOL CTranslationDlg::OnInitDialog() { CDialog::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 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); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 CAboutDlg dlgAbout; dlgAbout.DoModal(); return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CTranslationDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CTranslationDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CTranslationDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CTranslationDlg::SplideFrontSpc (CString &str) { int i = 0; for(i;str[i]==' ';) {str.Delete (0);}//MessageBox (_T("ok")); } void CTranslationDlg::OnBnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 CString code ,temp ,output; //CString word[8] = ; CString flag[3][10] = {{_T("if"),_T("int"),_T("for"),_T("while"),_T("do"),_T("return"),_T("break"),_T("continue")}, {_T("+"),_T("-"),_T("*"),_T("/"),_T("="),_T(">"),_T("<"),_T("<="),_T(">="),_T("!=")}, { _T(","),_T(";"),_T("{"),_T("}"),_T("("),_T(")")}}; content.GetWindowTextW (code); code.Replace (_T("\r\n"),_T("")); code.Append (_T(" ")); while(!code.IsEmpty ()) { temp.Empty (); int i,j; int isfind = 0;//是否找到,找到了代表其类型 SplideFrontSpc(code); //temp = code.Left (code.Find (_T(","))); //code.Delete (0,2);//temp = code[0]; //截取下一个单词 temp = code.Left (code.Find (_T(" "))); code.Delete (0,code.Find (_T(" "))); //对比单词类型 for(i=0;!isfind&&i<3;i++) for(j=0;j<10;j++) if(temp == flag[i][j]) { if(i==0) isfind = 1; else if(i==1) isfind = 4; else isfind = 5; break; } if(isfind==0) { int isnum = temp[0]-'0'; if(isnum>-1||isnum<10) isfind = 3; else isfind = 2; } CString cnum; cnum.Format (_T("%d"),isfind); if(!temp.IsEmpty ()) temp = _T("(") + cnum + _T(", \"") + temp + _T("\")"); else temp = _T("词法分析完毕!\r\n"); output = output + temp + _T("\r\n"); } //output = _T("asdjfk\r\naksdjfl\nasldkfj\n"); result.SetWindowTextW (output); } BOOL CTranslationDlg::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 return CDialog::PreTranslateMessage(pMsg); } void CTranslationDlg::OnOK() { // TODO: 在此添加专用代码和/或调用基类 CDialog::OnOK(); }
基于DirectSound的简易播放器 #include "stdafx.h" #include "test.h" #include "testDlg.h" #include "process.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation 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() // CtestDlg dialog CtestDlg::CtestDlg(CWnd* pParent /*=NULL*/) : CDialog(CtestDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CtestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CtestDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_BUTTON_OPEN, &CtestDlg::OnBnClickedButtonOpen) ON_BN_CLICKED(IDC_BUTTON_PLAY, &CtestDlg::OnBnClickedButtonPlay) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON_STOP, &CtestDlg::OnBnClickedButtonStop) END_MESSAGE_MAP() //ON_BN_CLICKED(IDC_Sound_Play, OnSound2) //ON_BN_CLICKED(IDC_Sound_stop, OnSound1) //ON_BN_CLICKED(IDC_sound_pause, OnBothSounds) //ON_WM_DESTROY() //ON_BN_CLICKED(IDC_BUTTON_Slow, OnBUTTONSlow) //ON_BN_CLICKED(IDC_BUTTON_Fast, OnBUTTONFast) //ON_BN_CLICKED(IDC_BUTTON_Normal, OnBUTTONNormal) // CtestDlg message handlers BOOL CtestDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. 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); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_sndSound1 = new CDirectSound(); if(!m_sndSound1) { exit(-1); } return TRUE; // return TRUE unless you set the focus to a control } void CtestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CtestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CtestDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } //--------------------------------------------------------------------------------- void CtestDlg::OnBnClickedButtonOpen() { // TODO: Add your control notification handler code here LPCTSTR lpszFilter =L"Wave File(*.wav)|*.wav|All Files|*.*||"; CFileDialog dlg(TRUE,NULL,NULL,/*OFN_HIDEREADONLY |*/ OFN_OVERWRITEPROMPT,lpszFilter); if(dlg.DoModal()==IDOK) { m_soundfile = dlg.GetPathName(); } } // unsigned int WINAPI FileReadProc(LPVOID pOwner) { CtestDlg* pThis = (CtestDlg*)pOwner; pThis->ReadFileProc(); return 1; } // typedef struct _WAVE_FORMAT { WORD AudioFormat; WORD NumChannels; DWORD SampleRate; DWORD ByteRate; WORD BlockAlign; WORD BitsPerSample; }WAVE_FORMAT,*PWAVE_FORMAT; void CtestDlg::OnBnClickedButtonPlay() { // TODO: Add your control notification handler code here if(m_soundfile.IsEmpty()) { MessageBox(L"请选中播放文件!"); return; } USES_CONVERSION; fp = fopen(W2A(m_soundfile.GetBuffer(m_soundfile.GetLength())),"rb"); if(NULL == fp) { MessageBox(L"打开所所定的播放文件失败,请确认文件是否存在!"); return; } fseek(fp,20,0); //Skip previous 20 bytes RIFF header WAVE_FORMAT waveFormat; int nLen =fread(&waveFormat,1,sizeof(WAVE_FORMAT),fp); AUDIO_CONFIG WaveHead; WaveHead.wFormatTag = 1; WaveHead.nChannels = 2; WaveHead.nSamplesPerSec = 44100; WaveHead.nAvgBytesPerSec = 176400; WaveHead.nBlockAlign = 4; WaveHead.wBitsPerSample = 16; WaveHead.nBlockAlign=waveFormat.BlockAlign; WaveHead.nChannels=waveFormat.NumChannels; WaveHead.nSamplesPerSec=waveFormat.SampleRate; WaveHead.wBitsPerSample=waveFormat.BitsPerSample; WaveHead.nAvgBytesPerSec=waveFormat.ByteRate; m_sndSound1->CreateDSound(&WaveHead,8000); fseek(fp,20+sizeof(WAVE_FORMAT),0); unsigned int dwReadID; m_hThread = (HANDLE)_beginthreadex(0,0,FileReadProc,this,0,&dwReadID); Sleep(200); m_bStop = 0; m_bContine = 1; m_sndSound1->Play(); } void CtestDlg::ReadFileProc() { //BYTE buf[1025]; int n = 0; while(1) { BYTE buf[1025]; int nlen; if(m_bStop) break; nlen = fread(buf,1,1024,fp); if(!nlen) break; while(m_sndSound1->WriteDataToBuf(buf,nlen)==-1) { Sleep(100); } } if(fp) { fclose(fp); fp = NULL; } } void CtestDlg::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here m_bStop = 1; OnBnClickedButtonStop(); if(m_hThread) { WaitForSingleObject(m_hThread,2000); CloseHandle(m_hThread); m_hThread = 0; } if(m_sndSound1) { delete m_sndSound1; m_sndSound1 = NULL; } if(fp) { fclose(fp); fp = NULL; } } void CtestDlg::OnBnClickedButtonStop() { // TODO: Add your control notification handler code here m_bStop = 1; if(m_hThread) { WaitForSingleObject(m_hThread,2000); CloseHandle(m_hThread); m_hThread = NULL; } m_sndSound1->Stop(); }

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧