?? menudemoview.cpp
字號(hào):
// MenuDemoView.cpp : implementation of the CMenuDemoView class
//
#include "stdafx.h"
#include "MenuDemo.h"
#include "MenuDemoDoc.h"
#include "MenuDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMenuDemoView
IMPLEMENT_DYNCREATE(CMenuDemoView, CView)
BEGIN_MESSAGE_MAP(CMenuDemoView, CView)
//{{AFX_MSG_MAP(CMenuDemoView)
ON_COMMAND(ID_MENURUN, OnMenurun)
ON_UPDATE_COMMAND_UI(ID_MENURUN, OnUpdateMenurun)
ON_COMMAND(ID_MENUPOUSE, OnMenupouse)
ON_UPDATE_COMMAND_UI(ID_MENUPOUSE, OnUpdateMenupouse)
ON_COMMAND(IDS_ID_MENUSTOP, OnMenustop)//"停止程序"菜單項(xiàng)消息映射宏
ON_WM_RBUTTONDOWN()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMenuDemoView construction/destruction
CMenuDemoView::CMenuDemoView()
{
// TODO: add construction code here
IsRun=false;
IsPause=false;
Isinit=true;
Newstart=true;
m_CheckBitmap.LoadBitmap(IDB_STAR);
}
CMenuDemoView::~CMenuDemoView()
{
}
BOOL CMenuDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMenuDemoView drawing
void CMenuDemoView::OnDraw(CDC* pDC)
{
CMenuDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
// Associate bitmaps with the "Test" menu item.
CWnd *pWnd=AfxGetApp( )->GetMainWnd( ); //獲得窗口指針
CMenu * pMenu=pWnd->GetMenu( ); //獲得menu指針
CMenu* submenu = pMenu->GetSubMenu(5);
ASSERT(submenu->SetMenuItemBitmaps(ID_MENURUN, MF_BYCOMMAND,
&m_CheckBitmap, &m_CheckBitmap));
}
/////////////////////////////////////////////////////////////////////////////
// CMenuDemoView printing
BOOL CMenuDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMenuDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMenuDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMenuDemoView diagnostics
#ifdef _DEBUG
void CMenuDemoView::AssertValid() const
{
CView::AssertValid();
}
void CMenuDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMenuDemoDoc* CMenuDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMenuDemoDoc)));
return (CMenuDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMenuDemoView message handlers
void CMenuDemoView::OnMenurun()
{
// TODO: Add your command handler code here
IsPause=false;
IsRun=true;
CClientDC dc(this); //獲得DC
dc.TextOut(30,40,"運(yùn)行菜單項(xiàng)響應(yīng),程序正在運(yùn)行中!");//輸出文本
if(Newstart)
{
CWnd *pWnd=AfxGetApp( )->GetMainWnd( ); //獲得窗口指針
CMenu * pMenu=pWnd->GetMenu( ); //獲得menu指針
pMenu=pMenu->GetSubMenu(5); //獲得菜單項(xiàng)("操作"項(xiàng))
pMenu->AppendMenu(MF_SEPARATOR); //增加分隔符
//增加菜單項(xiàng)"停止程序(&S)"
pMenu->AppendMenu(MF_STRING,IDS_ID_MENUSTOP,"停止程序(&S)");
Newstart=false; //改變條件變量
}
}
void CMenuDemoView::OnUpdateMenurun(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(IsRun)
{
pCmdUI->SetText("運(yùn)行中");//更改菜單項(xiàng)標(biāo)題
pCmdUI->Enable(false);//菜單項(xiàng)無(wú)效
pCmdUI->SetCheck(false);//菜單項(xiàng)標(biāo)記取消
}
else
{
pCmdUI->SetText("運(yùn)行");//恢復(fù)菜單項(xiàng)標(biāo)題
pCmdUI->Enable(true);//菜單項(xiàng)生效
pCmdUI->SetCheck(true);//菜單項(xiàng)標(biāo)記恢復(fù)
}
}
void CMenuDemoView::OnMenupouse()
{
// TODO: Add your command handler code here
IsPause=true;
IsRun=false;
CClientDC dc(this); //獲得DC
dc.TextOut(30,40,"暫停菜單項(xiàng)響應(yīng),程序正在暫停中!");//輸出文本
}
void CMenuDemoView::OnUpdateMenupouse(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(Isinit)//程序第一次啟動(dòng)
{
pCmdUI->Enable(false);//暫停菜單項(xiàng)無(wú)效
Isinit=false;
}
else
{
if(IsPause)//暫停菜單項(xiàng)執(zhí)行
{
pCmdUI->SetText("暫停中");//更改菜單項(xiàng)標(biāo)題
pCmdUI->Enable(false);//菜單項(xiàng)無(wú)效
pCmdUI->SetCheck(false);//菜單項(xiàng)標(biāo)記取消
}
else
{
pCmdUI->SetText("暫停");//恢復(fù)菜單項(xiàng)標(biāo)題
pCmdUI->Enable(true);//菜單項(xiàng)生效
pCmdUI->SetCheck(true);//菜單項(xiàng)標(biāo)記恢復(fù)
}
}
}
void CMenuDemoView::OnMenustop() //"停止程序"菜單項(xiàng)消息映射函數(shù)
{
// TODO: Add your command handler code here
IsPause=true;
IsRun=false;
CClientDC dc(this); //獲得DC
dc.TextOut(30,40,"停止菜單項(xiàng)響應(yīng),程序已經(jīng)被停止!");//輸出文本
CWnd *pWnd=AfxGetApp( )->GetMainWnd( ); //獲得窗口指針
CMenu * pMenu=pWnd->GetMenu( ); //獲得menu指針
CMenu* submenu = pMenu->GetSubMenu(5);//獲取“命令”菜單項(xiàng)
submenu->RemoveMenu(3, MF_BYPOSITION);//將“停止程序”菜單項(xiàng)刪除
submenu->RemoveMenu(2, MF_BYPOSITION);//將分隔符刪除
Newstart=true; //改變條件變量
}
void CMenuDemoView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CWnd *pWnd=AfxGetApp( )->GetMainWnd( ); //獲得窗口指針
CMenu * pMenu=pWnd->GetMenu( ); //獲得menu指針
CMenu* submenu = pMenu->GetSubMenu(5);//獲取“命令”菜單項(xiàng)
CPoint pt;
GetCursorPos(&pt);//獲得鼠標(biāo)位置
submenu->TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,this); //彈出浮動(dòng)菜單
CView::OnRButtonDown(nFlags, point);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -