0% found this document useful (0 votes)
9 views

CodeMFC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CodeMFC

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Main.

h
#include<afxwin.h>
#define caro 3
bool isClick = false;
bool drawFirst = true;
RECT rect;
int cx, cy;
int m_Width = 200, m_Height = 200;
int padding_X = 400, padding_Y = 150;
CBrush blue(RGB(0, 0, 255)), red(RGB(255, 0, 0));

class CMyApp : public CWinApp


{
public:
virtual BOOL InitInstance();
};

class CMainWindow : public CFrameWnd


{
public:
afx_msg void OnPaint();
afx_msg void OnUpdateRed();
afx_msg void OnUpdateBlue();
afx_msg void OnUpdateUIRed(CCmdUI* pCmdUI);
afx_msg void OnUpdateUIBlue(CCmdUI* pCmdUI);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

int m_type = 0;
afx_msg void OnDialog();
CMainWindow();
DECLARE_MESSAGE_MAP()
};

Main.cpp
#include<afxwin.h>
#include "Main.h"
#include "resource.h"
#include "Dialog.h"
#include "CObjFile.h"
CMyApp myApp;
Dialog myDlg;
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_WM_PAINT()
ON_COMMAND(ID_COLOR_RED, &CMainWindow::OnUpdateRed)
ON_COMMAND(ID_COLOR_BLUE, &CMainWindow::OnUpdateBlue)
ON_UPDATE_COMMAND_UI(ID_COLOR_RED, &CMainWindow::OnUpdateUIRed)
ON_UPDATE_COMMAND_UI(ID_COLOR_BLUE, &CMainWindow::OnUpdateUIBlue)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_MENU, &CMainWindow::OnDialog)
END_MESSAGE_MAP()
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CMainWindow::CMainWindow()
{
LoadFrame(IDR_MENU1, WS_SYSMENU, NULL, NULL);
LoadAccelTable(ATL_MAKEINTRESOURCE(IDR_ACCELERATOR1));
}

void CMainWindow::OnPaint()
{
CPaintDC dc(this);
CRect rcClient;
GetClientRect(&rcClient);
cx = m_Width / caro;
cy = m_Height / caro;
if (m_type != 2) {
for (int x = 0; x < caro; x++)
{
for (int y = 0; y < caro; y++)
{
dc.Rectangle((x * cx) + padding_X, (y * cy) + padding_Y, ((x + 1) * cx) + padding_X, ((y + 1) * cy) +
padding_Y);
if (isClick) {
if (m_type == 0)
{
dc.SelectObject(&red);
}
else
{
dc.SelectObject(blue);
}
dc.Rectangle((x * cx) + padding_X, (y * cy) + padding_Y, ((x + 1) *
cx) + padding_X, ((y + 1) * cy) + padding_Y);
}
}
}
}
if (m_type == 2)
{
CFile file;
file.Open(L"info.txt", CFile::modeRead);
CArchive ar(&file, CArchive::load);
CObjFile infor;
infor.Serialize(ar);
ar.Close();
dc.Rectangle(50, 50, infor.m_x, infor.m_y);
}

void CMainWindow::OnUpdateRed()
{
m_type = 0;
isClick = false;
}

void CMainWindow::OnUpdateBlue()
{
m_type = 1;
isClick = false;
}
void CMainWindow::OnUpdateUIRed(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_type == 0);
}

void CMainWindow::OnUpdateUIBlue(CCmdUI* pCmdUI) {


pCmdUI->SetCheck(m_type == 1);
}

void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point) {


int x, y;
x = (point.x - padding_X) / cx;
y = (point.y - padding_Y) / cy;

if (x < caro && y < caro)


{
rect.left = (x * cx) + padding_X;
rect.top = (y * cy) + padding_Y;
rect.right = ((x + 1) * cx) + padding_X;
rect.bottom = ((y + 1) * cx) + padding_Y;

isClick = true;
InvalidateRect(&rect, TRUE);
}

void CMainWindow::OnDialog()
{
m_type = 2;
myDlg.DoModal();
Invalidate();
}

Dialog.h
#pragma once
#include<afxwin.h>

// Dialog dialog

class Dialog : public CDialog


{
DECLARE_DYNAMIC(Dialog)

public:
Dialog(CWnd* pParent = nullptr); // standard constructor
virtual ~Dialog();

// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_DIALOG1 };
#endif

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

DECLARE_MESSAGE_MAP()
public:
int m_a = 0;
int m_b = 0;
int m_c = 0;
int m_pt = 0;
afx_msg void OnBnClickedDo();
int m_dai = 0;
int m_rong = 0 ;
afx_msg void OnSave();
};

Dialog.cpp
// Dialog.cpp : implementation file
//
#include<afxwin.h>
#include "Dialog.h"
#include "afxdialogex.h"
#include "resource.h"
#include "CObjFile.h"

// Dialog dialog

IMPLEMENT_DYNAMIC(Dialog, CDialog)

Dialog::Dialog(CWnd* pParent /*=nullptr*/)


: CDialog(IDD_DIALOG1, pParent)
{

}
Dialog::~Dialog()
{
}

void Dialog::DoDataExchange(CDataExchange* pDX)


{
DDX_Text(pDX, IDC_A, m_a);
DDX_Text(pDX, IDC_B, m_b);
DDX_Text(pDX, IDC_C, m_c);
DDX_Radio(pDX, IDC_SUM, m_pt);

DDX_Text(pDX, IDC_DAI, m_dai);


DDX_Text(pDX, IDC_RONG, m_rong);
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(Dialog, CDialog)
ON_BN_CLICKED(IDC_DO,&Dialog::OnBnClickedDo)
ON_BN_CLICKED(IDC_SAVE, &Dialog::OnSave)
END_MESSAGE_MAP()

// Dialog message handlers

void Dialog::OnBnClickedDo()
{
UpdateData(TRUE);
if (m_pt == 0)
{
m_c = m_a + m_b;
}
else
{
m_c = m_a - m_b;
}
UpdateData(FALSE);
}

void Dialog::OnSave()
{
UpdateData(TRUE);
CObjFile infor;
CFile file;
file.Open(L"info.txt", CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file, CArchive::store);
infor.m_x = m_dai;
infor.m_y = m_rong;
infor.Serialize(ar);
ar.Close();
CString msg;
msg.Format(_T("kich thuoc da duoc luu %d %d"), m_dai, m_rong);
MessageBox(msg, _T("Thong Bao", MB_YESNO));

UpdateData(FALSE);
}
CobjFile.h
#include<afxwin.h>

class CObjFile : public CObject


{
public:
int m_x, m_y;
CObjFile(void);
void Serialize(CArchive& ar);
DECLARE_SERIAL(CObjFile);
};

CObjFile.cpp
#include <afxwin.h>
#include "CObjFile.h"
CObjFile::CObjFile() {
}
IMPLEMENT_SERIAL(CObjFile, CObject, 0)
void CObjFile::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsStoring()) {
ar << m_x << m_y;
}
else
{
ar >> m_x >> m_y;
}
}

You might also like