13.07.2015 Views

UI Thread in MFC

UI Thread in MFC

UI Thread in MFC

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>UI</strong> <strong>Thread</strong> <strong>in</strong> <strong>MFC</strong>參 料 :1. <strong>MFC</strong> 142. Chapter 17,W<strong>in</strong>dows Programm<strong>in</strong>g with <strong>MFC</strong>, Second Edition


thread 來 行 message – loop W<strong>in</strong>dowmessage.1W<strong>in</strong>Ma<strong>in</strong> functionwhile (GetMessage (&msg, NULL, 0, 0)) {TranslateMessage (&msg) ;Message-loopDispatchMessage (&msg) ;}… WndProc ( …){switch (message){case WM_xxx:}}W<strong>in</strong>Proc function2void CtestDlg::OnBnClickedOk(){}3


不 , 1W<strong>in</strong>Ma<strong>in</strong> functionwhile (GetMessage (&msg, NULL, 0, 0)) {TranslateMessage (&msg) ;Message-loopDispatchMessage (&msg) ;}… WndProc ( …){switch (message){case WM_xxx:}}W<strong>in</strong>Proc function2void CtestDlg::OnBnClickedOk(){}while(true); 行Message-loop 不 3不


Introduction thread 立 , message - loop• <strong>UI</strong> <strong>Thread</strong> 來 理 <strong>UI</strong> <strong>Thread</strong>類 thread 立 , 行 function– Message – loop– CW<strong>in</strong><strong>Thread</strong>, afxBeg<strong>in</strong><strong>Thread</strong> threadStep 1: class class My<strong>UI</strong><strong>Thread</strong>: public public CW<strong>in</strong><strong>Thread</strong>{DECLARE_DYNCREATE (My<strong>UI</strong><strong>Thread</strong>);public: public:Step 2: 立 BOOL BOOL InitInstance (){ (){m_pMa<strong>in</strong>Wnd = new new MyFrame();m_pMa<strong>in</strong>Wnd->ShowW<strong>in</strong>dow (SW_SHOW);}}; };IMPLEMENT_DYNCREATE (My<strong>UI</strong><strong>Thread</strong>, CW<strong>in</strong><strong>Thread</strong>)


立 <strong>UI</strong> <strong>Thread</strong><strong>UI</strong> thread 類 CW<strong>in</strong><strong>Thread</strong>* p<strong>Thread</strong> = AfxBeg<strong>in</strong><strong>Thread</strong> (RUNTIME_CLASS (My<strong>UI</strong><strong>Thread</strong>));afxBeg<strong>in</strong><strong>Thread</strong> CW<strong>in</strong><strong>Thread</strong>* AFXAPI AfxBeg<strong>in</strong><strong>Thread</strong>(CRuntimeClass* p<strong>Thread</strong>Class, thread <strong>in</strong>t nPriority = THREAD_PRIORITY_NORMAL,<strong>UI</strong>NT nStackSize = 0, thread 立 stack sizeDWORD dwCreateFlags = 0, 立 LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL) thread security


thread?• WM_Q<strong>UI</strong>T <strong>Thread</strong>message queue PostMessage(WM_Q<strong>UI</strong>T);PostMessage(WM_CLOSE);CWnd::OnNcDestroy post WM_Q<strong>UI</strong>T thread message queue.


來 例 立 立 threadPostMessage(WM_CLOSE); <strong>UI</strong> threadCW<strong>in</strong><strong>Thread</strong>* p<strong>Thread</strong>;p<strong>Thread</strong> = AfxBeg<strong>in</strong><strong>Thread</strong> (RUNTIME_CLASS (My<strong>UI</strong><strong>Thread</strong>));


: message event// A. 類 <strong>UI</strong> thread // 類 class MyFrame: public CFrameWnd{DECLARE_MESSAGE_MAP()CButton MyButton, ExitButton;public:MyFrame(){ 立 立 離 Create(NULL,"Hello <strong>MFC</strong>"); // 立 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(){ // <strong>Thread</strong>PostMessage (WM_Q<strong>UI</strong>T);}


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


<strong>UI</strong> class 例Step 1:<strong>UI</strong> 類 ( CW<strong>in</strong><strong>Thread</strong>)class My<strong>UI</strong><strong>Thread</strong> : public CW<strong>in</strong><strong>Thread</strong>{DECLARE_DYNCREATE (My<strong>UI</strong><strong>Thread</strong>)public:BOOL InitInstance ();};IMPLEMENT_DYNCREATE (My<strong>UI</strong><strong>Thread</strong>, CW<strong>in</strong><strong>Thread</strong>)Step 2: <strong>UI</strong> thread 行 method 裡 , BOOL My<strong>UI</strong><strong>Thread</strong>::InitInstance () {m_pMa<strong>in</strong>Wnd = new MyFrame(); // 立 m_pMa<strong>in</strong>Wnd->ShowW<strong>in</strong>dow(SW_SHOW); // 來m_pMa<strong>in</strong>Wnd->UpdateW<strong>in</strong>dow(); // WM_PAINT message return TRUE;}


<strong>UI</strong> thread // 利 afxBeg<strong>in</strong><strong>Thread</strong> 立 <strong>UI</strong> thread 例void CMy<strong>UI</strong><strong>Thread</strong>DemoDlg::OnBnClickedOk(){CW<strong>in</strong><strong>Thread</strong>* p<strong>Thread</strong> = AfxBeg<strong>in</strong><strong>Thread</strong> (RUNTIME_CLASS (My<strong>UI</strong><strong>Thread</strong>));}


• End

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!