zoukankan      html  css  js  c++  java
  • win32里玩事件转

    #include "stdafx.h"
    #include <stdio.h>
    #include <windows.h>
    #include <iostream>
    #include <process.h>
    
    #define UM_MSG1 WM_USER+1
    #define UM_MSG2 WM_USER+2
    
    DWORD WINAPI Thread1(LPVOID para)
    {
    	DWORD dwThreadId = *(DWORD *)para;
    	DWORD i=0;
    	TCHAR *p;
    	char strTmp[100];
    	while(TRUE)
    	{
    		Sleep(1700);        
    		p=new TCHAR[10];
    		sprintf(strTmp,"Hello %d %x",i++,p);
    		PostThreadMessage(dwThreadId,UM_MSG1,(WPARAM)strTmp,(LPARAM)p);
    		//delete []p;
    	}
    	return 0;
    }
    
    DWORD WINAPI Thread2(LPVOID para)
    {
    	char strTmp[100];
    	DWORD dwThreadId = *(DWORD *)para;
    	DWORD i=0;
    	TCHAR *p;
    	while(TRUE)
    	{
    
    		Sleep(3000);
    		p=new TCHAR[10];
    		sprintf(strTmp,"World %d %x",i++,p);
    		PostThreadMessage(dwThreadId,UM_MSG2,(WPARAM)strTmp,(LPARAM)p);
    		delete []p;
    	}
    	return 0;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	DWORD dwValue =GetCurrentThreadId();
    	HANDLE hThread1 = CreateThread(NULL,0,&Thread1,&dwValue,0,NULL);
    	HANDLE hThread2 = CreateThread(NULL,0,&Thread2,&dwValue,0,NULL);
    	CloseHandle(hThread1);
    	CloseHandle(hThread2);
    	MSG msg;
    	while(GetMessage(&msg,NULL,0,0))
    	{
    		switch(msg.message)
    		{
    		case UM_MSG1:
    		case UM_MSG2:
    			printf("msg:0x%x      w:%x   ws:%s     l:%x\n",msg.message,&msg.wParam,msg.wParam,msg.lParam);
    			delete [](TCHAR *)msg.lParam;  //注释掉这句你就会看到堆内存地址变化
    			break;
    		default:
    			printf("Unknown msg:0x%x\n",msg.message);
    			break;
    		}
    		Sleep(1);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    阅读笔记06
    阅读笔记05
    学习进度03
    四则运算03
    阅读笔记04
    求最大子数组值(有环版)
    合作项目02
    新的小组信息以及项目名称与介绍
    第六周进度条
    软件工程个人作业4(课堂练习&&课堂作业)
  • 原文地址:https://www.cnblogs.com/chuncn/p/2436614.html
Copyright © 2011-2022 走看看