您的位置:首页 > 其它

MFC 定时触发器实现循环给指定线程进行发送消息

2013-12-02 14:36 573 查看
//头文件

#include "StdAfx.h"

#include "ATPC.h"

#include "TWinApp.h"

#include "MainWindow.h"

#include "TWndAttrs.h"

#include "TWinThread.h"

//窗体拖动的宏的定义

#define SC_DRAGMOVE (0xf012)

CMainWindow::CMainWindow()

{

}

CMainWindow::~CMainWindow(void)

{

 m_wndAttrs.Destory();

}

//获得桌面的位置参数

void CMainWindow::GetWindowPos(RECT& _in_out_rect)

{

 ::GetWindowRect( ::GetDesktopWindow(),&_in_out_rect);

 LONG nWidth  =_in_out_rect.right - _in_out_rect.left;

 LONG nHight = _in_out_rect.bottom -_in_out_rect.top;

 _in_out_rect.left = 0;

 _in_out_rect.top = 0;

 _in_out_rect.right = nWidth/4;

 _in_out_rect.bottom = nHight*2/3;

}

//创建主窗体

BOOL CMainWindow::MainWindowInit()

{

 m_pThread.CreateThread();//启动主线程通过拦截系统时间消息进行相关处理

 RECT rect = {0};

 GetWindowPos(rect);//参数引用,获得参数值的传递

 LPTWINDOWPROPERTIES pWndProperty =m_wndAttrs.GetWindowProperty(CTWndAttrs::E_WND_ID_MAINWND);

 if( NULL == pWndProperty ||

  false == CMainWindow::Create(pWndProperty->lpszClass , pWndProperty->lpszName, \

          pWndProperty->style, rect, NULL,\

          pWndProperty->lpszMenu, pWndProperty->dwExStyle,NULL))//构造函数直接进 行窗口的创建

 {

  _tprintf(_T("CMainWindow::Createfalse\r\n"));

  return false;

 }

 this->SetTimer(0x11223344, 2000,NULL);//触发一个定时器,每到这个时间段就会发送一个ON_WM_TIMER,然后拦截进行处理

 return true;

}

//创建自己的消息映射表(放在类的实现进行定义拦截消息的)

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)

 ON_WM_MOVE()

 ON_WM_CLOSE()

 ON_WM_TIMER()

 ON_WM_CREATE()

 ON_WM_CTLCOLOR()

 ON_WM_ERASEBKGND()

 ON_WM_SYSCOMMAND()

 //ON_MESSAGE(WM_USER_THREADEND,OnMyMess)//添加的消息映射

 //ON_COMMAND()

END_MESSAGE_MAP()

//处理单击关闭标题后的工作

void CMainWindow::OnClose()

{

 ::DestroyWindow(m_hWnd);//销毁活动的窗口(参数是获得当前活动窗口的句柄)

 ExitThread(0);

}

//在主窗体基础上进行控件的创建

int CMainWindow::OnCreate(LPCREATESTRUCT lpcreatestruct)

{

 LPTWINDOWPROPERTIES pWndproperty =m_wndAttrs.GetWindowProperty(CTWndAttrs::E_WND_ID_LISTBOX);

   

 RECT rect = {0};

 GetClientRect(&rect);

 

 if( NULL == pWndproperty || false ==m_listbox.Create(pWndproperty->style, CRect(rect), this,pWndproperty->id))

 {

  return false;

 }

 

 for(int i=0; i<50; ++i)

 {

  m_listbox.AddString(_T("TESTTEST TEST TEST TEST TEST TEST TEST TEST"));

  m_listbox.AddString(_T("山 东 科 技大 学 "));

 }

 return true;

 //returnCFrameWnd::OnCreate(lpcreatestruct);//并不是非要返回父类的成员函数OnCreate

}

//修改主窗体控件背景的颜色

BOOL CMainWindow::OnEraseBkgnd(CDC* pDC)

{

 return CFrameWnd::OnEraseBkgnd(pDC);

}

//设置控件的背景颜色和文本颜色

HBRUSH CMainWindow::OnCtlColor(CDC* pDC, CWnd* pWnd, UINTnCtlColor)

{

 if( CTLCOLOR_LISTBOX == nCtlColor && pWnd== &m_listbox )

 {

  LPTWINDOWPROPERTIESpWndProperty =m_wndAttrs.GetWindowProperty(CTWndAttrs::E_WND_ID_LISTBOX);

  if(pWndProperty)

  {

   pDC->SetTextColor(pWndProperty->colors[E_WND_COLOR_TEXT]);

   pDC->SetBkColor(pWndProperty->colors[E_WND_COLOR_BACKGROUND]);

   pDC->SetBkMode(OPAQUE);

   return::CreateSolidBrush(pWndProperty->colors[E_WND_COLOR_BACKGROUND]);

  }

 }

 return CFrameWnd::OnCtlColor(pDC, pWnd,nCtlColor);//其实不返回父类,也可以成功创建窗口控件                                            

}

//拦截系统消息WM_SYSCOMMAND 进行处理窗体的移动禁止

void CMainWindow::OnSysCommand(UINT nID, LPARAM lParam)

{

 if (  SC_DRAGMOVE != nID )

 {

  CFrameWnd::OnSysCommand(nID,lParam);

 }

}

//拦截系统时间消息进行处理
voidCMainWindow::OnTimer(UINT_PTR nIDEvent)

{

 static UINT_PTR evt = WM_USER +0;//用户信息的定义使用的是WM_USER+X(整数值)

 if( 0x11223344 == nIDEvent )

 {

  this->KillTimer(0x11223344);

  

  BOOL ret =::PostThreadMessage(m_pThread.m_nThreadID,evt++, 0,0);//把消息传送到指定ID的线程,不等待线程对消息的处理就会立即返回

  if( !ret )

  {

   DWORD err =GetLastError();//获得的具体的出错信息

   LPVOIDlpMsgBuf;//空类型指针

   ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS,

       NULL,err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Defaultlanguage

       (LPTSTR)&lpMsgBuf, 0,NULL );//获得err系统消息的ID来找到具体的消息的含义,然后首地址返回到空指针lpMsgBuf

   OutputDebugString((LPCWSTR)lpMsgBuf);

  }

  this->SetTimer(0x11223344,5000, NULL);

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: