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

📄 testdlg.cpp

📁 《Visual C++编程技巧典型案例解析:基础与应用篇(下)(含1CD-ROM)——编程技巧典型案例集锦系列》源码
💻 CPP
字号:
// TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include <afxpriv.h>              // For WM_KICKIDLE
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	m_bFlag = TRUE;
	//{{AFX_DATA_INIT(CTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_MENUSELECT()
	ON_COMMAND(ID_TEST_11, OnTest11)
	ON_UPDATE_COMMAND_UI(ID_TEST_11, OnUpdateTest11)
	ON_COMMAND(ID_TEST_2, OnTest2)
	ON_UPDATE_COMMAND_UI(ID_TEST_2, OnUpdateTest2)
	ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
	ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

BOOL CTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	//创建工具栏
	if(!m_wndToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD|WS_VISIBLE|CBRS_TOP| CBRS_GRIPPER ,CRect(4,4,0,0))||!m_wndToolBar.LoadToolBar(IDR_DIALOGTOOLBAR) )
	{
		MessageBox("工具栏创建失败","提示",MB_ICONWARNING|MB_OK);
		return FALSE;
	}
	//显示工具栏
	m_wndToolBar.ShowWindow(SW_SHOW);
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST, 0);
	
	//加载加速键
	m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CTestDlg::PreTranslateMessage(MSG* pMsg) 
{
	//更新工具栏
	if(m_wndToolBar.IsWindowVisible())
		m_wndToolBar.OnUpdateCmdUI((CFrameWnd *)this, TRUE);

	//处理加速键,返回值为0表示不是加速键信息
	if(! ::TranslateAccelerator(m_hWnd,m_hAccel,pMsg))
		return CDialog::PreTranslateMessage(pMsg);
	else
		return TRUE;	
}

//响应消息WM_KICKIDLE,用于更新工具栏(仅仅用于模态对话框)

LRESULT CTestDlg::OnKickIdle(WPARAM wParam, LPARAM lParam)
{
//下面两行代码和函数PreTranslateMessage(MSG* pMsg)中两行代码作用相同
//要测试这两行代码作用,先屏蔽函数PreTranslateMessage(MSG* pMsg)中两行代码
//再启用下面两行代码
  //@**#---2002-12-24 14:23:32 (orbit)---#**@
//if(m_wndToolBar.IsWindowVisible())
//		m_wndToolBar.OnUpdateCmdUI((CFrameWnd *)this, TRUE);
	return 0;
}

void CTestDlg::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSysMenu) 
{
	CCmdUI cmdUI;
	UINT   nCount ,i;
	CMenu * pMainMenu = NULL,*pSubMenu=NULL;

	if(hSysMenu==NULL||!(nFlags&MF_POPUP))//菜单句柄为空或点击的不是下拉菜单项
		return ;
	pMainMenu =CMenu::FromHandle(hSysMenu);//根据菜单句柄获取菜单指针
	pSubMenu = pMainMenu-> GetSubMenu(nItemID);//获取子菜单指针

    if(IsMenu(pSubMenu->GetSafeHmenu()))
    {
        cmdUI.m_nIndexMax = pSubMenu->GetMenuItemCount();//获取子菜单项的个数

		//循环依次更新子菜单项
        for (i = 0; i < cmdUI.m_nIndexMax;++i)
        {	
            cmdUI.m_nIndex = i;
            cmdUI.m_nID = pSubMenu->GetMenuItemID(i);//获取子菜单项的指针
			if(cmdUI.m_nID==(UINT)-1||cmdUI.m_nID==0)//子菜单项为下拉菜单或分组栏
				continue;
            cmdUI.m_pMenu = pSubMenu;
            cmdUI.DoUpdate(this,false); //发送ON_UPDATE_COMMAND_UI消息

			// 判断是否增加或删除了菜单项
			nCount = pSubMenu->GetMenuItemCount();
			if (nCount < cmdUI.m_nIndexMax)
			{
				cmdUI.m_nIndex -= (cmdUI.m_nIndexMax - nCount);
				while (cmdUI.m_nIndex < nCount && pSubMenu->GetMenuItemID(cmdUI.m_nIndex) == cmdUI.m_nID)
				{
					cmdUI.m_nIndex++;
				}
			}
			cmdUI.m_nIndexMax = nCount;
        }
     }
}

void CTestDlg::OnTest11() 
{
	MessageBox("arrive 11");
}

void CTestDlg::OnUpdateTest11(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_bFlag==TRUE);
	
}

void CTestDlg::OnTest2() 
{
	MessageBox("arrive 2");
	
}

void CTestDlg::OnUpdateTest2(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_bFlag);
	
}

void CTestDlg::OnButtonTest() 
{
	if(m_bFlag)
		m_bFlag = FALSE;	
	else
		m_bFlag = TRUE;	
}

⌨️ 快捷键说明

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