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

📄 kevlab.cpp

📁 wince开发平台下
💻 CPP
字号:
// KEVLab.cpp : Defines the entry point for the application.
//
// Sample Application created for the Windows Embedded Developers Conference, Feb 6-8 2001.
// Author mikehall
//
//
//

#include "stdafx.h"
#include "resource.h"
#include <stdio.h>

#include <celog.h>

#define MAX_LOADSTRING 100
#define DEMOINTERVAL 500


        // Definitions for our debug zones                               
        #define ZONEID_INIT      0		// Initialization of the application
        #define ZONEID_TRACE     1		// Tracing entry/exit points for routines.
        #define ZONEID_EXCEPT    2
        #define ZONEID_RSVD3     3
        #define ZONEID_RSVD4     4
        #define ZONEID_RSVD5     5
        #define ZONEID_RSVD6     6
        #define ZONEID_RSVD7     7
        #define ZONEID_RSVD8     8
        #define ZONEID_RSVD9     9
        #define ZONEID_RSVD10   10
        #define ZONEID_RSVD11   11
        #define ZONEID_RSVD12   12
        #define ZONEID_RSVD13   13        
        #define ZONEID_WARN     14
        #define ZONEID_ERROR    15

        // These masks are useful for initialization of dpCurSettings                                    
        #define ZONEMASK_INIT      (1<<ZONEID_INIT) 
        #define ZONEMASK_TRACE     (1<<ZONEID_TRACE) 
        #define ZONEMASK_EXCEPT    (1<<ZONEID_EXCEPT) 
        #define ZONEMASK_RSVD3     (1<<ZONEID_RSVD3) 
        #define ZONEMASK_RSVD4     (1<<ZONEID_RSVD4) 
        #define ZONEMASK_RSVD5     (1<<ZONEID_RSVD5) 
        #define ZONEMASK_RSVD6     (1<<ZONEID_RSVD6) 
        #define ZONEMASK_RSVD7     (1<<ZONEID_RSVD7) 
        #define ZONEMASK_RSVD8     (1<<ZONEID_RSVD8) 
        #define ZONEMASK_RSVD9     (1<<ZONEID_RSVD9) 
        #define ZONEMASK_RSVD10    (1<<ZONEID_RSVD10)
        #define ZONEMASK_RSVD11    (1<<ZONEID_RSVD11)
        #define ZONEMASK_RSVD12    (1<<ZONEID_RSVD12)
        #define ZONEMASK_RSVD13    (1<<ZONEID_RSVD13)        
        #define ZONEMASK_WARN      (1<<ZONEID_WARN )  
        #define ZONEMASK_ERROR     (1<<ZONEID_ERROR) 
 
#ifdef DEBUG
        // These macros are used as the first arg to DEBUGMSG
        #define ZONE_INIT       DEBUGZONE(ZONEID_INIT)
        #define ZONE_TRACE      DEBUGZONE(ZONEID_TRACE)
        #define ZONE_EXCEPT     DEBUGZONE(ZONEID_EXCEPT)
        #define ZONE_RSVD3      DEBUGZONE(ZONEID_RSVD3)
        #define ZONE_RSVD4      DEBUGZONE(ZONEID_RSVD4)
        #define ZONE_RSVD5      DEBUGZONE(ZONEID_RSVD5)
        #define ZONE_RSVD6      DEBUGZONE(ZONEID_RSVD6)
        #define ZONE_RSVD7      DEBUGZONE(ZONEID_RSVD7)
        #define ZONE_RSVD8      DEBUGZONE(ZONEID_RSVD8)
        #define ZONE_RSVD9      DEBUGZONE(ZONEID_RSVD9)
        #define ZONE_RSVD10     DEBUGZONE(ZONEID_RSVD10)
        #define ZONE_RSVD11     DEBUGZONE(ZONEID_RSVD11)
        #define ZONE_RSVD12     DEBUGZONE(ZONEID_RSVD12)
        #define ZONE_RSVD13     DEBUGZONE(ZONEID_RSVD13)
        #define ZONE_WARN       DEBUGZONE(ZONEID_WARN )
        #define ZONE_ERROR      DEBUGZONE(ZONEID_ERROR)
#endif

DBGPARAM dpCurSettings = {
    TEXT("KevLab"), {
        TEXT("Init"),TEXT("Trace"),TEXT("Not Defined"),TEXT("Not Defined"),
        TEXT("Not Defined"),TEXT("Not Defined"),TEXT("Not Defined"),TEXT("Not Defined"),
        TEXT("Not Defined"),TEXT("Not Defined"),TEXT("Not Defined"),TEXT("Not Defined"),
        TEXT("Not Defined"),TEXT("Not Defined"),TEXT("Not Defined"),TEXT("Not Defined")},
    // By default, turn on the zones for init and errors.
    ZONEMASK_INIT    
}; 


// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text

DWORD WINAPI DemoThread(LPVOID lpParameter);

void CreateDemoStuff( );

CRITICAL_SECTION GlobalCriticalSection;		// Critical Section, used in KEV Demo.
DWORD g_dwCSOwner;	// ID of thread that currently owns Critical Section.
HFONT g_hFont;
HWND g_hWnd;

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

void CreateDemoStuff( )
{
LOGFONT lf;

memset(&lf,0x00,sizeof(LOGFONT));
lf.lfHeight=24;
wcscpy(lf.lfFaceName,L"Arial");
g_hFont=CreateFontIndirect(&lf);

}


int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	DEBUGREGISTER(NULL);

	DEBUGMSG (ZONE_INIT, (TEXT("KevLab starting\n")));

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_KEVLAB, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_KEVLAB);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASS wc;

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC) WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = 0;
    wc.hCursor = 0;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = 0;
    wc.lpszClassName = szWindowClass;

	return RegisterClass(&wc);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
      0, 0, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	DWORD dwThreadID;
	int x;
	TCHAR szBuffer[50];

	switch (message) 
	{
		case WM_CREATE:
			g_hWnd=hWnd;
			CreateDemoStuff( );
			InitializeCriticalSection(&GlobalCriticalSection); 			
			for(x=0;x < 3;x++) {
				CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DemoThread,(LPVOID)(x+1),0,&dwThreadID);
			}
		break;

		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
		break;
/*
		case WM_PAINT:
			DEBUGMSG (ZONE_TRACE, (TEXT("Enter WM_PAINT handler\n")));
			hdc = BeginPaint(hWnd, &ps);
			SelectObject(ps.hdc,g_hFont);
			for (x=0;x < 3;x++) {
				wsprintf(szBuffer,L"CriticalSection - Thread %d",x+1);
				ExtTextOut(ps.hdc,30,x*30,ETO_OPAQUE,NULL,szBuffer,lstrlen(szBuffer),NULL);
			}

			DEBUGMSG (ZONE_TRACE, (TEXT("Display CriticalSection 'Owner'\n")));
			ExtTextOut(ps.hdc,0,(g_dwCSOwner-1)*30,ETO_OPAQUE,NULL,L">>",2,NULL);
			EndPaint(hWnd, &ps);
			DEBUGMSG (ZONE_TRACE, (TEXT("Leave WM_PAINT handler\n")));
			break;
*/
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}


DWORD WINAPI DemoThread(LPVOID lpParameter)
{
DWORD dwThreadID=(DWORD)lpParameter;
TCHAR szString[120];
SYSTEMTIME sTime;

DEBUGMSG (ZONE_INIT, (TEXT("Thread %d starting\n"),dwThreadID));

while(TRUE) {
	DEBUGMSG (ZONE_TRACE, (TEXT("Thread %d - EnterCriticalSection\n"),dwThreadID));
	EnterCriticalSection(&GlobalCriticalSection); 
	CELOGDATA(TRUE, CELID_RAW_LONG, &dwThreadID, (WORD) (sizeof(DWORD)), 1, CELZONE_MISC);

	DEBUGMSG (ZONE_TRACE, (TEXT("Thread %d - Owns CriticalSection\n"),dwThreadID));
	g_dwCSOwner=dwThreadID;
//	InvalidateRect(g_hWnd,NULL,TRUE);
	memset(&sTime,0x00,sizeof(sTime));
	GetLocalTime(&sTime);
	wsprintf(szString,L"Thread %d - %02d:%02d:%02d\n",dwThreadID,sTime.wHour,sTime.wMinute,sTime.wSecond);
	OutputDebugString(szString);
	Sleep(dwThreadID*DEMOINTERVAL);
	DEBUGMSG (ZONE_TRACE, (TEXT("Thread %d - LeaveCriticalSection\n"),dwThreadID));
	DEBUGMSG (ZONE_TRACE, (TEXT("--------------------------------\n")));
	LeaveCriticalSection(&GlobalCriticalSection);
}

return 0L;
}

⌨️ 快捷键说明

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