Study/MFC MFC 다이얼로그 박스 만들기 코딩하는 야구쟁이 2011. 9. 30. 15:40 // MFC_20063703_4_2Dlg.h : header file // #if !defined(AFX_MFC_20063703_4_2DLG_H__EC2DCBE3_188F_4F0B_B5B9_52DD02873C9D__INCLUDED_) #define AFX_MFC_20063703_4_2DLG_H__EC2DCBE3_188F_4F0B_B5B9_52DD02873C9D__INCLUDED_ #include "CodeTableDlg.h" // Added by ClassView #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 ///////////////////////////////////////////////////////////////////////////// // CMFC_20063703_4_2Dlg dialog class CMFC_20063703_4_2Dlg : public CDialog { // Construction public: CCodeTableDlg m_dlgCodeTable; BOOL m_bCodeViewed; void UpdateStockInfo(); CMFC_20063703_4_2Dlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CMFC_20063703_4_2Dlg) enum { IDD = IDD_MFC_20063703_4_2_DIALOG }; CString m_strProdName; CString m_strProdCode; int m_nUnitCost; int m_nQuantity; int m_nTotalCost; CString m_strStockInfo; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMFC_20063703_4_2Dlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CMFC_20063703_4_2Dlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnChangeEditProdName(); afx_msg void OnChangeEditProdCode(); afx_msg void OnChangeEditUnitCost(); afx_msg void OnChangeEditQuantity(); afx_msg void OnButtonSave(); afx_msg void OnButtonOpen(); afx_msg void OnButtonCodeView(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_MFC_20063703_4_2DLG_H__EC2DCBE3_188F_4F0B_B5B9_52DD02873C9D__INCLUDED_) // MFC_20063703_4_2Dlg.cpp : implementation file // #include "stdafx.h" #include "MFC_20063703_4_2.h" #include "MFC_20063703_4_2Dlg.h" #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() ///////////////////////////////////////////////////////////////////////////// // CMFC_20063703_4_2Dlg dialog CMFC_20063703_4_2Dlg::CMFC_20063703_4_2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CMFC_20063703_4_2Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CMFC_20063703_4_2Dlg) m_strProdName = _T(""); m_strProdCode = _T(""); m_nUnitCost = 0; m_nQuantity = 0; m_nTotalCost = 0; m_strStockInfo = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_bCodeViewed = FALSE; } void CMFC_20063703_4_2Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMFC_20063703_4_2Dlg) DDX_Text(pDX, IDC_EDIT_PROD_NAME, m_strProdName); DDX_Text(pDX, IDC_EDIT_PROD_CODE, m_strProdCode); DDX_Text(pDX, IDC_EDIT_UNIT_COST, m_nUnitCost); DDX_Text(pDX, IDC_EDIT_QUANTITY, m_nQuantity); DDX_Text(pDX, IDC_EDIT_TOTAL_COST, m_nTotalCost); DDX_Text(pDX, IDC_EDIT_STOCK_INFO, m_strStockInfo); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMFC_20063703_4_2Dlg, CDialog) //{{AFX_MSG_MAP(CMFC_20063703_4_2Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_EN_CHANGE(IDC_EDIT_PROD_NAME, OnChangeEditProdName) ON_EN_CHANGE(IDC_EDIT_PROD_CODE, OnChangeEditProdCode) ON_EN_CHANGE(IDC_EDIT_UNIT_COST, OnChangeEditUnitCost) ON_EN_CHANGE(IDC_EDIT_QUANTITY, OnChangeEditQuantity) ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave) ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen) ON_BN_CLICKED(IDC_BUTTON_CODE_VIEW, OnButtonCodeView) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMFC_20063703_4_2Dlg message handlers BOOL CMFC_20063703_4_2Dlg::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 // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CMFC_20063703_4_2Dlg::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 CMFC_20063703_4_2Dlg::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 CMFC_20063703_4_2Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CMFC_20063703_4_2Dlg::UpdateStockInfo() { UpdateData(TRUE); m_nTotalCost = m_nUnitCost * m_nQuantity; m_strStockInfo.Format("제품명 : %s \r\n\r\n 제품코드 : %s \r\n\r\n 단가 : %d \r\n\r\n 수량 : %d \r\n\r\n 총액 : %d \r\n", m_strProdName, m_strProdCode, m_nUnitCost, m_nQuantity, m_nTotalCost); UpdateData(FALSE); } void CMFC_20063703_4_2Dlg::OnChangeEditProdName() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here UpdateStockInfo(); } void CMFC_20063703_4_2Dlg::OnChangeEditProdCode() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here UpdateStockInfo(); } void CMFC_20063703_4_2Dlg::OnChangeEditUnitCost() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here UpdateStockInfo(); } void CMFC_20063703_4_2Dlg::OnChangeEditQuantity() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here UpdateStockInfo(); } void CMFC_20063703_4_2Dlg::OnButtonSave() { // TODO: Add your control notification handler code here char Filter[] = "p42 File(*.p42) |*.p42| 모든파일(*.*) |*.*|"; CFileDialog pDlg(FALSE, "p42 file(*.p42)", "*.p42", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, Filter, NULL); UpdateData(TRUE); if(!m_strProdName.IsEmpty() && !m_strProdCode.IsEmpty() && (m_nUnitCost>0) && (m_nQuantity>0) && (m_nTotalCost>0)) { if(pDlg.DoModal() == IDOK) { FILE *fp; fp = fopen(pDlg.GetPathName(), "w"); fprintf(fp, "%s\n%s\n%d\n%d\n%d", m_strProdName, m_strProdCode, m_nUnitCost, m_nQuantity, m_nTotalCost); fclose(fp); } } else { AfxMessageBox("정보를 입력해주세요(단가, 수량, 총액은 1이상)"); } } void CMFC_20063703_4_2Dlg::OnButtonOpen() { // TODO: Add your control notification handler code here char Filter[] = "p42 File(*.p42) |*.p42| 모든파일(*.*) |*.*|"; CFileDialog pDlg(TRUE, "p42 file(*.p42)", "*.p42", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, Filter, NULL); if(pDlg.DoModal() == IDOK) { FILE *fp; fp = fopen(pDlg.GetPathName(), "r"); fscanf(fp, "%s\n", m_strProdName); fscanf(fp, "%s\n", m_strProdCode); fscanf(fp, "%d\n%d\n%d", &m_nUnitCost, &m_nQuantity, &m_nTotalCost); UpdateData(FALSE); UpdateStockInfo(); fclose(fp); } } void CMFC_20063703_4_2Dlg::OnButtonCodeView() { // TODO: Add your control notification handler code here if(!m_bCodeViewed) { m_dlgCodeTable.Create(IDD_DIALOG_CODE_TABLE, this); CRect rectMain, rectCodeTable; GetWindowRect(&rectMain); m_dlgCodeTable.GetWindowRect(&rectCodeTable); m_dlgCodeTable.MoveWindow(rectMain.right, rectMain.top, rectCodeTable.Width(), rectCodeTable.Height()); m_dlgCodeTable.ShowWindow(SW_SHOW); m_bCodeViewed = TRUE; } else { m_dlgCodeTable.ShowWindow(SW_HIDE); m_dlgCodeTable.DestroyWindow(); m_bCodeViewed = FALSE; } }