⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menufloatview.cpp

📁 精通MFC原书的所有代码打包成5个文件(由于一共太大,多次上传都失败了),这里是第一部分
💻 CPP
字号:
// MenuFloatView.cpp : implementation of the CMenuFloatView class
//

#include "stdafx.h"
#include "MenuFloat.h"

#include "MenuFloatDoc.h"
#include "MenuFloatView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMenuFloatView

IMPLEMENT_DYNCREATE(CMenuFloatView, CView)

BEGIN_MESSAGE_MAP(CMenuFloatView, CView)
	//{{AFX_MSG_MAP(CMenuFloatView)
	ON_COMMAND(ID_ADDMENU, OnAddmenu)
	ON_UPDATE_COMMAND_UI(ID_ADDMENU, OnUpdateAddmenu)
	ON_COMMAND(ID_POPUPMESSAGE, OnPopupmessage)//弹出消息框映射函数
	ON_COMMAND(ID_MENUITEM1, OnMenuitem1)
	ON_WM_RBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_KEYDOWN()
	ON_COMMAND(ID_MENUITEM3, OnMenuitem3)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMenuFloatView construction/destruction

CMenuFloatView::CMenuFloatView()
{
	// TODO: add construction code here
	canadd=true;

}

CMenuFloatView::~CMenuFloatView()
{
}

BOOL CMenuFloatView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMenuFloatView drawing

void CMenuFloatView::OnDraw(CDC* pDC)
{
	CMenuFloatDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMenuFloatView printing

BOOL CMenuFloatView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMenuFloatView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMenuFloatView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMenuFloatView diagnostics

#ifdef _DEBUG
void CMenuFloatView::AssertValid() const
{
	CView::AssertValid();
}

void CMenuFloatView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMenuFloatDoc* CMenuFloatView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMenuFloatDoc)));
	return (CMenuFloatDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMenuFloatView message handlers

void CMenuFloatView::OnAddmenu() 
{
	// TODO: Add your command handler code here
	CWnd *pWnd=AfxGetApp()->GetMainWnd();//获得窗口指针
	CMenu * pMenu=pWnd->GetMenu();//获得menu指针
	pMenu=pMenu->GetSubMenu(3);//获得菜单项("操作"项)
	pMenu->AppendMenu(MF_SEPARATOR);//增加分隔符
	pMenu->AppendMenu(MF_STRING,ID_POPUPMESSAGE,"弹出消息(&M)");//增加菜单项
	canadd=false;
	
}

void CMenuFloatView::OnUpdateAddmenu(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(!canadd)
	{
		pCmdUI->Enable(false);//使其无效
	}
	
}

void CMenuFloatView::OnPopupmessage()
{
	AfxMessageBox("动态添加菜单项成功");
}

void CMenuFloatView::OnMenuitem1() 
{
	// TODO: Add your command handler code here
	AfxMessageBox("浮动菜单消息1触发成功!");
	
}

void CMenuFloatView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMenu menu;			//定义菜单
	menu.LoadMenu(IDR_FLOATMENU);//载入浮动菜单资源
	CMenu* pM=menu.GetSubMenu(0);//菜单的第一项作为浮动菜单
	CPoint pt;
	GetCursorPos(&pt);//获得鼠标位置
	pM->TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,this);//弹出浮动菜单
	CView::OnRButtonDown(nFlags, point);
}

void CMenuFloatView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	if((nFlags&MK_LBUTTON) == MK_LBUTTON)//鼠标左键按下
	{
		CClientDC dc(this);//获得DC
		dc.MoveTo(start.x,start.y);//画线起点
		dc.LineTo(point.x,point.y);//画线终点
	}
	if((nFlags&MK_CONTROL) == MK_CONTROL)//Ctrl键按下
	{
		CClientDC dc(this);//获得DC
		dc.SetPixel(point.x,point.y,RGB(255,0,0));//填充鼠标经过的点
		dc.SetPixel((point.x-1),(point.y-1),RGB(255,0,0));//填充鼠标经过的左下点
		dc.SetPixel((point.x+1),(point.y+1),RGB(255,0,0));//填充鼠标经过的右上点
	}

	
	CView::OnMouseMove(nFlags, point);
}

void CMenuFloatView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	start.x=point.x;//获得起始点X坐标
	start.y=point.y;//获得起始点Y坐标
	
	CView::OnLButtonDown(nFlags, point);
}

void CMenuFloatView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	char keychar;//用户的按键
	HCURSOR mousecusor;//鼠标光标句柄
	keychar=char(nChar);
	if(keychar=='I')
	{
		mousecusor=AfxGetApp()->LoadStandardCursor(IDC_IBEAM);//获取系统标准光标
		SetCursor(mousecusor);//载入光标
	}
	if(keychar=='S')
	{
		mousecusor=AfxGetApp()->LoadStandardCursor(IDC_SIZENS);//获取系统标准光标
		SetCursor(mousecusor);//载入光标
	}
	if(keychar=='C')
	{
		mousecusor=AfxGetApp()->LoadStandardCursor(IDC_CROSS);//获取系统标准光标
		SetCursor(mousecusor);//载入光标
	}

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CMenuFloatView::OnMenuitem3() 
{
	// TODO: Add your command handler code here
	Invalidate(true);//客户区重绘
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -