⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 20. 多任务和多线程.txt

📁 本书介绍了在Microsoft Windows 98、Microsoft Windows NT 4.0和Windows NT 5.0下程序写作的方法
💻 TXT
📖 第 1 页 / 共 5 页
字号:
           int                   iNum = 0, iNext = 1, iLine = 0, iTemp ;
        
           PPARAMS               pparams ;
        
           TCHAR                         szBuffer[16] ;
        
   
        
           pparams = (PPARAMS) pvoid ;
        
           while (!pparams->bKill)
        
    {
        
                          if (iNum < 0)
        
                          {
        
                                         iNum  = 0 ;
        
                                         iNext = 1 ;
        
                  }
        
                  iLine = CheckBottom ( pparams->hwnd,        pparams->cyClient,
        
   pparams->cyChar, iLine) ;
        

                  hdc = GetDC (pparams->hwnd) ;
        
        
        
                  TextOut (hdc, 0, iLine * pparams->cyChar, szBuffer,
        
                                                         wsprintf (szBuffer, TEXT ("%d"), iNum)) ;
        
        
        
                ReleaseDC (pparams->hwnd, hdc) ;
        
                  iTemp  = iNum ;
        
                  iNum   = iNext ;
        
                  iNext += iTemp ;
        
                  iLine++ ;
        
           }
        
           _endthread () ;
        
}
        

LRESULT APIENTRY WndProc3 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
        
{
        
           static PARAMS params ;
        
           switch (message)
        
           {
        
           case   WM_CREATE:
        
                          params.hwnd = hwnd ;
        
                          params.cyChar = HIWORD (GetDialogBaseUnits ()) ;
        
                          _beginthread (Thread3, 0, 秏s) ;
        
                          return 0 ;
        

           case   WM_SIZE:
        
                          params.cyClient = HIWORD (lParam) ;
        
                          return 0 ;
        
        
        
           case   WM_DESTROY:
        
                          params.bKill = TRUE ;
        
                          return 0 ;
        
           }
        
           return DefWindowProc (hwnd, message, wParam, lParam) ;
        
}
        

// -------------------------------------------------------------------------
        
// Window 4: Display circles of random radii
        
// -------------------------------------------------------------------------
        

void Thread4 (PVOID pvoid)
        
{
        
           HDC                           hdc ;
        
           int                           iDiameter ;
        
           PPARAMS               pparams ;
        
   
        
           pparams = (PPARAMS) pvoid ;
        
           while (!pparams->bKill)
        
           {
        
                  InvalidateRect (pparams->hwnd, NULL, TRUE) ;
        
                  UpdateWindow (pparams->hwnd) ;
        
        
        
                  iDiameter =   rand() % (max (1,
        
               min (pparams->cxClient, pparams->cyClient))) ;
        
        
        
                  hdc = GetDC (pparams->hwnd) ;
        
        
        
                  Ellipse (hdc, (pparams->cxClient - iDiameter) / 2,
        
                                  (pparams->cyClient - iDiameter) / 2,
        
                                  (pparams->cxClient + iDiameter) / 2,
        
                             (pparams->cyClient + iDiameter) / 2) ;
        
        
        
                  ReleaseDC (pparams->hwnd, hdc) ;
        
           }
        
    _endthread () ;
        
}
        

LRESULT APIENTRY WndProc4 (HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
        
{
        
           static PARAMS params ;
        
           switch (message)
        
           {
        
           case   WM_CREATE:
        
                          params.hwnd = hwnd ;
        
                          params.cyChar = HIWORD (GetDialogBaseUnits ()) ;
        
                          _beginthread (Thread4, 0, 秏s) ;
        
                          return 0 ;
        
        
        
           case   WM_SIZE:
        
                          params.cxClient = LOWORD (lParam) ;
        
                          params.cyClient = HIWORD (lParam) ;
        
                         return 0 ;
        
        
        
           case   WM_DESTROY:
        
                          params.bKill = TRUE ;
        
                          return 0 ;
        
           }
        
           return DefWindowProc (hwnd, message, wParam, lParam) ;
        
}
        

// --------------------------------------------------------------------------
        
// Main window to create child windows
        
// --------------------------------------------------------------------------
        

LRESULT APIENTRY WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
        
{
        
           static HWND           hwndChild[4] ;
        
           static TCHAR * szChildClass[] = { TEXT ("Child1"), TEXT ("Child2"),
        
       TEXT ("Child3"), TEXT ("Child4") } ;
        
           static WNDPROC ChildProc[] = { WndProc1, WndProc2, WndProc3, WndProc4 } ;
        
           HINSTANCE             hInstance ;
        
           int                                  i, cxClient, cyClient ;
        
           WNDCLASS                      wndclass ;
        
   
        
           switch (message)
        
           {
        
           case   WM_CREATE:
        
                          hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;
        
                          wndclass.style                                              = CS_HREDRAW | CS_VREDRAW ;
        
                          wndclass.cbClsExtra                          = 0 ;
        
                          wndclass.cbWndExtra                          = 0 ;
        
                          wndclass.hInstance                           = hInstance ;
        
                          wndclass.hIcon                               = NULL ;
        
                          wndclass.hCursor                            = LoadCursor (NULL, IDC_ARROW) ;
        
                          wndclass.hbrBackground               = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
        
                          wndclass.lpszMenuName  = NULL ;
        
        
        
                          for (i = 0 ; i < 4 ; i++)
        
                          {
        
                                         wndclass.lpfnWndProc          = ChildProc[i] ;
        
                                         wndclass.lpszClassName        = szChildClass[i] ;
        
             
        
                                         RegisterClass (&wndclass) ;
        
             
        
                                         hwndChild[i] = CreateWindow (szChildClass[i], NULL,
        
                           WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,
        
                          0, 0, 0, 0,
        
                           hwnd, (HMENU) i, hInstance, NULL) ;
        
                          }
        
        
        
                          return 0 ;
        
        
        
           case   WM_SIZE:
        
                          cxClient = LOWORD (lParam) ;
        
                          cyClient = HIWORD (lParam) ;
        
        
        
                          for (i = 0 ; i < 4 ; i++)
        
                                                 MoveWindow (hwndChild[i], (i % 2) * cxClient / 2,
        
                                                                (i > 1) * cyClient / 2,
        
                                             cxClient / 2, cyClient / 2, TRUE) ;
        
                          return 0 ;
        
        
        
          case   WM_CHAR:
        
                          if (wParam == '\x1B')
        
                                                 DestroyWindow (hwnd) ;
        
        
        
                          return 0 ;
        
       
        
           case   WM_DESTROY:
        
                          PostQuitMessage (0) ;
        
                          return 0 ;
        
           }
        
           return DefWindowProc (hwnd, message, wParam, lParam) ;
        
}
        
MULTI2.C的WinMain和WndProc函数非常类似于MULTI1.C中的同名函数。WndProc为四个窗口注册了四种窗口类别,建立了这些窗口,并在WM_SIZE消息处理期间缩放这些窗口。WndProc的唯一不同是它不再设定Windows定时器,也不再处理WM_TIMER消息。

MULTI2中较大的改变是每个子窗口消息处理程序透过在WM_CREATE消息处理期间呼叫_beginthread函数来建立另一个线程。总括来说,MULTI2程序有五个同时执行的执行绪,主执行绪包含主窗口消息处理程序和四个子窗口消息处理程序,其余的

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -