hi,
Am trying to create a dialog box in MFC that has two buttons and an edit control.When i click button1,a dialog box that displays all the files in the system is displayed.when i select a file,the file name with the full path should be displayed in the edit control.When button2 is clicked tyhe file should open and i should be able to view the contents of the file.The code is as follows...
// dialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "dial.h"
#include "dialDlg.h"
#include<iostre am>
#include<fstrea m>
using namespace std;
#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::CAbo utDlg() : CDialog(CAboutD lg::IDD)
{
}
void CAboutDlg::DoDa taExchange(CDat aExchange* pDX)
{
CDialog::DoData Exchange(pDX);
}
BEGIN_MESSAGE_M AP(CAboutDlg, CDialog)
END_MESSAGE_MAP ()
// CdialDlg dialog
CdialDlg::Cdial Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CdialDl g::IDD, pParent)
, m_text(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_M AINFRAME);
}
void CdialDlg::DoDat aExchange(CData Exchange* pDX)
{
CDialog::DoData Exchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_text);
}
BEGIN_MESSAGE_M AP(CdialDlg, CDialog)
ON_WM_SYSCOMMAN D()
ON_WM_PAINT()
ON_WM_QUERYDRAG ICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(I DC_BUTTON1, &CdialDlg::OnBn ClickedButton1)
ON_BN_CLICKED(I DC_BUTTON2, &CdialDlg::OnBn ClickedButton2)
END_MESSAGE_MAP ()
// CdialDlg message handlers
BOOL CdialDlg::OnIni tDialog()
{
CDialog::OnInit Dialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABO UTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOU TBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(F ALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.Lo adString(IDS_AB OUTBOX);
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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CdialDlg::OnSys Command(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModa l();
}
else
{
CDialog::OnSysC ommand(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 CdialDlg::OnPai nt()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ ICONERASEBKGND, reinterpret_cas t<WPARAM>(dc.Ge tSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetric s(SM_CXICON);
int cyIcon = GetSystemMetric s(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::OnPain t();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CdialDlg::OnQue ryDragIcon()
{
return static_cast<HCU RSOR>(m_hIcon);
}
void CdialDlg::OnBnC lickedButton1()
{
CFileDialog FileDlg(TRUE, NULL, NULL, 0,NULL,NULL);
if( FileDlg.DoModal () == IDOK )
{
temp = FileDlg.GetPath Name();
m_text.SetStrin g(temp);
}
}
void CdialDlg::OnBnC lickedButton2()
{
CFile f1;
CString stri;
CString stri1;
stri="success";
stri1="failed";
if(f1.Open(temp ,CFile::modeRea d,&fileExceptio n)==TRUE)
{
::MessageBox(NU LL,(LPCWSTR)str i,NULL,0);
}
else
{
::MessageBox(NU LL,(LPCWSTR)str i1,NULL,0);
}
//::MessageBox(NU LL,(LPCWSTR)tem p,NULL,0);
}
My problem is, a dialog is displayed.But when a file is chosen it is not displayed in the edit control.Also on button2 click,i cannot open the file.plz tell me what is the issue here.anybpdy there to help me....?plzzzzzz zzz it is very urgent.
thanks a lot in advance..
shakthi
Am trying to create a dialog box in MFC that has two buttons and an edit control.When i click button1,a dialog box that displays all the files in the system is displayed.when i select a file,the file name with the full path should be displayed in the edit control.When button2 is clicked tyhe file should open and i should be able to view the contents of the file.The code is as follows...
// dialDlg.cpp : implementation file
//
#include "stdafx.h"
#include "dial.h"
#include "dialDlg.h"
#include<iostre am>
#include<fstrea m>
using namespace std;
#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::CAbo utDlg() : CDialog(CAboutD lg::IDD)
{
}
void CAboutDlg::DoDa taExchange(CDat aExchange* pDX)
{
CDialog::DoData Exchange(pDX);
}
BEGIN_MESSAGE_M AP(CAboutDlg, CDialog)
END_MESSAGE_MAP ()
// CdialDlg dialog
CdialDlg::Cdial Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CdialDl g::IDD, pParent)
, m_text(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_M AINFRAME);
}
void CdialDlg::DoDat aExchange(CData Exchange* pDX)
{
CDialog::DoData Exchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_text);
}
BEGIN_MESSAGE_M AP(CdialDlg, CDialog)
ON_WM_SYSCOMMAN D()
ON_WM_PAINT()
ON_WM_QUERYDRAG ICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(I DC_BUTTON1, &CdialDlg::OnBn ClickedButton1)
ON_BN_CLICKED(I DC_BUTTON2, &CdialDlg::OnBn ClickedButton2)
END_MESSAGE_MAP ()
// CdialDlg message handlers
BOOL CdialDlg::OnIni tDialog()
{
CDialog::OnInit Dialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABO UTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOU TBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(F ALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.Lo adString(IDS_AB OUTBOX);
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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CdialDlg::OnSys Command(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModa l();
}
else
{
CDialog::OnSysC ommand(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 CdialDlg::OnPai nt()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ ICONERASEBKGND, reinterpret_cas t<WPARAM>(dc.Ge tSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetric s(SM_CXICON);
int cyIcon = GetSystemMetric s(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::OnPain t();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CdialDlg::OnQue ryDragIcon()
{
return static_cast<HCU RSOR>(m_hIcon);
}
void CdialDlg::OnBnC lickedButton1()
{
CFileDialog FileDlg(TRUE, NULL, NULL, 0,NULL,NULL);
if( FileDlg.DoModal () == IDOK )
{
temp = FileDlg.GetPath Name();
m_text.SetStrin g(temp);
}
}
void CdialDlg::OnBnC lickedButton2()
{
CFile f1;
CString stri;
CString stri1;
stri="success";
stri1="failed";
if(f1.Open(temp ,CFile::modeRea d,&fileExceptio n)==TRUE)
{
::MessageBox(NU LL,(LPCWSTR)str i,NULL,0);
}
else
{
::MessageBox(NU LL,(LPCWSTR)str i1,NULL,0);
}
//::MessageBox(NU LL,(LPCWSTR)tem p,NULL,0);
}
My problem is, a dialog is displayed.But when a file is chosen it is not displayed in the edit control.Also on button2 click,i cannot open the file.plz tell me what is the issue here.anybpdy there to help me....?plzzzzzz zzz it is very urgent.
thanks a lot in advance..
shakthi
Comment