CodeMFC
CodeMFC
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));
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);
}
isClick = true;
InvalidateRect(&rect, TRUE);
}
void CMainWindow::OnDialog()
{
m_type = 2;
myDlg.DoModal();
Invalidate();
}
Dialog.h
#pragma once
#include<afxwin.h>
// Dialog 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()
{
}
BEGIN_MESSAGE_MAP(Dialog, CDialog)
ON_BN_CLICKED(IDC_DO,&Dialog::OnBnClickedDo)
ON_BN_CLICKED(IDC_SAVE, &Dialog::OnSave)
END_MESSAGE_MAP()
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>
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;
}
}