关于PostMessage,SendMessage,SendNotifyMessage三个函数的探讨

PostMessage,SendMessage,SendNotifyMessage这三个函数是由windows操作系统提供的用来在窗口之间传递消息的API函数,三个函数的功能描述如下。

PostMessage函数功能:该函数将一个消息放入(寄送)到与指定窗口创建的线程相联系消息队列里,不等待线程处理消息就返回,是异步消息模式。消息队列里的消息通过调用GetMessage和PeekMessage取得。

SendMessage函数功能:该函数将指定的消息发送到一个或多个窗口,为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回。而函数PostMessage不同,将一个消息寄送到一个线程的消息队列后立即返回。

SendNotifyMessage函数功能:该函数将指定的消息发送到一个窗口。如果该窗口是由调用线程创建的;此函数为该窗口调用窗口程序,并等待窗口程序处理完消息后再返回。如果该窗口是由不同的线程创建的,此函数将消息传给该窗口程序,并立即返回,不等待窗口程序处理完消息。

下面一段代码是为了测试这三个函数的功能而写的。

#include <Windows.h>
#include 
<process.h>
#include 
<stdio.h>

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

HWND htest1;
HWND htest2;


HWND utCreateMsgWindow(
char szName[], WNDPROC lpfnWndProc)
{
    WNDCLASS wcl;
    HWND hWnd 
= NULL;
    
    memset(
&wcl,0,sizeof(wcl));
    
    
if (lpfnWndProc == NULL)
    
{
        
return  NULL; /*失败*/
    }

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

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


LRESULT CALLBACK test1WndProc(IN HWND hWnd,
                              IN unsigned 
int uMsg,  
                              IN WPARAM  wParam,
                              IN LPARAM lParam)
{
    
switch(uMsg)
    
{
    
case TEST_MSG:
        
        
break;
    
default
        
return DefWindowProc(hWnd,uMsg,wParam,lParam);
    }

    
return 0;
}


LRESULT CALLBACK test2WndProc(IN HWND hWnd,
                              IN unsigned 
int uMsg,  
                              IN WPARAM  wParam,
                              IN LPARAM lParam)
{
    
switch(uMsg)
    
{
    
case TEST_MSG:
        printf(
"test2!\n");
        Sleep(
1000);
        
break;
    
default
        
return DefWindowProc(hWnd,uMsg,wParam,lParam);
    }

    
    
return 0;
}



void test1(void* parm)
{
    BOOL bRet;
    MSG msg;


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

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


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

        
if (bRet == -1)
        
{
            
// handle the error and possibly exit
            break;
        }

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

    }

    _endthread(); 
}

void test2(void* parm)
{
    BOOL bRet;
    MSG msg;

    

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

    
    
while( (bRet = GetMessage( &msg, NULL, 00 )) != 0)
    

        
if (bRet == -1)
        
{
            
// handle the error and possibly exit
            break;
        }

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

    }

    _endthread(); 
}




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

posted on 2010-08-06 10:13 小妹阿青 阅读(3712) 评论(0)  编辑 收藏 引用 所属分类: Visual C++/MFC


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿

随笔分类

随笔档案

IT技术

趣闻轶事

搜索

最新评论

阅读排行榜

评论排行榜