#if !defined(AFX_BASEWORKTHREAD_H__D104C15C_8BCD_475B_91C4_4960EBE866A4__INCLUDED_) #define AFX_BASEWORKTHREAD_H__D104C15C_8BCD_475B_91C4_4960EBE866A4__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // BaseWorkThread.h : header file #include "DialogLoading.h" // ///////////////////////////////////////////////////////////////////////////// // BaseWorkThread window //iori 多线程 #define WM_ONTHREADFINISHED (WM_USER + 199) class BaseWorkThread : public CWnd { // Construction public: BaseWorkThread(CWnd* cWnd); // Attributes public: CDialogLoading dlgLoading; BOOL bIsShowModal; // Operations public: CWinThread* startWork(); static UINT innerBeginThread(BaseWorkThread* instance); //内部静态方法,用于启动工作线程 virtual LRESULT onWorkerFinished(BaseWorkThread* instance); // 线程加载完成的回调函数体 // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(BaseWorkThread) //}}AFX_VIRTUAL // Implementation public: virtual ~BaseWorkThread(); // Generated message map functions protected: CWinThread *m_pThread; virtual UINT worker() ; // 工作线程调用的实现体 2022-03-11 //{{AFX_MSG(BaseWorkThread) // NOTE - the ClassWizard will add and remove member functions here. afx_msg LRESULT workder_onFinished(WPARAM wParam, LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_BASEWORKTHREAD_H__D104C15C_8BCD_475B_91C4_4960EBE866A4__INCLUDED_)
// BaseWorkThread.cpp : implementation file // #include "stdafx.h" #include "BaseWorkThread.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // BaseWorkThread BaseWorkThread::BaseWorkThread(CWnd* pParent) { this->bIsShowModal = FALSE; CWnd::Create(NULL,_T("MySocketManage"),WS_CHILD,CRect(0,0,0,0), pParent,1234); } BaseWorkThread::~BaseWorkThread() { } CWinThread* BaseWorkThread::startWork() { CWinThread *m_pThread = NULL; if(this->bIsShowModal) { m_pThread = ::AfxBeginThread((AFX_THREADPROC)BaseWorkThread::innerBeginThread, this); dlgLoading.ShowModal(); return m_pThread; } BOOL result = dlgLoading.Show(); if(result) { m_pThread = ::AfxBeginThread((AFX_THREADPROC)BaseWorkThread::innerBeginThread, this); } return m_pThread; } UINT BaseWorkThread::innerBeginThread(BaseWorkThread* instance) { return instance->worker(); } UINT BaseWorkThread::worker() { Sleep(5000); HINSTANCE hInst=0; SHELLEXECUTEINFO sInfo; sInfo.hwnd=NULL; sInfo.lpVerb="open"; sInfo.lpFile="SETUP.exe"; sInfo.lpParameters=""; sInfo.lpDirectory="./debug/"; sInfo.nShow=SW_SHOWNORMAL; sInfo.hInstApp=hInst; sInfo.cbSize=sizeof(SHELLEXECUTEINFO); sInfo.fMask=SEE_MASK_NOCLOSEPROCESS; //ShellExecuteEx(&sInfo); //WaitForSingleObject(sInfo.hProcess, INFINITE ); /* 如果某个软件是用 Windows Installer 打包的,那你就应该能在文件夹中看到 *.msi 文件。这是最典型的特征,这些文件通常可以使用 /QB 和 /QN 参数进行自动安装。 /qb 会在窗口中显示一个基本的安装进程。 /qn 参数则不会显示任何窗口,直接在后台自动安装。 为了阻止某些程序安装成功后自动重启动(例如 Kerio Personal Firewall 4),你可以在 /qn 或者 /qb参数后使用REBOOT=Suppress标记。 例如:安装虚拟光驱 DaemonTools:msiexec /i dtools.msi /qb REBOOT=SUPPRESS */ ::PostMessage(this->m_hWnd, WM_ONTHREADFINISHED, 1, 1); return 0; //成功返回 } LRESULT BaseWorkThread::workder_onFinished(WPARAM wParam, LPARAM lParam) { if(this->dlgLoading) { if(this->bIsShowModal) { this->dlgLoading.EndDialog(1); } else { this->dlgLoading.ShowWindow(0); //this->dlgLoading.DestroyWindow(); 非模式执行完一次扣 对话框的 Hwnd 被清除,导至下次产生不了窗口 } } return this->onWorkerFinished(this); } LRESULT BaseWorkThread::onWorkerFinished(BaseWorkThread* instance) { ::AfxMessageBox("线程完成!", 0, 0); return 0; } BEGIN_MESSAGE_MAP(BaseWorkThread, CWnd) //{{AFX_MSG_MAP(BaseWorkThread) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP ON_MESSAGE(WM_ONTHREADFINISHED, workder_onFinished) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // BaseWorkThread message handlers
#if !defined(AFX_DIALOGLOADING_H__73F5F815_CCC7_42AF_B8C1_99B4F2A993E5__INCLUDED_) #define AFX_DIALOGLOADING_H__73F5F815_CCC7_42AF_B8C1_99B4F2A993E5__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // DialogLoading.h : header file // #include "Resource.h" ///////////////////////////////////////////////////////////////////////////// // CDialogLoading dialog class CDialogLoading : public CDialog { DECLARE_DYNAMIC(CDialogLoading) // Construction public: //CDialogLoading::CDialogLoading(); //下面的构造函数参数可为NULL, 所以可代表无参构造 CDialogLoading(CWnd* pParent = NULL); // standard constructor CDialogLoading::~CDialogLoading(); BOOL Show(); int ShowModal(); // Dialog Data //{{AFX_DATA(CDialogLoading) enum { IDD = IDD_DIALOGLoading }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CDialogLoading) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: BOOL bAlreadyInit; // Generated message map functions //{{AFX_MSG(CDialogLoading) virtual BOOL OnInitDialog(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_DIALOGLOADING_H__73F5F815_CCC7_42AF_B8C1_99B4F2A993E5__INCLUDED_)
// DialogLoading.cpp : implementation file // #include "stdafx.h" #include "ex08a.h" #include "DialogLoading.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNAMIC(CDialogLoading, CDialog) ///////////////////////////////////////////////////////////////////////////// // CDialogLoading dialog CDialogLoading::CDialogLoading(CWnd* pParent /*=NULL*/) : CDialog(CDialogLoading::IDD, pParent),bAlreadyInit(FALSE) { //{{AFX_DATA_INIT(CDialogLoading) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } CDialogLoading::~CDialogLoading() { } int CDialogLoading::ShowModal() { return this->DoModal(); } BOOL CDialogLoading::Show() { BOOL bResult = FALSE; if(this->bAlreadyInit != TRUE) { bResult = CDialog::Create(CDialogLoading::IDD); if(bResult) { bAlreadyInit = TRUE; this->CenterWindow(); this->ShowWindow(1); } } else { if(this->m_hWnd) { this->ShowWindow(1); return TRUE; } else { return FALSE; } } return bResult; } void CDialogLoading::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDialogLoading) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDialogLoading, CDialog) //{{AFX_MSG_MAP(CDialogLoading) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDialogLoading message handlers BOOL CDialogLoading::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here this->bAlreadyInit = FALSE; return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
void CEx08aView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default //CView::OnLButtonDown(nFlags, point); w = new BaseWorkThread(AfxGetMainWnd()/*this*/);
w->bIsShowModal = FALSE;
w->startWork();
}
Adding Dialog Controls at Runtime
You've seen how to use the resource editor to create dialog controls at build time. If you need to add a dialog control at runtime, here are the programming steps:
- Add an embedded control window data member to your dialog class. The MFC control window classes include CButton, CEdit, CListBox, and CComboBox. An embedded control C++ object is constructed and destroyed along with the dialog object.
- Choose Resource Symbols from Visual C++'s View menu. Add an ID constant for the new control.
- Use ClassWizard to map the WM_INITDIALOG message, thus overriding CDialog::OnInitDialog. This function should call the embedded control window's Create member function. This call displays the new control in the dialog. Windows will destroy the control window when it destroys the dialog window.
- In your derived dialog class, manually add the necessary notification message handlers for your new control.
In Chapter 13, you'll be adding a rich edit control to a view at runtime.
int CEx13aView::OnCreate(LPCREATESTRUCT lpCreateStruct) { CRect rect(0, 0, 0, 0); if (CView::OnCreate(lpCreateStruct) == -1) return -1; m_rich.Create(ES_AUTOVSCROLL | ES_MULTILINE | ES_WANTRETURN | WS_CHILD | WS_VISIBLE | WS_VSCROLL, rect, this, 1); return 0; }
标签:__,MAP,return,BaseWorkThread,静默,线程,基类,AFX,CDialogLoading From: https://www.cnblogs.com/ioriwellings/p/16113815.html