Новичок
Профиль
Группа: Участник
Сообщений: 5
Регистрация: 12.1.2006
Репутация: нет Всего: нет
|
Я заполняю дерево данными из БД: | Код | // XSportsAppDlg.cpp : implementation file //
#include "stdafx.h" #include "XSportsApp.h" #include "XSportsAppDlg.h" #include ".\xsportsappdlg.h"
#ifdef _DEBUG #define new DEBUG_NEW #endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog { public: CAboutDlg();
// Dialog Data enum { IDD = IDD_ABOUTBOX };
protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation protected: DECLARE_MESSAGE_MAP() };
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { }
void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP()
// CXSportsAppDlg dialog
CXSportsAppDlg::CXSportsAppDlg(CWnd* pParent /*=NULL*/) : CDialog(CXSportsAppDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_bError=false; m_csPath=_T(""); }
void CXSportsAppDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); }
BEGIN_MESSAGE_MAP(CXSportsAppDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_WM_GETMINMAXINFO() ON_WM_CLOSE() ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnTvnSelchangedTree) ON_NOTIFY(TVN_ITEMEXPANDED, IDC_TREE, OnTvnItemExpandedTree) ON_CBN_SELCHANGE(IDC_CBOX, OnCbnSelchangeCBox) ON_WM_SIZE() END_MESSAGE_MAP()
// CXSportsAppDlg message handlers
BOOL CXSportsAppDlg::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
MoveWindow(0,0,700,500,TRUE); CenterWindow(); CRect rect; GetClientRect(rect); m_stat.Create("Стихия:",WS_VISIBLE|WS_CHILD,CRect(9,9,48,70),this,IDC_STAT); m_stat.SetFont(GetFont()); m_cbox.Create(WS_VISIBLE|WS_CHILD|CBS_DROPDOWNLIST,CRect(53,5,155,70),this,IDC_CBOX); m_cbox.SetFont(GetFont()); m_tree.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT|TVS_SHOWSELALWAYS/*|TVS_SINGLEEXPAND*/|TVS_TRACKSELECT,CRect(5,30,155,rect.Height()-5),this,IDC_TREE); m_html.Create(NULL,NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(160,5,rect.Width()-10,rect.Height()-10),this,IDC_HTML,NULL); m_html.MoveWindow(CRect(160,5,rect.Width()-5,rect.Height()-5)); m_html.Navigate(_T("About:Blank"),NULL,NULL,NULL,NULL);
m_csPath=m_html.GetFullName(); m_csPath.Replace("XSportsApp.exe","Source\\"); try{ _bstr_t strCnn("Provider=Microsoft.JET.OLEDB.4.0;Data Source="+m_csPath+"XSportsDB.mdb;"); if (FAILED(m_pCon.CreateInstance(__uuidof(Connection))))AfxMessageBox("Ошибка выделения памяти для соединения с БД."); if (FAILED(m_pRec.CreateInstance(__uuidof(Recordset))))AfxMessageBox("Ошибка выделения памяти для чтения из БД."); if (FAILED(m_pCon->Open(strCnn,"","",NULL)))AfxMessageBox("Ошибка соединения с БД"); if (FAILED(m_pRec->Open("XSportsList",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdTable)))AfxMessageBox("Ошибка чтения из БД"); } catch(...){ AfxMessageBox("При подключении к базе данных произошло исключение."); m_bError=true; CrashBlock(); }
this->SetWindowText("Энциклопедия экстремального спорта");
m_cbox.AddString("Земля"); m_cbox.AddString("Вода"); m_cbox.AddString("Воздух");
m_csPath.Replace('\\','/'); m_html.Navigate(_T(m_csPath+"root.html")); if(!m_bError){ if(m_pRec->GetRecordCount()==0){ AfxMessageBox("База данных пуста."); m_bError=true; CrashBlock(); } else{ m_cbox.SetCurSel(0); OnCbnSelchangeCBox(); HTREEITEM tmp=m_tree.GetFirstVisibleItem(); m_tree.Expand(tmp,TVE_EXPAND); } } return TRUE; // return TRUE unless you set the focus to a control }
void CXSportsAppDlg::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 CXSportsAppDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CXSportsAppDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); }
void CXSportsAppDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI) { lpMMI->ptMinTrackSize=CPoint(800,600);
CDialog::OnGetMinMaxInfo(lpMMI); }
bool CXSportsAppDlg::FillTree(unsigned long index,HTREEITEM parent) { bool crash=true; unsigned long i,j=m_pRec->GetFields()->GetItem("ID_Self")->GetValue().lVal; HTREEITEM tmp; for(m_pRec->MoveFirst(),i=0;!m_pRec->EndOfFile;m_pRec->MoveNext(),i++){ if(m_pRec->GetFields()->GetItem("ID_Parent")->GetValue().lVal==j){ tmp=m_tree.InsertItem(CString(m_pRec->GetFields()->GetItem("Name")->GetValue().bstrVal),parent); if(m_pRec->GetFields()->GetItem("HasChildren")->GetValue().boolVal) if(!FillTree(i,tmp))return false; crash=false; } } m_pRec->MoveFirst(); m_pRec->Move(index); if(crash)return false; return true; }
void CXSportsAppDlg::OnClose() { if(!m_bError){ m_pRec->Close(); m_pCon->Close(); }
CDialog::OnClose(); }
void CXSportsAppDlg::OnTvnSelchangedTree(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
HTREEITEM tmp=m_tree.GetSelectedItem(); if(!m_tree.ItemHasChildren(tmp)){ CString str2=m_tree.GetItemText(tmp); for(m_pRec->MoveFirst();!m_pRec->EndOfFile;m_pRec->MoveNext()){ if(m_pRec->GetFields()->GetItem("Name")->GetValue().bstrVal==str2){ str2=CString(m_pRec->GetFields()->GetItem("DescriptionFileName")->GetValue().bstrVal); m_html.Navigate2(_T(m_csPath+str2)); } } }
*pResult = 0; }
void CXSportsAppDlg::OnTvnItemExpandedTree(NMHDR *pNMHDR, LRESULT *pResult) { //? }
void CXSportsAppDlg::OnCbnSelchangeCBox() { m_tree.DeleteAllItems(); unsigned long i; int cs=m_cbox.GetCurSel()+1; HTREEITEM tmp; for(m_pRec->MoveFirst(),i=0;!m_pRec->EndOfFile;m_pRec->MoveNext(),i++){ if(m_pRec->GetFields()->GetItem("ID_Parent")->GetValue().lVal==0){ if(m_pRec->GetFields()->GetItem("Group")->GetValue().lVal==cs){ tmp=m_tree.InsertItem(CString(m_pRec->GetFields()->GetItem("Name")->GetValue().bstrVal)); if(m_pRec->GetFields()->GetItem("HasChildren")->GetValue().boolVal) if(!FillTree(i,tmp))m_bError=true; } } } if(m_bError){ AfxMessageBox("Ошибка в базе данных."); CrashBlock(); } }
void CXSportsAppDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy);
if(m_html.m_hWnd!=NULL){ CRect rect; GetClientRect(rect); m_tree.MoveWindow(CRect(5,30,155,rect.Height()-5)); m_html.MoveWindow(CRect(160,5,rect.Width()-5,rect.Height()-5)); } }
void CXSportsAppDlg::CrashBlock(){ m_cbox.EnableWindow(0); m_tree.EnableWindow(0); m_html.EnableWindow(0); }
|
Тут использован класс CHTMLCTRL для отображения HTML страниц внутри диалога(Его нету).
|