问个简单的问题,线程中发送消息,当前类不能接受,而主窗口能够接受,为什么呢??? 如果确认窗口被创建出来并且没有被销毁,这么写应该没问题的,下断点确认一下this->m_hWnd和线程中的P是一样的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 跟踪过的,m_hwnd是正确的,就是不知道那里的问题 1、如果线程建立了窗口,可以直接给窗口发消息2、如果线程建立的消息循环,可以用PostThreadMessage给线程发消息注意,PostMessage和SendMessage都是给有窗口的线程发消息的 TO:五岭散人1.已经建立了窗口:CMyClass::CMyClass(){ if(Create(NULL,"mywindow",WS_CHILD,CRect(0,0,0,0),AfxGetMainWnd(),1000) == 0) { AfxMessageBox("create error!"); return; }}2.消息循环是有的,(1)class CMyClass : public CWnd ,是从CWnd 继承的(2)直接在成员函数中使用sendmessage()是能收到消息的,就是在线程中发送的消息不能收到。 在哪调用了1。在OnLButtonDown中 PostMessage(my_hand,WM_MYMESS,NULL,NULL) =0,而GetLastError() = 0(ERROR_SUCCESS),RecvMessag并没有执行PostMessage返回BOOL,返回0表示没成功确认窗口已经创建好了。可以用IsWindow判断一下 TO:五岭散人1.窗口确认已经创建好了,在构造函数中:CMyClass::CMyClass(){ if(Create(NULL,"mywindow",WS_CHILD,CRect(0,0,0,0),AfxGetMainWnd(),1000) == 0) { AfxMessageBox("create error!"); return; } if ( !IsWindow(m_hWnd)) { AfxMessageBox("error!"); return; }}2.实现是在SDI的CVIEW中的,void CTest3View::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CMyClass server; server.StartThread();//这样发送就接受不了,PostMessage返回的是ERROR_SUCCESS, //server.SendMyMessage();//如果是这样发送消息就可以接受 CView::OnLButtonDown(nFlags, point);}3。MSDN中说:ERROR_SUCCESS The operation completed successfully,有点不明白, if(::PostMessage(my_hand,WM_MYMESS,NULL,NULL) == 0) { AfxMessageBox("PostMessage error!"); DWORD i = GetLastError(); int j = 0; }既然PostMessage返回0,为什么GetLastError = ERROR_SUCCESS??4。下面是我的全部代码::MyClass.h :#if !defined(AFX_MYCLASS_H__21492F03_52C0_47CA_89EC_F99114D0C0F5__INCLUDED_)#define AFX_MYCLASS_H__21492F03_52C0_47CA_89EC_F99114D0C0F5__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// MyClass.h : header file///////////////////////////////////////////////////////////////////////////////// CMyClass window#define WM_MYMESS WM_USER+1class CMyClass : public CWnd{// Constructionpublic: CMyClass();// Attributespublic: static UINT WorkThread(LPVOID P); void StartThread(); void SendMyMessage();// Operationspublic:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMyClass) //}}AFX_VIRTUAL// Implementationpublic: virtual ~CMyClass(); // Generated message map functionsprotected: //{{AFX_MSG(CMyClass) // NOTE - the ClassWizard will add and remove member functions here. afx_msg void RecvMessage(WPARAM wParam,LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP()};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYCLASS_H__21492F03_52C0_47CA_89EC_F99114D0C0F5__INCLUDED_)下面是MyClass.cpp:// MyClass.cpp : implementation file//#include "stdafx.h"#include "MyClass.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CMyClassCMyClass::CMyClass(){ if(Create(NULL,"mywindow",WS_CHILD,CRect(0,0,0,0),AfxGetMainWnd(),1000) == 0) { AfxMessageBox("create error!"); return; } if ( !IsWindow(m_hWnd)) { AfxMessageBox("error!"); return; }}CMyClass::~CMyClass(){}BEGIN_MESSAGE_MAP(CMyClass, CWnd) //{{AFX_MSG_MAP(CMyClass) // NOTE - the ClassWizard will add and remove mapping macros here. ON_MESSAGE(WM_MYMESS,RecvMessage) //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CMyClass message handlersUINT CMyClass::WorkThread(LPVOID P){ //CMyClass* point = (CMyClass*)P; HWND my_hand = (HWND)P; if(::PostMessage(my_hand,WM_MYMESS,NULL,NULL) == 0) { AfxMessageBox("PostMessage error!"); DWORD i = GetLastError(); int j = 0; } return 1;}void CMyClass::StartThread(){ AfxBeginThread(&WorkThread,(LPVOID)this->m_hWnd);}void CMyClass::SendMyMessage(){ ::SendMessage(m_hWnd,WM_MYMESS,NULL,NULL);}void CMyClass::RecvMessage(WPARAM wParam,LPARAM lParam){ AfxMessageBox("recv mess!");}实现是在SDI的VIEW中:void CTest3View::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CMyClass server; server.StartThread(); //server.SendMyMessage(); CView::OnLButtonDown(nFlags, point);}请哪位高手帮调试......... void CTest1View::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCMyClass my;my.StartThread();//my.SendMyMessage();CView::OnLButtonDown(nFlags, point);} ///////////你的CMyClass类只在过程中存在,放到CTestView类下 TO:Elysium:的确的哈,有点晕了哈,忘了用指针了但是还是有点疑问哈,1。MSDN上说 0 The operation completed successfully. ERROR_SUCCESS if(::PostMessage(my_hand,WM_MYMESS,NULL,NULL) == 0) { AfxMessageBox("PostMessage error!"); DWORD i = GetLastError(); } 为什么在PostMessage == 0 的时候,返回的是ERROR_SUCCESS 呢。2。刚才的问题是不是因为PostMessage发出消息后,并没有执行该消息,局部变量就不在了,所以没有执行,但是如果换成SendMessage,在消息相应后再返回,但是同样出现与PostMessage一样的问题,为什么呢? 而且即使在使用局部变量的时候,在一般的成员函数中使用SendMessage也能接收,???? postmessage返回0本来就是错误信息啊,成功了是非0值 的确是的,如果上面是因为PostMessage错误的话,是因为PostMessage发出消息后没有来得急执行窗口就销毁了,那如果用SendMessage,是执行完成才返回,然后窗口才被销毁,但是SendMessage也没有得到相应。而且一般的成员函数就能得到相应。那就说明一个问题,只可能是那个线程函数,static的原因,具体是什么原因,不知道, Elysium 東鱗覀爫 等 级: 发表于:2007-10-19 11:10:2412楼 得分:0 postmessage返回0本来就是错误信息啊,成功了是非0值 postmessage返回0是错误信息,但是用GetLastError返回的是ERROR_SUCCESS VC 得到字符串 初学者关于进程注入的问题,求助! 关于USB HID的问题,如何区分一个HID设备是鼠标还是键盘,或者其他的? VC开发问题 关于CSocket CSockFile CArchive 位图掩码问题 请问如何让对话框失去响应 大家看一下这有什么错, 搞过modem编程的大哥们请指教啊(关于TAPI)(300分求教) 我因为编程,老是睡眠不足怎么办? CComboBox问题! VC++中使用内存映射文件处理大文件
2、如果线程建立的消息循环,可以用PostThreadMessage给线程发消息
注意,PostMessage和SendMessage都是给有窗口的线程发消息的
1.已经建立了窗口:
CMyClass::CMyClass()
{
if(Create(NULL,"mywindow",WS_CHILD,CRect(0,0,0,0),AfxGetMainWnd(),1000) == 0)
{
AfxMessageBox("create error!");
return;
}
}
2.消息循环是有的,
(1)class CMyClass : public CWnd ,是从CWnd 继承的
(2)直接在成员函数中使用sendmessage()是能收到消息的,就是在线程中发送的消息不能收到。
1。在OnLButtonDown中 PostMessage(my_hand,WM_MYMESS,NULL,NULL) =0,而GetLastError() = 0(ERROR_SUCCESS),RecvMessag并没有执行PostMessage返回BOOL,返回0表示没成功
确认窗口已经创建好了。可以用IsWindow判断一下
1.窗口确认已经创建好了,在构造函数中:
CMyClass::CMyClass()
{
if(Create(NULL,"mywindow",WS_CHILD,CRect(0,0,0,0),AfxGetMainWnd(),1000) == 0)
{
AfxMessageBox("create error!");
return;
}
if ( !IsWindow(m_hWnd))
{
AfxMessageBox("error!");
return;
}
}
2.实现是在SDI的CVIEW中的,
void CTest3View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMyClass server;
server.StartThread();//这样发送就接受不了,PostMessage返回的是ERROR_SUCCESS,
//server.SendMyMessage();//如果是这样发送消息就可以接受
CView::OnLButtonDown(nFlags, point);
}
3。MSDN中说:ERROR_SUCCESS The operation completed successfully,有点不明白,
if(::PostMessage(my_hand,WM_MYMESS,NULL,NULL) == 0)
{
AfxMessageBox("PostMessage error!");
DWORD i = GetLastError();
int j = 0;
}
既然PostMessage返回0,为什么GetLastError = ERROR_SUCCESS??
4。下面是我的全部代码::
MyClass.h :
#if !defined(AFX_MYCLASS_H__21492F03_52C0_47CA_89EC_F99114D0C0F5__INCLUDED_)
#define AFX_MYCLASS_H__21492F03_52C0_47CA_89EC_F99114D0C0F5__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyClass.h : header file
///////////////////////////////////////////////////////////////////////////////
// CMyClass window
#define WM_MYMESS WM_USER+1
class CMyClass : public CWnd
{
// Construction
public:
CMyClass();// Attributes
public:
static UINT WorkThread(LPVOID P);
void StartThread();
void SendMyMessage();
// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyClass)
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CMyClass(); // Generated message map functions
protected:
//{{AFX_MSG(CMyClass)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg void RecvMessage(WPARAM wParam,LPARAM lParam);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYCLASS_H__21492F03_52C0_47CA_89EC_F99114D0C0F5__INCLUDED_)下面是MyClass.cpp:
// MyClass.cpp : implementation file
//#include "stdafx.h"
#include "MyClass.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMyClassCMyClass::CMyClass()
{
if(Create(NULL,"mywindow",WS_CHILD,CRect(0,0,0,0),AfxGetMainWnd(),1000) == 0)
{
AfxMessageBox("create error!");
return;
}
if ( !IsWindow(m_hWnd))
{
AfxMessageBox("error!");
return;
}
}CMyClass::~CMyClass()
{
}
BEGIN_MESSAGE_MAP(CMyClass, CWnd)
//{{AFX_MSG_MAP(CMyClass)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_MESSAGE(WM_MYMESS,RecvMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyClass message handlers
UINT CMyClass::WorkThread(LPVOID P)
{
//CMyClass* point = (CMyClass*)P;
HWND my_hand = (HWND)P;
if(::PostMessage(my_hand,WM_MYMESS,NULL,NULL) == 0)
{
AfxMessageBox("PostMessage error!");
DWORD i = GetLastError();
int j = 0;
}
return 1;
}
void CMyClass::StartThread()
{
AfxBeginThread(&WorkThread,(LPVOID)this->m_hWnd);
}
void CMyClass::SendMyMessage()
{
::SendMessage(m_hWnd,WM_MYMESS,NULL,NULL);}
void CMyClass::RecvMessage(WPARAM wParam,LPARAM lParam)
{
AfxMessageBox("recv mess!");
}
实现是在SDI的VIEW中:
void CTest3View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMyClass server;
server.StartThread();
//server.SendMyMessage();
CView::OnLButtonDown(nFlags, point);
}请哪位高手帮调试.........
{
// TODO: Add your message handler code here and/or call default
CMyClass my;
my.StartThread();
//my.SendMyMessage();
CView::OnLButtonDown(nFlags, point);
}
///////////
你的CMyClass类只在过程中存在,放到CTestView类下
if(::PostMessage(my_hand,WM_MYMESS,NULL,NULL) == 0)
{
AfxMessageBox("PostMessage error!");
DWORD i = GetLastError();
}
为什么在PostMessage == 0 的时候,返回的是ERROR_SUCCESS 呢。2。刚才的问题是不是因为PostMessage发出消息后,并没有执行该消息,局部变量就不在了,所以没有执行,但是如果换成SendMessage,在消息相应后再返回,但是同样出现与PostMessage一样的问题,为什么呢?
那就说明一个问题,只可能是那个线程函数,static的原因,具体是什么原因,不知道,
東鱗覀爫
等 级:
发表于:2007-10-19 11:10:2412楼 得分:0
postmessage返回0本来就是错误信息啊,成功了是非0值
postmessage返回0是错误信息,但是用GetLastError返回的是ERROR_SUCCESS