ThreadDlg.cpp
上传用户:swkcbjrc
上传日期:2016-04-02
资源大小:45277k
文件大小:5k
源码类别:

游戏

开发平台:

Visual C++

  1. // ThreadDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Thread.h"
  5. #include "ThreadDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. CWorkThreadPool *g_pPool=NULL;
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CThreadDlg dialog
  51. CThreadDlg::CThreadDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CThreadDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CThreadDlg)
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CThreadDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CThreadDlg)
  63. DDX_Control(pDX, IDC_LIST1, m_list);
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CThreadDlg, CDialog)
  67. //{{AFX_MSG_MAP(CThreadDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_WM_DESTROY()
  72. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CThreadDlg message handlers
  77. BOOL CThreadDlg::OnInitDialog()
  78. {
  79. CDialog::OnInitDialog();
  80. // Add "About..." menu item to system menu.
  81. // IDM_ABOUTBOX must be in the system command range.
  82. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  83. ASSERT(IDM_ABOUTBOX < 0xF000);
  84. CMenu* pSysMenu = GetSystemMenu(FALSE);
  85. if (pSysMenu != NULL)
  86. {
  87. CString strAboutMenu;
  88. strAboutMenu.LoadString(IDS_ABOUTBOX);
  89. if (!strAboutMenu.IsEmpty())
  90. {
  91. pSysMenu->AppendMenu(MF_SEPARATOR);
  92. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  93. }
  94. }
  95. // Set the icon for this dialog.  The framework does this automatically
  96. //  when the application's main window is not a dialog
  97. SetIcon(m_hIcon, TRUE); // Set big icon
  98. SetIcon(m_hIcon, FALSE); // Set small icon
  99. g_pPool=new CWorkThreadPool();
  100. g_pPool->SetPF(Test);
  101. g_pPool->Run();
  102. // TODO: Add extra initialization here
  103. return TRUE;  // return TRUE  unless you set the focus to a control
  104. }
  105. void CThreadDlg::OnSysCommand(UINT nID, LPARAM lParam)
  106. {
  107. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  108. {
  109. CAboutDlg dlgAbout;
  110. dlgAbout.DoModal();
  111. }
  112. else
  113. {
  114. CDialog::OnSysCommand(nID, lParam);
  115. }
  116. }
  117. // If you add a minimize button to your dialog, you will need the code below
  118. //  to draw the icon.  For MFC applications using the document/view model,
  119. //  this is automatically done for you by the framework.
  120. void CThreadDlg::OnPaint() 
  121. {
  122. if (IsIconic())
  123. {
  124. CPaintDC dc(this); // device context for painting
  125. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  126. // Center icon in client rectangle
  127. int cxIcon = GetSystemMetrics(SM_CXICON);
  128. int cyIcon = GetSystemMetrics(SM_CYICON);
  129. CRect rect;
  130. GetClientRect(&rect);
  131. int x = (rect.Width() - cxIcon + 1) / 2;
  132. int y = (rect.Height() - cyIcon + 1) / 2;
  133. // Draw the icon
  134. dc.DrawIcon(x, y, m_hIcon);
  135. }
  136. else
  137. {
  138. CDialog::OnPaint();
  139. }
  140. }
  141. // The system calls this to obtain the cursor to display while the user drags
  142. //  the minimized window.
  143. HCURSOR CThreadDlg::OnQueryDragIcon()
  144. {
  145. return (HCURSOR) m_hIcon;
  146. }
  147. void CThreadDlg::OnDestroy() 
  148. {
  149. CDialog::OnDestroy();
  150. }
  151. void CThreadDlg::Test(LPVOID lpParam)
  152. {
  153. CThreadDlg * pDlg = (CThreadDlg* )lpParam;
  154. DWORD id = GetCurrentThreadId();
  155. CString str;
  156. str.Format("work thread is working %xn",id);
  157. pDlg->m_list.AddString(str);
  158. Sleep(100);
  159. }
  160. void CThreadDlg::OnButton1() 
  161. {
  162. for(int i=0;i<100;i++)
  163. {
  164. g_pPool->RegisterWorkItem(this);
  165. }
  166. }
  167. void CThreadDlg::OnOK() 
  168. {
  169. delete g_pPool;
  170. g_pPool=NULL;
  171. ::PostQuitMessage(0);
  172. CDialog::OnOK();
  173. }