Лабораторная работа №6. Элементы управления «Шкала индикации» (Progress) и «Маркер» (Slider). Модальные диалоговые окна
1.

// CLabProgressBarDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CLabProgressBar.h"
#include "CLabProgressBarDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const double pi=3.14159265;
/////////////////////////////////////////////////////////////////////////////
// CCLabProgressBarDlg dialog
CCLabProgressBarDlg::CCLabProgressBarDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCLabProgressBarDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCLabProgressBarDlg)
m_res = 0.0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCLabProgressBarDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCLabProgressBarDlg)
DDX_Control(pDX, IDC_SLIDER1, m_slider);
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
DDX_Text(pDX, IDC_EDIT1, m_res);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCLabProgressBarDlg, CDialog)
//{{AFX_MSG_MAP(CCLabProgressBarDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCLabProgressBarDlg message handlers
BOOL CCLabProgressBarDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
// TODO: Add extra initialization here
m_slider.SetRange(100,1000000);
// устанавливаем диапазон в элементе Slider
m_slider.SetPos(100000);
//устанавливаем положение ползунка
//return TRUE; // return TRUE unless you set the focus to a control
//}
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CCLabProgressBarDlg::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 CCLabProgressBarDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCLabProgressBarDlg::OnButton1()
{
// TODO: Add your control notification handler code here
//обработчик кнопки Граничные условия.
d.DoModal(); //вызов модального окна
}
void CCLabProgressBarDlg::OnButton2()
{ //обработчик кнопки Пуск.
CWaitCursor w; //класс для установки курсора - часы.
double s=0;
double x=d.m_a*pi; //считывание из диалога нач. значения.
double h=(d.m_b*pi-x)/m_slider.GetPos(); //вычисление h=(b-a)/N
int i=0;
while(x<d.m_b*pi)
{ double f=sin(x); //вычисление синуса
s+=f*h; // вычисление суммы
x+=h;
m_progress.SetPos(100*i++/m_slider.GetPos());
//установка положения в полосе Progress
}m_res=s;
UpdateData(0);
}

Лабораторная работа №7.
SDI – приложение. Работа с данными в архитектуре «Документ/представление» («Document/view»)
11. В налоговой инспекции содержатся данные о предприятиях, которые определяют название, УНП, ФИО директора, вид деятельности, дата регистрации. Вывести все предприятия по видам деятельности (исключать ликвидированные).
// MDIDoc.cpp : implementation of the CMDIDoc class
//
#include "stdafx.h"
#include "MDI.h"
#include "MDIDoc.h"
#include "MyDialog.h"
#include "MyDialog2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMDIDoc
IMPLEMENT_DYNCREATE(CMDIDoc, CDocument)
BEGIN_MESSAGE_MAP(CMDIDoc, CDocument)
//{{AFX_MSG_MAP(CMDIDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMDIDoc construction/destruction
CMDIDoc::CMDIDoc()
{
// TODO: add one-time construction code here
}
CMDIDoc::~CMDIDoc()
{
}
CMDIDoc* CMDIDoc::GetDoc()
{
CMDIChildWnd * pChild =
((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();
if ( !pChild )
return NULL;
CDocument * pDoc = pChild->GetActiveDocument();
if ( !pDoc )
return NULL;
// Ошибка, если документ неправильного типа
if ( ! pDoc->IsKindOf( RUNTIME_CLASS(CMDIDoc) ) )
return NULL;
return (CMDIDoc *) pDoc;
}
BOOL CMDIDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
m_data.SetSize(1, 1);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMDIDoc serialization
void CMDIDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_data.GetSize();
for (int i=0; i < m_data.GetSize(); i++)
{
ar << m_data.GetAt(i).naz;
ar << m_data.GetAt(i).unp;
ar << m_data.GetAt(i).fio;
ar << m_data.GetAt(i).vid;
ar << m_data.GetAt(i).reg;
ar << m_data.GetAt(i).likv;
}
}
else
{
int size;
STRUCTRECORD rec;
ar >> size;
m_data.SetSize(size, 1);
for (int i=0; i < size; i++)
{
ar >> rec.naz;
ar >> rec.unp;
ar >> rec.fio;
ar >> rec.vid;
ar >> rec.reg;
ar >> rec.likv;
m_data.SetAt(i,rec);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CMDIDoc diagnostics
#ifdef _DEBUG
void CMDIDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMDIDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMDIDoc commands
// MDIView.cpp : implementation of the CMDIView class
//
#include "stdafx.h"
#include "MDI.h"
#include "MDIDoc.h"
#include "MDIView.h"
#include "MyDialog.h"
#include "MyDialog2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMDIView
IMPLEMENT_DYNCREATE(CMDIView, CView)
BEGIN_MESSAGE_MAP(CMDIView, CView)
//{{AFX_MSG_MAP(CMDIView)
ON_COMMAND(ID_MYBUTTON, OnMybutton)
ON_COMMAND(ID_MYBUTTON2, OnMybutton2)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMDIView construction/destruction
CMDIView::CMDIView()
{
// TODO: add construction code here
}
CMDIView::~CMDIView()
{
}
BOOL CMDIView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMDIView drawing
void CMDIView::OnDraw(CDC* pDC)
{
CMDIDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); // TODO: add draw code for native data here
CString s;
int i = pDoc->m_data.GetSize();
for (int j=0; j<(i-1); j++)
{
s.Format("%2d. %-20s %-9s %-20s %-20s %-10s",(j+1),
pDoc->m_data.GetAt(j).naz,
pDoc->m_data.GetAt(j).unp,
pDoc->m_data.GetAt(j).fio,
pDoc->m_data.GetAt(j).vid,
pDoc->m_data.GetAt(j).reg);
pDC->TextOut(10, (20+j*20), s);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMDIView printing
BOOL CMDIView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMDIView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMDIView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMDIView diagnostics
#ifdef _DEBUG
void CMDIView::AssertValid() const
{
CView::AssertValid();
}
void CMDIView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMDIDoc* CMDIView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDIDoc)));
return (CMDIDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMDIView message handlers
CMDIView* CMDIView::GetView()
{
CMDIChildWnd * pChild =
((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();
if ( !pChild )
return NULL;
CView * pView = pChild->GetActiveView();
if ( !pView )
return NULL;
// Ошибка, если Вид неправильного типа
if ( ! pView->IsKindOf( RUNTIME_CLASS(CMDIView) ) )
return NULL;
return (CMDIView *) pView;
}
void CMDIView::OnInitialUpdate()
{
CView::OnInitialUpdate();
CMDIDoc* pDoc = GetDocument();
}
void CMDIView::OnMybutton()
{
CMDIDoc* pDoc = GetDocument();
CMyDialog MyD;
MyD.DoModal(); // создаём новый диалог
STRUCTRECORD rec;
if (MyD.m_naz!="")
{
rec.naz = MyD.m_naz;
rec.unp = MyD.m_unp;
rec.fio = MyD.m_fio;
rec.vid = MyD.m_vid;
rec.reg = MyD.m_reg;
rec.likv = MyD.m_likv;
int i = pDoc->m_data.GetSize();
pDoc->m_data.SetAt((i-1),rec); // получаем новые значения
pDoc->m_data.SetSize((i+1),1); // увеличиваем размер массива
OnInitialUpdate(); // синхронизируем данные
Invalidate( TRUE ); // перерисовываем экран(вызов OnDraw())
pDoc->SetModifiedFlag(); // ставим флаг изменения документа
}
}
void CMDIView::OnMybutton2()
{
CMDIDoc* pDoc = GetDocument();
CMyDialog2 MyD;
MyD.DoModal();
OnInitialUpdate(); // синхронизируем данные
Invalidate( TRUE ); // перерисовываем экран(вызов OnDraw())
pDoc->SetModifiedFlag();
}
// MyDialog2.cpp : implementation file
//
#include "stdafx.h"
#include "MDI.h"
#include "MDIDoc.h"
#include "MDIView.h"
#include "MyDialog2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyDialog2 dialog
CMyDialog2::CMyDialog2(CWnd* pParent /*=NULL*/)
: CDialog(CMyDialog2::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDialog2)
//}}AFX_DATA_INIT
}
void CMyDialog2::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDialog2)
DDX_Control(pDX, IDC_LIST1, m_l);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDialog2, CDialog)
//{{AFX_MSG_MAP(CMyDialog2)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDialog2 message handlers
void CMyDialog2::OnButton1() //..........работает
{
m_l.ResetContent();
int i = CMDIDoc::GetDoc()->m_data.GetSize();
CString str;
UpdateData(true);
for (int j=0; j<(i-1); j++)
{
str.Format("%d. Название: %-20s УНП: %-9s ФИО: %-20s Вид: %-20s Дата: %-10s",(j+1),
CMDIDoc::GetDoc()->m_data.GetAt(j).naz,
CMDIDoc::GetDoc()->m_data.GetAt(j).unp,
CMDIDoc::GetDoc()->m_data.GetAt(j).fio,
CMDIDoc::GetDoc()->m_data.GetAt(j).vid,
CMDIDoc::GetDoc()->m_data.GetAt(j).reg);
m_l.AddString(str);
UpdateData(false);
}
}
void CMyDialog2::OnButton2() //..........работает
{
m_l.ResetContent();
}
void CMyDialog2::OnButton3()
{
m_l.ResetContent();
int i = CMDIDoc::GetDoc()->m_data.GetSize();
CString str,d;
int j,j1=0;
UpdateData(true);
STRUCTRECORD temp;
CArray <STRUCTRECORD, STRUCTRECORD> m_data2;
m_data2.SetSize(i, 1);
for ( j=0; j<(i-1); j++)
m_data2[j]=CMDIDoc::GetDoc()->m_data.GetAt(j);
for ( j=0; j<(i-1); j++)
{for ( j1=(i-2); j1>j; j1--)
{
if (m_data2[j1-1].vid<m_data2[j1].vid)
{
temp=m_data2[j1];
m_data2[j1]=m_data2[j1-1];
m_data2[j1-1]=temp;
}
}
}
int k=1;
for ( j=(i-2); j>=0; j--)
{
if (m_data2[j].likv==false)
{ str.Format("%d. Вид: %-20s Название: %-20s УНП: %-9s ФИО: %-20s Дата: %-10s",k,
m_data2[j].vid,
m_data2[j].naz,
m_data2[j].unp,
m_data2[j].fio,
m_data2[j].reg);
m_l.AddString(str);
k++;
UpdateData(false);
}
}
}





