《深入浅出MFC》笔记(四)

#define HIGHEST_THREAD    0x00
#define ABOVE_AVE_THREAD  0x3F
#define NORMAL_THREAD     0x7F
#define BELOW_AVE_THREAD  0xBF
#define LOWEST_THREAD     0xFF
#define SLEEPDELAY        1
#define FORLOOPDELAY      2
#define NODELAY           3

HWND hWnd;
HANDLE _hThread[5];
UINT   _uDelayType=NODELAY;//类型

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    int    i;
    DWORD  ThreadID[5];
    static HMENU  hMenu;
    static HANDLE hMasterThread;
    static DWORD  ThreadArg[5] = {HIGHEST_THREAD,    // 0x00
                                  ABOVE_AVE_THREAD,  // 0x3F
                                  NORMAL_THREAD,     // 0x7F
                                  BELOW_AVE_THREAD,  // 0xBF
                                  LOWEST_THREAD      // 0xFF
                                 };
    switch (message)
    {
    case WM_CREATE:
        {
            hMenu = GetMenu (hWnd);
          hMasterThread = GetCurrentThread();
          SetThreadPriority(hMasterThread, THREAD_PRIORITY_HIGHEST);
          for(i=0; i<5; i++)  
              _hThread[i] = CreateThread(NULL,
                                        0,
                                        (LPTHREAD_START_ROUTINE)ThreadProc,
                                        &ThreadArg[i],
                                        CREATE_SUSPENDED,
                                        &ThreadID[i]);
          SetThreadPriority(_hThread[0], THREAD_PRIORITY_HIGHEST);
          SetThreadPriority(_hThread[1], THREAD_PRIORITY_ABOVE_NORMAL);
          SetThreadPriority(_hThread[2], THREAD_PRIORITY_NORMAL);
          SetThreadPriority(_hThread[3], THREAD_PRIORITY_BELOW_NORMAL);
          SetThreadPriority(_hThread[4], THREAD_PRIORITY_LOWEST);
        }
        break;
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        case IDM_RESUME:      
            EnableMenuItem(hMenu, IDM_RESUME, MF_BYCOMMAND | MF_GRAYED);
            EnableMenuItem(hMenu, IDM_SUSPEND, MF_BYCOMMAND | MF_ENABLED);
            DrawMenuBar(hWnd);
            for (i=0; i<5; i++)
                ResumeThread(_hThread[i]);   
            return (0);
         case IDM_SUSPEND:  
            for (i=0; i<5; i++)
                SuspendThread(_hThread[i]);  
            EnableMenuItem(hMenu, IDM_SUSPEND, MF_BYCOMMAND | MF_GRAYED);
            EnableMenuItem(hMenu, IDM_RESUME, MF_BYCOMMAND | MF_ENABLED);
            DrawMenuBar(hWnd);
            return (0);
         case IDM_FORLOOP:    
            _uDelayType = FORLOOPDELAY;
            EnableMenuItem(hMenu, IDM_FORLOOP, MF_BYCOMMAND | MF_GRAYED);
            EnableMenuItem(hMenu, IDM_SLEEP, MF_BYCOMMAND | MF_ENABLED);
            EnableMenuItem(hMenu, IDM_NODELAY, MF_BYCOMMAND | MF_ENABLED);
            DrawMenuBar(hWnd);
            return (0);
         case IDM_SLEEP: 
            _uDelayType = SLEEPDELAY;
            EnableMenuItem(hMenu, IDM_SLEEP, MF_BYCOMMAND | MF_GRAYED);
            EnableMenuItem(hMenu, IDM_FORLOOP, MF_BYCOMMAND | MF_ENABLED);
            EnableMenuItem(hMenu, IDM_NODELAY, MF_BYCOMMAND | MF_ENABLED);
            DrawMenuBar(hWnd);
            return (0);
     case IDM_NODELAY: 
            _uDelayType = NODELAY;
            EnableMenuItem(hMenu, IDM_NODELAY, MF_BYCOMMAND | MF_GRAYED);
            EnableMenuItem(hMenu, IDM_FORLOOP, MF_BYCOMMAND | MF_ENABLED);
            EnableMenuItem(hMenu, IDM_SLEEP, MF_BYCOMMAND | MF_ENABLED);
            DrawMenuBar(hWnd);
            return (0);
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        {
            hdc = BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        {
             for(i=0; i<5; i++)
                 TerminateThread(_hThread[i], 0); 
            PostQuitMessage(0);
        }
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

VOID ThreadProc(DWORD *ThreadArg)
{
RECT rect;
HDC  hDC;
HANDLE hBrush, hOldBrush;
DWORD dwThreadHits = 0;
char  cBuf[80];
int   iThreadNo, i;
   GetClientRect (hWnd, &rect);
   hDC = GetDC (hWnd);
   hBrush = CreateSolidBrush(RGB(*(ThreadArg), *(ThreadArg), *(ThreadArg)));  // 跑て礶肅︹
   hOldBrush = SelectObject(hDC, hBrush);
   switch (*ThreadArg) {
     case HIGHEST_THREAD   : iThreadNo = 0; break;
     case ABOVE_AVE_THREAD : iThreadNo = 1; break;
     case NORMAL_THREAD    : iThreadNo = 2; break;
     case BELOW_AVE_THREAD : iThreadNo = 3; break;
     case LOWEST_THREAD    : iThreadNo = 4; break;
   }
   sprintf(cBuf, "T%d", iThreadNo);
   TextOut(hDC, *(ThreadArg), rect.bottom-150, (LPCWSTR)(LPCSTR)cBuf, strlen(cBuf));
   sprintf(cBuf, "P=%d", GetThreadPriority(_hThread[iThreadNo]));
   TextOut(hDC, *(ThreadArg), rect.bottom-130, (LPCWSTR)(LPCSTR)cBuf, strlen(cBuf));
   do
   {
     dwThreadHits++;      
     Rectangle(hDC, *(ThreadArg), rect.bottom-(dwThreadHits/10),
               *(ThreadArg)+0x40, rect.bottom);
  if (_uDelayType == SLEEPDELAY)
         Sleep(10);
     else if (_uDelayType == FORLOOPDELAY)
         for (i=0; i<30000; i++);
     else // _uDelayType == NODELAY)
         {   }
   } while (dwThreadHits < 1000);     
   hBrush = SelectObject(hDC, hOldBrush);   
   DeleteObject (hBrush);
   ReleaseDC (hWnd, hDC);
}

2.C++中的RTTI
#include <typeinfo.h>
#include <iostream>
using namespace std;
#include <string.h>

class graphicImage
{
protected:
    char name[80];
public:
    graphicImage()
    {
        strcpy(name,"graphicImage");
    }
    virtual void display()
    {
        cout << "Display a generic image." << endl;
    }
    char* getName()
    {
        return name;
    }
};
//----------------------------------------------------------------
class GIFimage : public graphicImage
{
public:
    GIFimage()
    {
        strcpy(name,"GIFimage");
    }
    void display()
    {
        cout << "Display a GIF file." << endl;
    }
};
class PICTimage : public graphicImage
{
public:
    PICTimage()
    {
        strcpy(name,"PICTimage");
    }
    void display()
    {
        cout << "Display a PICT file." << endl;
    }
};
//----------------------------------------------------------------
void processFile(graphicImage *type)
{
    if (typeid(GIFimage) == typeid(*type))
    {
        ((GIFimage *)type)->display();
    }
    else if (typeid(PICTimage) == typeid(*type))
    {
        ((PICTimage *)type)->display();
    }
    else
        cout << "Unknown type! " << (typeid(*type)).name() << endl;
}
void main()
{
    graphicImage *gImage = new GIFimage();
    graphicImage *pImage = new PICTimage();
    processFile(gImage);
    processFile(pImage);
}



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/01/19/1045794.html,如需转载请自行联系原作者
上一篇:《深入浅出MFC》笔记(三)


下一篇:oracle database 18c rpm 安装