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

本文通过示例代码介绍了如何设置不同线程的优先级,并演示了C++中运行时类型识别(RTTI)的应用。首先,创建了五个具有不同优先级的线程并展示了如何控制它们的执行;接着,通过RTTI实现了多态对象类型的检查与转换。
None.gif#define HIGHEST_THREAD    0x00
None.gif
#define ABOVE_AVE_THREAD  0x3F
None.gif
#define NORMAL_THREAD     0x7F
None.gif
#define BELOW_AVE_THREAD  0xBF
None.gif
#define LOWEST_THREAD     0xFF
None.gif
#define SLEEPDELAY        1
None.gif
#define FORLOOPDELAY      2
None.gif
#define NODELAY           3
None.gif
None.gifHWND hWnd;
None.gifHANDLE _hThread[
5];
None.gifUINT   _uDelayType
=NODELAY;//类型
None.gif

None.gifLRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
int wmId, wmEvent;
InBlock.gif    PAINTSTRUCT ps;
InBlock.gif    HDC hdc;
InBlock.gif    
int    i;
InBlock.gif    DWORD  ThreadID[
5];
InBlock.gif    
static HMENU  hMenu;
InBlock.gif    
static HANDLE hMasterThread;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
static DWORD  ThreadArg[5= dot.gif{HIGHEST_THREAD,    // 0x00
InBlock.gif
                                  ABOVE_AVE_THREAD,  // 0x3F
InBlock.gif
                                  NORMAL_THREAD,     // 0x7F
InBlock.gif
                                  BELOW_AVE_THREAD,  // 0xBF
InBlock.gif
                                  LOWEST_THREAD      // 0xFF
ExpandedSubBlockEnd.gif
                                 }
;
InBlock.gif    
switch (message)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
case WM_CREATE:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            hMenu 
= GetMenu (hWnd);
InBlock.gif          hMasterThread 
= GetCurrentThread();
InBlock.gif          SetThreadPriority(hMasterThread, THREAD_PRIORITY_HIGHEST);
InBlock.gif          
for(i=0; i<5; i++)  
InBlock.gif              _hThread[i] 
= CreateThread(NULL,
InBlock.gif                                        
0,
InBlock.gif                                        (LPTHREAD_START_ROUTINE)ThreadProc,
InBlock.gif                                        
&ThreadArg[i],
InBlock.gif                                        CREATE_SUSPENDED,
InBlock.gif                                        
&ThreadID[i]);
InBlock.gif          SetThreadPriority(_hThread[
0], THREAD_PRIORITY_HIGHEST);
InBlock.gif          SetThreadPriority(_hThread[
1], THREAD_PRIORITY_ABOVE_NORMAL);
InBlock.gif          SetThreadPriority(_hThread[
2], THREAD_PRIORITY_NORMAL);
InBlock.gif          SetThreadPriority(_hThread[
3], THREAD_PRIORITY_BELOW_NORMAL);
InBlock.gif          SetThreadPriority(_hThread[
4], THREAD_PRIORITY_LOWEST);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
break;
InBlock.gif    
case WM_COMMAND:
InBlock.gif        wmId    
= LOWORD(wParam);
InBlock.gif        wmEvent 
= HIWORD(wParam);
InBlock.gif        
// Parse the menu selections:
InBlock.gif
        switch (wmId)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
case IDM_ABOUT:
InBlock.gif            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
InBlock.gif            
break;
InBlock.gif        
case IDM_EXIT:
InBlock.gif            DestroyWindow(hWnd);
InBlock.gif            
break;
InBlock.gif        
case IDM_RESUME:      
InBlock.gif            EnableMenuItem(hMenu, IDM_RESUME, MF_BYCOMMAND 
| MF_GRAYED);
InBlock.gif            EnableMenuItem(hMenu, IDM_SUSPEND, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            DrawMenuBar(hWnd);
InBlock.gif            
for (i=0; i<5; i++)
InBlock.gif                ResumeThread(_hThread[i]);   
InBlock.gif            
return (0);
InBlock.gif         
case IDM_SUSPEND:  
InBlock.gif            
for (i=0; i<5; i++)
InBlock.gif                SuspendThread(_hThread[i]);  
InBlock.gif            EnableMenuItem(hMenu, IDM_SUSPEND, MF_BYCOMMAND 
| MF_GRAYED);
InBlock.gif            EnableMenuItem(hMenu, IDM_RESUME, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            DrawMenuBar(hWnd);
InBlock.gif            
return (0);
InBlock.gif         
case IDM_FORLOOP:    
InBlock.gif            _uDelayType 
= FORLOOPDELAY;
InBlock.gif            EnableMenuItem(hMenu, IDM_FORLOOP, MF_BYCOMMAND 
| MF_GRAYED);
InBlock.gif            EnableMenuItem(hMenu, IDM_SLEEP, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            EnableMenuItem(hMenu, IDM_NODELAY, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            DrawMenuBar(hWnd);
InBlock.gif            
return (0);
InBlock.gif         
case IDM_SLEEP: 
InBlock.gif            _uDelayType 
= SLEEPDELAY;
InBlock.gif            EnableMenuItem(hMenu, IDM_SLEEP, MF_BYCOMMAND 
| MF_GRAYED);
InBlock.gif            EnableMenuItem(hMenu, IDM_FORLOOP, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            EnableMenuItem(hMenu, IDM_NODELAY, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            DrawMenuBar(hWnd);
InBlock.gif            
return (0);
InBlock.gif     
case IDM_NODELAY: 
InBlock.gif            _uDelayType 
= NODELAY;
InBlock.gif            EnableMenuItem(hMenu, IDM_NODELAY, MF_BYCOMMAND 
| MF_GRAYED);
InBlock.gif            EnableMenuItem(hMenu, IDM_FORLOOP, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            EnableMenuItem(hMenu, IDM_SLEEP, MF_BYCOMMAND 
| MF_ENABLED);
InBlock.gif            DrawMenuBar(hWnd);
InBlock.gif            
return (0);
InBlock.gif        
default:
InBlock.gif            
return DefWindowProc(hWnd, message, wParam, lParam);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
break;
InBlock.gif    
case WM_PAINT:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            hdc 
= BeginPaint(hWnd, &ps);
InBlock.gif            EndPaint(hWnd, 
&ps);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
break;
InBlock.gif    
case WM_DESTROY:
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             
for(i=0; i<5; i++)
InBlock.gif                 TerminateThread(_hThread[i], 
0); 
InBlock.gif            PostQuitMessage(
0);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
break;
InBlock.gif    
default:
InBlock.gif        
return DefWindowProc(hWnd, message, wParam, lParam);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
None.gifVOID ThreadProc(DWORD 
*ThreadArg)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifRECT rect;
InBlock.gifHDC  hDC;
InBlock.gifHANDLE hBrush, hOldBrush;
InBlock.gifDWORD dwThreadHits 
= 0;
InBlock.gif
char  cBuf[80];
InBlock.gif
int   iThreadNo, i;
InBlock.gif   GetClientRect (hWnd, 
&rect);
InBlock.gif   hDC 
= GetDC (hWnd);
InBlock.gif   hBrush 
= CreateSolidBrush(RGB(*(ThreadArg), *(ThreadArg), *(ThreadArg)));  // 跑て礶肅︹
InBlock.gif
   hOldBrush = SelectObject(hDC, hBrush);
ExpandedSubBlockStart.gifContractedSubBlock.gif   
switch (*ThreadArg) dot.gif{
InBlock.gif     
case HIGHEST_THREAD   : iThreadNo = 0break;
InBlock.gif     
case ABOVE_AVE_THREAD : iThreadNo = 1break;
InBlock.gif     
case NORMAL_THREAD    : iThreadNo = 2break;
InBlock.gif     
case BELOW_AVE_THREAD : iThreadNo = 3break;
InBlock.gif     
case LOWEST_THREAD    : iThreadNo = 4break;
ExpandedSubBlockEnd.gif   }

InBlock.gif   sprintf(cBuf, 
"T%d", iThreadNo);
InBlock.gif   TextOut(hDC, 
*(ThreadArg), rect.bottom-150, (LPCWSTR)(LPCSTR)cBuf, strlen(cBuf));
InBlock.gif   sprintf(cBuf, 
"P=%d", GetThreadPriority(_hThread[iThreadNo]));
InBlock.gif   TextOut(hDC, 
*(ThreadArg), rect.bottom-130, (LPCWSTR)(LPCSTR)cBuf, strlen(cBuf));
InBlock.gif   
do
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     dwThreadHits
++;      
InBlock.gif     Rectangle(hDC, 
*(ThreadArg), rect.bottom-(dwThreadHits/10),
InBlock.gif               
*(ThreadArg)+0x40, rect.bottom);
InBlock.gif  
if (_uDelayType == SLEEPDELAY)
InBlock.gif         Sleep(
10);
InBlock.gif     
else if (_uDelayType == FORLOOPDELAY)
InBlock.gif         
for (i=0; i<30000; i++);
InBlock.gif     
else // _uDelayType == NODELAY)
ExpandedSubBlockStart.gifContractedSubBlock.gif
         dot.gif{   }
ExpandedSubBlockEnd.gif   }
 while (dwThreadHits < 1000);     
InBlock.gif   hBrush 
= SelectObject(hDC, hOldBrush);   
InBlock.gif   DeleteObject (hBrush);
InBlock.gif   ReleaseDC (hWnd, hDC);
ExpandedBlockEnd.gif}

None.gif

2C++中的RTTI

None.gif#include <typeinfo.h>
None.gif#include 
<iostream>
None.gif
using namespace std;
None.gif#include 
<string.h>
None.gif
None.gif
class graphicImage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
protected:
InBlock.gif    
char name[80];
InBlock.gif
public:
InBlock.gif    graphicImage()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        strcpy(name,
"graphicImage");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
virtual void display()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< "Display a generic image." << endl;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
char* getName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return name;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
//----------------------------------------------------------------
None.gif
class GIFimage : public graphicImage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    GIFimage()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        strcpy(name,
"GIFimage");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
void display()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< "Display a GIF file." << endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
class PICTimage : public graphicImage
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    PICTimage()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        strcpy(name,
"PICTimage");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
void display()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        cout 
<< "Display a PICT file." << endl;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
//----------------------------------------------------------------
None.gif
void processFile(graphicImage *type)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
if (typeid(GIFimage) == typeid(*type))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ((GIFimage 
*)type)->display();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else if (typeid(PICTimage) == typeid(*type))
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ((PICTimage 
*)type)->display();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
InBlock.gif        cout 
<< "Unknown type! " << (typeid(*type)).name() << endl;
ExpandedBlockEnd.gif}

None.gif
void main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    graphicImage 
*gImage = new GIFimage();
InBlock.gif    graphicImage 
*pImage = new PICTimage();
InBlock.gif    processFile(gImage);
InBlock.gif    processFile(pImage);
ExpandedBlockEnd.gif}

None.gif

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值