在屏幕上作图

发表于:2007-07-01来源:作者:点击数: 标签:
建立一个透明的窗体: class CMyWnd : public CWnd { public: void CreateMyWnd(LPCTSTR pTitle,RECT CMyWnd(); public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTransparentWnd) public: //}}AFX_VIRTUAL // I


建立一个透明的窗体:

class CMyWnd : public CWnd 
{
public:
 void CreateMyWnd(LPCTSTR pTitle,RECT &rect);
 CMyWnd();
public:
// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CTransparentWnd)
 public:
 //}}AFX_VIRTUAL
  
  // Implementation
 public:
  afx_msg void OnRefresh();
  
  virtual ~CMyWnd();
 protected:
  // Generated message map functions
protected:
 //{{AFX_MSG(CTransparentWnd)
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
 afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
 afx_msg void OnExit();

 afx_msg void OnSet();
 afx_msg void OnExitAll();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

private:
 CPoint m_pt;
};

#endif // !defined(AFX_MYWND_H__9C4F7859_0D5B_4801_9FE3_401BBC31591C__INCLUDED_)

#include "stdafx.h"
#include "MyWnd.h"
#include "..\Page.h"
#include "..\PageDlg.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CMyWnd::CMyWnd()
{
 
}

CMyWnd::~CMyWnd()
{
 
}
BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
//{{AFX_MSG_MAP(CTransparentWnd)
 ON_WM_RBUTTONDOWN()
 ON_WM_LBUTTONDOWN()
 ON_WM_MOUSEMOVE()
 ON_COMMAND(IDC_EXIT,OnExit)
 ON_COMMAND(IDC_SET,OnSet)
 ON_COMMAND(IDC_REFRESH,OnRefresh)
 ON_COMMAND(IDC_EXIT_ALL,OnExitAll)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

 

void CMyWnd::CreateMyWnd(LPCTSTR pTitle,RECT &rect)
{  
 //建立窗口
 CreateEx(WS_EX_TRANSPARENT,  //透明
  AfxRegisterWndClass(0,AfxGetApp()->LoadCursor(IDC_PEN)),
  pTitle,
  WS_POPUP,
  rect,
  NULL,
  NULL,
  NULL );
}

void CMyWnd::OnRButtonDown(UINT nFlags, CPoint point)

 CMenu m_menu;
 m_menu.LoadMenu(IDR_MENU1); 
 
 ClientToScreen(&point);
 CMenu *psub = (CMenu *)m_menu.GetSubMenu(1);
 psub->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);
 m_menu.DestroyMenu();

 CWnd::OnRButtonDown(nFlags, point);
}

void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point)

 m_pt=point;  
 CWnd::OnLButtonDown(nFlags, point); 
}


void CMyWnd::OnMouseMove(UINT nFlags, CPoint point)

 if(nFlags & MK_LBUTTON)
 {  
  CClientDC dc(this);
  
  CPen pen(
     ((CPageDlg*)AfxGetMainWnd())->m_page1.m_nPenStyle,
     ((CPageDlg*)AfxGetMainWnd())->m_page1.m_nPenWidth,
     ((CPageDlg*)AfxGetMainWnd())->m_page1.m_PenColor
    );
  CPen* pOldPen=dc.SelectObject (&pen);

  dc.MoveTo (m_pt.x,m_pt.y);
  dc.LineTo (point.x,point.y);

  m_pt=point;
  dc.SelectObject (pOldPen);
 }
 
 CWnd::OnMouseMove(nFlags, point);
}

 

void CMyWnd::OnExit() //结束画图
{   
 ::SendMessage(GetSafeHwnd(),WM_CLOSE,0,0);
 ((CPageDlg*)AfxGetMainWnd())->m_page1.pWnd =NULL;
 delete ((CPageDlg*)AfxGetMainWnd())->m_page1.pWnd;

 AfxGetMainWnd()->ShowWindow(
  ((CPageDlg*)AfxGetMainWnd())->m_page1.m_bMinimized? SW_SHOWMINIMIZED : SW_SHOWNORMAL);
}

void CMyWnd::OnSet()    //设置
{
 ::SendMessage(GetSafeHwnd(),WM_CLOSE,0,0);
 ((CPageDlg*)AfxGetMainWnd())->m_page1.pWnd =NULL;
 delete ((CPageDlg*)AfxGetMainWnd())->m_page1.pWnd;
 AfxGetMainWnd()->ShowWindow(SW_SHOWNORMAL);
 
}

void CMyWnd::OnRefresh() //重新开始
{
 if(((CPageDlg*)AfxGetMainWnd())->m_page1.pWnd !=NULL)
 {
  ((CPageDlg*)AfxGetMainWnd())->m_page1.pWnd->ShowWindow(SW_HIDE);
  ((CPageDlg*)AfxGetMainWnd())->m_page1.pWnd->ShowWindow(SW_SHOWNORMAL);
 }
}

void CMyWnd::OnExitAll()  //退出程序
{
 if(MessageBox("确实要退出吗?","屏幕作图",MB_YESNO|MB_ICONQUESTION)==IDYES)
  AfxGetMainWnd()->SendMessage(WM_CLOSE,0,0);
}

开始:

void CPage1::OnStart()
{
 AfxGetMainWnd()->ShowWindow(SW_HIDE);
 
 UpdateData(); //更新变量,以便在CMyWnd中使用

 pWnd = new CMyWnd;
 CRect rect(0, 0, GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CXFULLSCREEN));
 pWnd->CreateMyWnd("", rect);       
 pWnd->ShowWindow(SW_SHOW);
 pWnd->SetWindowPos(&CWnd::wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
 
}

具体内容请参考源代码。

 


原文转自:http://www.ltesting.net