PostMessage,SendMessage,SendNotifyMessage这三个函数是由windows操作系统提供的用来在窗口之间传递消息的API函数,三个函数的功能描述如下。
PostMessage函数功能:该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里,不等待线程处理消息就返回,是异步消息模式。消息队列里的消息通过调用GetMessage和PeekMessage取得。
SendMessage函数功能:该函数将指定的消息发送到一个或多个窗口,为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。而函数PostMessage不同,将一个消息寄送到一个线程的消息队列后立即返回。
SendNotifyMessage函数功能:该函数将指定的消息发送到一个窗口。如果该窗口是由调用线程创建的;此函数为该窗口调用窗口程序,并等待窗口程序处理完消息后再返回。如果该窗口是由不同的线程创建的,此函数将消息传给该窗口程序,并立即返回,不等待窗口程序处理完消息。
下面一段代码是为了测试这三个函数的功能而写的。
 #include <Windows.h>
#include <Windows.h>
 #include <process.h>
#include <process.h>
 #include <stdio.h>
#include <stdio.h>

 #define  WM_USER  0x0400
#define  WM_USER  0x0400    
 #define  TEST_MSG  (WM_USER + 1)
#define  TEST_MSG  (WM_USER + 1)

 HWND htest1;
HWND htest1;
 HWND htest2;
HWND htest2;


 HWND utCreateMsgWindow(char szName[], WNDPROC lpfnWndProc)
HWND utCreateMsgWindow(char szName[], WNDPROC lpfnWndProc)


 {
{
 WNDCLASS wcl;
    WNDCLASS wcl;
 HWND hWnd = NULL;
    HWND hWnd = NULL;
 
    
 memset(&wcl,0,sizeof(wcl));
    memset(&wcl,0,sizeof(wcl));
 
    
 if (lpfnWndProc == NULL)
    if (lpfnWndProc == NULL)

 
     {
{

 return  NULL; /**//*失败*/
        return  NULL; /**//*失败*/
 }
    }
 
    

 /**//*注册窗口类*/
    /**//*注册窗口类*/ 
 
    
 wcl.cbClsExtra = 0;
    wcl.cbClsExtra = 0;
 wcl.cbWndExtra = 0;
    wcl.cbWndExtra = 0;
 wcl.hbrBackground = NULL;
    wcl.hbrBackground = NULL;
 wcl.hCursor = NULL;
    wcl.hCursor = NULL;
 wcl.hIcon = NULL;
    wcl.hIcon = NULL;
 wcl.hInstance = NULL;
    wcl.hInstance = NULL;
 wcl.lpfnWndProc = lpfnWndProc;
    wcl.lpfnWndProc = lpfnWndProc;
 wcl.lpszClassName = szName;
    wcl.lpszClassName = szName;
 wcl.lpszMenuName = NULL;
    wcl.lpszMenuName = NULL;
 wcl.style = CS_VREDRAW;
    wcl.style = CS_VREDRAW;
 if (RegisterClass(&wcl) == 0)
    if (RegisterClass(&wcl) == 0)

 
     {
{
 return NULL;
        return NULL;
 }
    }
 
    

 /**//*创建隐含窗口*/
    /**//*创建隐含窗口*/
 hWnd = CreateWindow(szName,NULL,WS_POPUP,0,0,
    hWnd = CreateWindow(szName,NULL,WS_POPUP,0,0,
 CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,
 NULL,NULL,NULL,NULL);
        NULL,NULL,NULL,NULL);
 return  hWnd;
    return  hWnd;
 }
}

 LRESULT CALLBACK test1WndProc(IN HWND hWnd,
LRESULT CALLBACK test1WndProc(IN HWND hWnd,
 IN unsigned int uMsg,
                              IN unsigned int uMsg,  
 IN WPARAM  wParam,
                              IN WPARAM  wParam,
 IN LPARAM lParam)
                              IN LPARAM lParam)


 {
{
 switch(uMsg)
    switch(uMsg)

 
     {
{
 case TEST_MSG:
    case TEST_MSG:
 
        
 break;
        break;
 default:
    default: 
 return DefWindowProc(hWnd,uMsg,wParam,lParam);
        return DefWindowProc(hWnd,uMsg,wParam,lParam);
 }
    }
 return 0;
    return 0;
 }
}

 LRESULT CALLBACK test2WndProc(IN HWND hWnd,
LRESULT CALLBACK test2WndProc(IN HWND hWnd,
 IN unsigned int uMsg,
                              IN unsigned int uMsg,  
 IN WPARAM  wParam,
                              IN WPARAM  wParam,
 IN LPARAM lParam)
                              IN LPARAM lParam)


 {
{
 switch(uMsg)
    switch(uMsg)

 
     {
{
 case TEST_MSG:
    case TEST_MSG:
 printf("test2!\n");
        printf("test2!\n");
 Sleep(1000);
        Sleep(1000);
 break;
        break;
 default:
    default: 
 return DefWindowProc(hWnd,uMsg,wParam,lParam);
        return DefWindowProc(hWnd,uMsg,wParam,lParam);
 }
    }
 
    
 return 0;
    return 0;
 }
}


 void test1(void* parm)
void test1(void* parm)


 {
{
 BOOL bRet;
    BOOL bRet;
 MSG msg;
    MSG msg;


 htest1 = utCreateMsgWindow("testWin1",test1WndProc);
    htest1 = utCreateMsgWindow("testWin1",test1WndProc);
 if(NULL == htest1)
    if(NULL == htest1)

 
     {
{
 return;
        return;
 }
    }
 
    
 Sleep(1000);
    Sleep(1000);
 
    
 //SendMessage(htest2,TEST_MSG,0,0);
    //SendMessage(htest2,TEST_MSG,0,0);
 //PostMessage(htest2,TEST_MSG,0,0);
    //PostMessage(htest2,TEST_MSG,0,0);
 SendNotifyMessage(htest2,TEST_MSG,0,0);
    SendNotifyMessage(htest2,TEST_MSG,0,0);


 printf("this is test1\n");
    printf("this is test1\n");
 
    
 while( (bRet = GetMessage( &msg, NULL, 0 , 0 )) != 0)
    while( (bRet = GetMessage( &msg, NULL, 0 , 0 )) != 0)

 
     {
{ 
 if (bRet == -1)
        if (bRet == -1)

 
         {
{
 // handle the error and possibly exit
            // handle the error and possibly exit
 break;
            break;
 }
        }
 else
        else

 
         {
{
 TranslateMessage(&msg);
            TranslateMessage(&msg); 
 DispatchMessage(&msg);
            DispatchMessage(&msg); 
 }
        }
 }
    }
 _endthread();
    _endthread(); 
 }
}
 void test2(void* parm)
void test2(void* parm)


 {
{
 BOOL bRet;
    BOOL bRet;
 MSG msg;
    MSG msg;

 
    

 htest2 = utCreateMsgWindow("testWin2",test2WndProc);
    htest2 = utCreateMsgWindow("testWin2",test2WndProc);
 if(NULL == htest2)
    if(NULL == htest2)

 
     {
{
 return;
        return;
 }
    }
 
    
 while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
    while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)

 
     {
{ 
 if (bRet == -1)
        if (bRet == -1)

 
         {
{
 // handle the error and possibly exit
            // handle the error and possibly exit
 break;
            break;
 }
        }
 else
        else

 
         {
{
 TranslateMessage(&msg);
            TranslateMessage(&msg); 
 DispatchMessage(&msg);
            DispatchMessage(&msg); 
 }
        }
 }
    }
 _endthread();
    _endthread(); 
 }
}



 int main()
int main()


 {
{
 _beginthread(test1,0,NULL);
    _beginthread(test1,0,NULL);
 _beginthread(test2,0,NULL);
    _beginthread(test2,0,NULL);
 
    
 Sleep(10000);
    Sleep(10000);
 
    
 return 0;
    return 0;
 }
}
