天天看點

MFC-消息映射機制

MFC-消息映射機制

#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
public:
  virtual LRESULT WindowProc(UINT msgID,WPARAM wParam,LPARAM IParam);
};

LRESULT MyFrameWnd::WindowProc(UINT msgID, WPARAM wParam, LPARAM IParam) {
  switch (msgID)
  {
     // 視窗被建立時觸發
   case WM_CREATE:
    AfxMessageBox("Hi Major");
    break;
  }
  return CFrameWnd::WindowProc(msgID,wParam,IParam);
}

class MyWinApp :public CWinApp {
public:
  MyWinApp() {

  }
  virtual BOOL InitInstance() {

  MyFrameWnd* pFrame  = new MyFrameWnd;
  pFrame->Create(NULL,"Test");
  m_pMainWnd = pFrame;
  pFrame->ShowWindow(SW_SHOW);
  pFrame->UpdateWindow();
  return TRUE;
  }
};

MyWinApp mainApp;      
MFC-消息映射機制
#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
  // 類内聲明宏
  DECLARE_MESSAGE_MAP()
public:
  LRESULT OnCreate(WPARAM wParam,LPARAM IParam);
};

// 類外實作宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
  ON_MESSAGE(WM_CREATE,OnCreate)
END_MESSAGE_MAP()

LRESULT MyFrameWnd::OnCreate(WPARAM wParam, LPARAM IParam) {
  AfxMessageBox("Hi Major!");
  return 0;
}

class MyWinApp :public CWinApp {
public:
  MyWinApp() {

  }
  virtual BOOL InitInstance() {

  MyFrameWnd* pFrame  = new MyFrameWnd;
  pFrame->Create(NULL,"Test");
  m_pMainWnd = pFrame;
  pFrame->ShowWindow(SW_SHOW);
  pFrame->UpdateWindow();
  return TRUE;
  }
};

MyWinApp mainApp;      
MFC-消息映射機制
#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
  // 類内聲明宏
  DECLARE_MESSAGE_MAP()
public:
  int OnCreate(LPCREATESTRUCT pcs);
};

// 類外實作宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
  ON_WM_CREATE()
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
  AfxMessageBox("Hi Major!");
  return CFrameWnd::OnCreate(pcs);
}

class MyWinApp :public CWinApp {
public:
  MyWinApp() {

  }
  virtual BOOL InitInstance() {

  MyFrameWnd* pFrame  = new MyFrameWnd;
  pFrame->Create(NULL,"Test");
  m_pMainWnd = pFrame;
  pFrame->ShowWindow(SW_SHOW);
  pFrame->UpdateWindow();
  return TRUE;
  }
};

MyWinApp mainApp;      
MFC-消息映射機制
#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
  // 類内聲明宏
  DECLARE_MESSAGE_MAP()
public:
  int OnCreate(LPCREATESTRUCT pcs);
  void OnPaint();
};

// 類外實作宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
  ON_WM_CREATE()
  ON_WM_PAINT()
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
  AfxMessageBox("Hi Major!");
  return CFrameWnd::OnCreate(pcs);
}

void MyFrameWnd::OnPaint() {
  PAINTSTRUCT ps = { 0 };
  HDC hdc = ::BeginPaint(this->m_hWnd,&ps);
  ::TextOutA(hdc,100,100,"Hi Major!",9);
  ::EndPaint(m_hWnd,&ps);
}



class MyWinApp :public CWinApp {
public:
  MyWinApp() {

  }
  virtual BOOL InitInstance() {

  MyFrameWnd* pFrame  = new MyFrameWnd;
  pFrame->Create(NULL,"Test");
  m_pMainWnd = pFrame;
  pFrame->ShowWindow(SW_SHOW);
  pFrame->UpdateWindow();
  return TRUE;
  }
};

MyWinApp mainApp;      
MFC-消息映射機制
#include <afxwin.h>

class MyFrameWnd :public CFrameWnd {
  // 類内聲明宏
  DECLARE_MESSAGE_MAP()
public:
  int OnCreate(LPCREATESTRUCT pcs);
  void OnPaint();
  void OnMouseMove(UINT nKey,CPoint pt);
  int m_x;
  int m_y;
};

// 類外實作宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
  ON_WM_CREATE()
  ON_WM_PAINT()
  ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
  AfxMessageBox("Hi Major!");
  return CFrameWnd::OnCreate(pcs);
}

void MyFrameWnd::OnPaint() {
  PAINTSTRUCT ps = { 0 };
  HDC hdc = ::BeginPaint(this->m_hWnd,&ps);
  ::TextOutA(hdc, m_x, m_y,"Hi Major!",9);
  ::EndPaint(m_hWnd,&ps);
}

void MyFrameWnd::OnMouseMove(UINT nKey, CPoint pt) {
  m_x = pt.x;
  m_y = pt.y;
  ::InvalidateRect(this->m_hWnd,NULL,TRUE);
}

class MyWinApp :public CWinApp {
public:
  MyWinApp() {

  }
  virtual BOOL InitInstance() {

  MyFrameWnd* pFrame  = new MyFrameWnd;
  pFrame->Create(NULL,"Test");
  m_pMainWnd = pFrame;
  pFrame->ShowWindow(SW_SHOW);
  pFrame->UpdateWindow();
  return TRUE;
  }
};

MyWinApp mainApp;      
#include <afxwin.h>

// 自定義消息
#define WM_MYMESSAGE WM_USER+001

class MyFrameWnd :public CFrameWnd {
  // 類内聲明宏
  DECLARE_MESSAGE_MAP()
public:
  int OnCreate(LPCREATESTRUCT pcs);
  void OnPaint();
  void OnMouseMove(UINT nKey,CPoint pt);
  LRESULT OnMyMessage(WPARAM wParam,LPARAM IParam);
  int m_x;
  int m_y;
};

// 類外實作宏
BEGIN_MESSAGE_MAP(MyFrameWnd,CFrameWnd)
  ON_WM_CREATE()
  ON_WM_PAINT()
  ON_WM_MOUSEMOVE()
  ON_MESSAGE(WM_MYMESSAGE,OnMyMessage)
END_MESSAGE_MAP()

int MyFrameWnd::OnCreate(LPCREATESTRUCT pcs) {
  AfxMessageBox("Hi Major!");
  // 發送消息
  ::PostMessage(this->m_hWnd, WM_MYMESSAGE,1,2);
  return CFrameWnd::OnCreate(pcs);
}

// 對應函數
LRESULT MyFrameWnd::OnMyMessage(WPARAM wParam, LPARAM IParam) {
  CString str;
  str.Format("wParam=%d,IParam=%d",wParam,IParam);
  AfxMessageBox(str);
  return 0;
}

void MyFrameWnd::OnPaint() {
  PAINTSTRUCT ps = { 0 };
  HDC hdc = ::BeginPaint(this->m_hWnd,&ps);
  ::TextOutA(hdc, m_x, m_y,"Hi Major!",9);
  ::EndPaint(m_hWnd,&ps);
}

void MyFrameWnd::OnMouseMove(UINT nKey, CPoint pt) {
  m_x = pt.x;
  m_y = pt.y;
  ::InvalidateRect(this->m_hWnd,NULL,TRUE);
}

class MyWinApp :public CWinApp {
public:
  MyWinApp() {

  }
  virtual BOOL InitInstance() {

  MyFrameWnd* pFrame  = new MyFrameWnd;
  pFrame->Create(NULL,"Test");
  m_pMainWnd = pFrame;
  pFrame->ShowWindow(SW_SHOW);
  pFrame->UpdateWindow();
  return TRUE;
  }
};

MyWinApp mainApp;      

繼續閱讀