UI Thread in MFC

debut.cis.nctu.edu.tw
  • No tags were found...

UI Thread in MFC

UI Thread in MFC參 料 :1. MFC 142. Chapter 17,Windows Programming with MFC, Second Edition


thread 來 行 message – loop Windowmessage.1WinMain functionwhile (GetMessage (&msg, NULL, 0, 0)) {TranslateMessage (&msg) ;Message-loopDispatchMessage (&msg) ;}… WndProc ( …){switch (message){case WM_xxx:}}WinProc function2void CtestDlg::OnBnClickedOk(){}3


不 , 1WinMain functionwhile (GetMessage (&msg, NULL, 0, 0)) {TranslateMessage (&msg) ;Message-loopDispatchMessage (&msg) ;}… WndProc ( …){switch (message){case WM_xxx:}}WinProc function2void CtestDlg::OnBnClickedOk(){}while(true); 行Message-loop 不 3不


Introduction thread 立 , message - loop• UI Thread 來 理 UI Thread類 thread 立 , 行 function– Message – loop– CWinThread, afxBeginThread threadStep 1: class class MyUIThread: public public CWinThread{DECLARE_DYNCREATE (MyUIThread);public: public:Step 2: 立 BOOL BOOL InitInstance (){ (){m_pMainWnd = new new MyFrame();m_pMainWnd->ShowWindow (SW_SHOW);}}; };IMPLEMENT_DYNCREATE (MyUIThread, CWinThread)


UI ThreadUI thread 類 CWinThread* pThread = AfxBeginThread (RUNTIME_CLASS (MyUIThread));afxBeginThread CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass, thread int nPriority = THREAD_PRIORITY_NORMAL,UINT nStackSize = 0, thread 立 stack sizeDWORD dwCreateFlags = 0, 立 LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL) thread security


thread?• WM_QUIT Threadmessage queue PostMessage(WM_QUIT);PostMessage(WM_CLOSE);CWnd::OnNcDestroy post WM_QUIT thread message queue.


來 例 立 立 threadPostMessage(WM_CLOSE); UI threadCWinThread* pThread;pThread = AfxBeginThread (RUNTIME_CLASS (MyUIThread));


: message event// A. 類 UI thread // 類 class MyFrame: public CFrameWnd{DECLARE_MESSAGE_MAP()CButton MyButton, ExitButton;public:MyFrame(){ 立 立 離 Create(NULL,"Hello MFC"); // 立 MyButton.Create("My button",WS_CHILD|WS_VISIBLE, \CRect(10,10,100,30),this,123);ExitButton.Create(" thread",WS_CHILD|WS_VISIBLE, \CRect(10,40,300,60),this,567); }afx_msg void OnBnClickedOk(){MessageBox("test");} 立 function};afx_msg void OnExitButtonClickedOk(){ // ThreadPostMessage (WM_QUIT);}


: , OnBnClickedOk() 理BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)ON_BN_CLICKED(123, OnBnClickedOk)ON_BN_CLICKED(567, OnExitButtonClickedOk)END_MESSAGE_MAP()


UI class 例Step 1:UI 類 ( CWinThread)class MyUIThread : public CWinThread{DECLARE_DYNCREATE (MyUIThread)public:BOOL InitInstance ();};IMPLEMENT_DYNCREATE (MyUIThread, CWinThread)Step 2: UI thread 行 method 裡 , BOOL MyUIThread::InitInstance () {m_pMainWnd = new MyFrame(); // 立 m_pMainWnd->ShowWindow(SW_SHOW); // 來m_pMainWnd->UpdateWindow(); // WM_PAINT message return TRUE;}


UI thread // 利 afxBeginThreadUI thread 例void CMyUIThreadDemoDlg::OnBnClickedOk(){CWinThread* pThread = AfxBeginThread (RUNTIME_CLASS (MyUIThread));}


• End

More magazines by this user
Similar magazines