请问一个dialog在doModal后如何显示一个MessageBox?

iCharlene 2007-09-11 11:10:09
希望在doModal后停顿3s,然后显示一个MessageBox~~
不知道如何实现,实在是没发现有什么消息被传递
求教~~
...全文
124 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qeq2008 2007-09-12
  • 打赏
  • 举报
回复
不会吧一定用定时器吗?
用Sleep(3000);
iCharlene 2007-09-12
  • 打赏
  • 举报
回复
我试试WM_ACTIVATE看
我是苦力 2007-09-12
  • 打赏
  • 举报
回复
呵呵,开个线程加个settimer,不要说没有窗口就不能加settimer哦
lushiyun 2007-09-12
  • 打赏
  • 举报
回复
把定时器直接加到DOMODAL后,或者用活动窗口消息响应WM_ACTIVATE,还是可以的
iCharlene 2007-09-11
  • 打赏
  • 举报
回复
我晕,定时器加哪去啊???
难道加到init里?
我在dialog的init里边还加了些访问网络的代码,等返回的时间
可能都不止3s了,这可是个土办法~
如果有什么窗口/对话框创建完毕的消息就好了
gpgty 2007-09-11
  • 打赏
  • 举报
回复
设置个3s的定时器,响应WM_TIMER消息,在响应函数里面加Messagebox,再关掉定时器就可以了
No.4 简单的MFC多对话框演示程序 “MultiDialog” 演示如何在工程中拥有多个对话框,及如何在一个对话框中调用另一个对话框。 重点:1、多个对话框类的建立;2、对话框的模式(Modal)调用方法;3、Spin控件的使用。 新建一个基于对话框的MFC工程, 通过菜单"Insert>>Resource"打开添加资源对话框, 在其中选择Dialog后点击New创建一个新的对话框 按Ctrl+W打开ClassWizard,系统会提示刚才创建了一个新的对话框资源,是否建立对应的类, 选择建立,然后在New Class窗口中Class Name栏输入它的名称:CSubClass1, 确定后系统会自动生成SubClass1.h和SubClass1.cpp并加入工程中,其中有已经创建好的CSubClass1的类的基本代码。 把这个对话框的Caption属性改为“难度选择”,在它上面画三个Radio“简单”、“标准”、“困难”, 并建立相关联的变量m_Option1。(要注意的是在ClassWizard中注意Class Name中应该选CSubClass1而不是之前的主对话框类) 按照相同方法建立第二个新对话框,类名“CSubClass2”,Caption为“关卡选择”。 在上面画一个Edit和一个Spin,注意先画Edit后画Spin,将Spin的Auto buddy和Set buddy integer勾上。 按Ctrl+W打开ClassWizard,为Edit建立关联变量,不过注意是int型而不是CString型, 也为Spin建立关联变量m_Spin1,注意这次是Control型变量CSpinButtonCtrl。 下面为这两个对话框添加代码。 双击“难度选择”对话框的OK按钮,建立对话框的OnOK映射。 在其中加入(在CDialog::OnOK();之前): UpdateData(TRUE); if ((m_Option1>2) ||(m_Option1<0)) { MessageBox("错误的选择!","提示",MB_OK); return; } 下面对“关卡选择”对话框添加初始化代码, 由于关卡的有效值只有1到6,因此需要在初始化时设置Spin控件的有效值范围。 按Ctrl+W打开ClassWizard,在左侧列表选择这个对话框类CSubDialog2, 在右侧列表中选择WM_INITDIALOG,点击右边的“Add Function...”按钮, 接着点击右边的“Edit Code”按钮,在其中中加入(在CDialog::OnInitDialog()那句之后,在return TRUE那句之前): m_Spin1.SetRange(1,6); m_Text1=1; m_Spin1.SetPos(1); 其中CSpinButtonCtrl::SetRange()函数的作用是设置和他关联的Spin控件的范围,两个参数分别是下界和上界。 而CSpinButtonCtrl::SetPos()是设定Spin的当前位置。 两个新的对话框都已建立完毕,下来是如何在主对话框中使用的问题。 首先,两个新对话框都有各自的类,分别在SubDialog1.h和SubDialog2.h中有定义。(类的细节则在对应的cpp中定义) 因此,主对话框想要调用这两个新对话框,需要先包含这两个头文件, 在你要使用的地方(本例是MultiDialogDlg.cpp中)文件前面加上 #include "SubDialog1.h" #include "SubDialog2.h" 然后在想要调用的地方就可以使用了。 本例中,首先为主窗口的两个Edit建立CString型关联变量m_Text1和m_Text2, 然后在两个按钮的消息映射函数中分别加入: CSubDialog1 dialog1; //定义CSubDialog1型对话框的一个新对象 dialog1.DoModal(); //使用“模式”调用,显示对话框 m_Text1.Format("%d",dialog1.m_Option1); //此句在上面对话框没有关闭前不会执行到 UpdateData(FALSE); 和 CSubDialog2 dialog1; dialog1.DoModal(); m_Text2.Format("%d",dialog1.m_Text1); UpdateData(FALSE); 其中第一句均为定义对话框新实例的语句,定义一个你想要的类型的对话框。 第二句是通过调用CDialog::DoModal()方法,来显示这个对话框,并进入“模式”(Modal)状态 在“模式”状态,当子对话框没有关闭之前,调用它的父对话框不能被响应, 并且其语句执行会停留在刚才的DoModal语句上等待,直到子对话框关闭才接着执行下一个语句。 第三第四句将子对话框得到的数据(即类的成员变量)显示在父对话框的Edit上。 四句执行完后退出该函数,这时刚才定义的CSubDialog1等对话框类变量被销毁,因此创建的话框也被销毁。
显示我的文档路径,// WENDANGDlg.cpp : implementation file // #include "stdafx.h" #include "WENDANG.h" #include "WENDANGDlg.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() ///////////////////////////////////////////////////////////////////////////// // CWENDANGDlg dialog CWENDANGDlg::CWENDANGDlg(CWnd* pParent /*=NULL*/) : CDialog(CWENDANGDlg::IDD, pParent) { //{{AFX_DATA_INIT(CWENDANGDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CWENDANGDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CWENDANGDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CWENDANGDlg, CDialog) //{{AFX_MSG_MAP(CWENDANGDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CWENDANGDlg message handlers BOOL CWENDANGDlg::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 return TRUE; // return TRUE unless you set the focus to a control } void CWENDANGDlg::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 CWENDANGDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (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(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CWENDANGDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CWENDANGDlg::OnOK() { char m_lpszDefaultDir[MAX_PATH]; char szDocument[MAX_PATH]={0}; memset(m_lpszDefaultDir,0,_MAX_PATH); LPITEMIDLIST pidl=NULL; SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl); if (pidl && SHGetPathFromIDList(pidl, szDocument)) { GetShortPathName(szDocument,m_lpszDefaultDir,_MAX_PATH); } MessageBox(m_lpszDefaultDir); }
基于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(); }

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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