【VC开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了VC开源代码-DynamicMenuDlg.cpp的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
// DynamicMenuDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DynamicMenu.h"
#include "DynamicMenuDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern _ConnectionPtr m_pCon;
extern _RecordsetPtr m_pRecord;
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()
/////////////////////////////////////////////////////////////////////////////
// CDynamicMenuDlg dialog
CDynamicMenuDlg::CDynamicMenuDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDynamicMenuDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDynamicMenuDlg)
// 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 CDynamicMenuDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDynamicMenuDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDynamicMenuDlg, CDialog)
//{{AFX_MSG_MAP(CDynamicMenuDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDynamicMenuDlg message handlers
BOOL CDynamicMenuDlg::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
//删除所有菜单项
//CMenu* m_Menu = GetMenu();
// m_Menu->DeleteMenu(0,MF_BYPOSITION);
menu.CreateMenu();
LoadMenuFromDatabase();
// this->SetMenu(&menu);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDynamicMenuDlg::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 CDynamicMenuDlg::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 CDynamicMenuDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDynamicMenuDlg::OnOK()
{
//CDialog::OnOK();
}
//从数据库中加载菜单
void CDynamicMenuDlg::LoadMenuFromDatabase()
{
CString sql;
sql.Format( "select * from tb_menuinfo where 上级菜单 is NULL");
m_pRecord = m_pCon->Execute((_bstr_t)sql,NULL,adCmdText);
CMenu m_menu;
CString c_menustr;
while (! m_pRecord->ADOEOF)
{
c_menustr = m_pRecord->GetCollect("菜单名称").bstrVal;
//menu.AppendMenu(MF_STRING,-1,c_menustr);
LoadSubMenu(&menu,c_menustr);
m_pRecord->MoveNext();
}
SetMenu(&menu);
}
//利用递归的方式加载子菜单
void CDynamicMenuDlg::LoadSubMenu(CMenu* m_menu,CString str)
{
_ConnectionPtr m_con;
_RecordsetPtr m_record;
m_con.CreateInstance("ADODB.Connection");
m_record.CreateInstance("ADODB.Recordset");
m_con->ConnectionString = m_pCon->ConnectionString;
m_con->Open("","","",-1);
CString sql;
sql.Format("select * from tb_menuinfo where 上级菜单 = '%s'",str);
m_record = m_con->Execute((_bstr_t)sql,NULL,adCmdText);
CMenu m_tempmenu;
m_tempmenu.CreatePopupMenu();
while (!m_record->ADOEOF)
{
CString c_menustr = m_record->GetCollect("菜单名称").bstrVal;
m_tempmenu.AppendMenu(MF_STRING,-1,c_menustr);
if (IsHaveSubMenu(c_menustr))
LoadSubMenu(&m_tempmenu,c_menustr);
m_record->MoveNext();
}
m_menu->AppendMenu(MF_POPUP,(UINT)m_tempmenu.m_hMenu,str);
m_tempmenu.Detach();
if (m_record != NULL)
m_record.Release();
if (m_con!= NULL)
m_con.Release();
}
//判断指定的菜单是否有子菜单项
BOOL CDynamicMenuDlg::IsHaveSubMenu(CString str)
{
_ConnectionPtr m_con;
_RecordsetPtr m_record;
m_con.CreateInstance("ADODB.Connection");
m_record.CreateInstance("ADODB.Recordset");
m_con->ConnectionString = m_pCon->ConnectionString;
m_con->Open("","","",-1);
CString sql;
sql.Format("select * from tb_menuinfo where 上级菜单 = '%s'",str);
m_record = m_con->Execute((_bstr_t)sql,NULL,adCmdText);
if ((!m_record->BOF) &&(! m_record->ADOEOF))
{
return TRUE;
}
return FALSE;
}
BOOL CDynamicMenuDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
上一篇:
DynamicMenu.cpp
下一篇:
浮生若萍