tlsdyn.c

来自「一本已经绝版的好书」· C语言 代码 · 共 123 行

C
123
字号
/************************************************************
Module name: TLSDyn.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <stdio.h>      // For sprintf
#include <process.h>    // For _beginthreadex
#include "..\SomeLib\SomeLib.H"
#include "Resource.H"


/////////////////////////////////////////////////////////////


HWND g_hwndLog = NULL;


/////////////////////////////////////////////////////////////


DWORD WINAPI ThreadFunc (LPVOID lpvThreadParm) {
   int nThreadNum = (int) lpvThreadParm;
   int nNumCycles = 4;
   TCHAR szBuf[100];
   LPCTSTR szString = LoadResString();

   while (nNumCycles--) {
      _stprintf(szBuf, __TEXT("Thread #%d: %s"),
         nThreadNum, szString);

      ListBox_AddString(g_hwndLog, szBuf);

      Sleep(nThreadNum * 50);
   }
   return(0);
}


/////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus,
   LPARAM lParam) {

   int nThreadNum;

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_TLSDYN, IDI_TLSDYN);

   g_hwndLog = GetDlgItem(hwnd, IDC_LOG);


   for (nThreadNum = 1; nThreadNum <= 5; nThreadNum++) {
      HANDLE hThread;
      DWORD dwIDThread;

      hThread = chBEGINTHREADEX(NULL, 0, ThreadFunc,
         (LPVOID) nThreadNum, 0, &dwIDThread);

      CloseHandle(hThread);
   }

   return(TRUE);
}


/////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl,
   UINT codeNotify) {

   switch (id) {
      case IDCANCEL:
         EndDialog(hwnd, id);
         break;
   }
}


/////////////////////////////////////////////////////////////


BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg,
   WPARAM wParam, LPARAM lParam) {

   switch (uMsg) {

      chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
      chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
   }
   return(FALSE);
}


/////////////////////////////////////////////////////////////


int WINAPI _tWinMain (HINSTANCE hinstExe,
   HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {

   chWARNIFUNICODEUNDERWIN95();

   MessageBox(NULL, LoadResString(),
      __TEXT("String Before Dialog Box"), MB_OK);

   DialogBox(hinstExe, MAKEINTRESOURCE(IDD_TLSDYN),
      NULL, Dlg_Proc);

   MessageBox(NULL, LoadResString(),
      __TEXT("String After Dialog Box"), MB_OK);

   return(0);
}


//////////////////////// End Of File ////////////////////////

⌨️ 快捷键说明

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