// test1Dlg.cpp : implementation file // #include "stdafx.h" #include "test1.h" #include "test1Dlg.h" #include ".\test1dlg.h" #include "EnterNumber.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // Ctest1Dlg dialog Ctest1Dlg::Ctest1Dlg(CWnd* pParent /*=NULL*/) : CDialog(Ctest1Dlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void Ctest1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(Ctest1Dlg, CDialog) ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDOK, OnBnClickedOk) ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1) ON_BN_CLICKED(CLEAR, OnBnClickedClear) ON_WM_LBUTTONDOWN() ON_BN_CLICKED(IDC_ABOUT, OnBnClickedAbout) ON_BN_CLICKED(CLEARCALC, OnBnClickedClearcalc) END_MESSAGE_MAP() // Ctest1Dlg message handlers BOOL Ctest1Dlg::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 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 Ctest1Dlg::OnPaint() { DrawMatrix(); if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(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 function to obtain the cursor to display while the user drags // the minimized window. HCURSOR Ctest1Dlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void Ctest1Dlg::OnBnClickedOk() { OnOK(); } void Ctest1Dlg::OnBnClickedButton1() { try { m_sudoku.solve(); } catch (...) { MessageBox("This problem can not be solved"); } Invalidate(); this->UpdateWindow(); } void Ctest1Dlg::OnBnClickedClear() { m_sudoku.clear(); Invalidate(); this->UpdateWindow(); } void Ctest1Dlg::OnBnClickedClearcalc() { m_sudoku.clearCalculated(); Invalidate(); this->UpdateWindow(); } void Ctest1Dlg::OnLButtonDown(UINT nFlags, CPoint point) { int x,y; int result; // TODO: Add your message handler code here and/or call default if (point.x >25 && point.x < 290 && point.y > 25 && point.y <290) { x = (point.x-25) / 30; y = (point.y-25) / 30; EnterNumber en(0); en.DoModal(); result = en.number(); m_sudoku.setNumber(x,y,result); this->Invalidate(); this->UpdateWindow(); } CDialog::OnLButtonDown(nFlags, point); } void Ctest1Dlg::OnBnClickedAbout() { MessageBox("Sudoku Solver (C) 2006-2007 by Torben H. Nielsen"); } void Ctest1Dlg::OnOK() { // TODO: Add your specialized code here and/or call the base class CDialog::OnOK(); } void Ctest1Dlg::OnCancel() { // TODO: Add your specialized code here and/or call the base class OnOK(); } void Ctest1Dlg::DrawMatrix(void) { int i,j; int startx,starty,stopx,stopy; CPaintDC paint(this); paint.SetBkMode(TRANSPARENT); CPen thinpen(PS_SOLID, 1, RGB(0,0,0) ); CPen thickpen(PS_SOLID, 3, RGB(0,0,0) ); CBrush whitebrush(RGB(255,255,255)); RECT area; area.top = 10; area.bottom = 310; area.left = 10; area.right = 310; paint.FillRect(&area,&whitebrush); //Draw horizontal lines for(i=0; i<10; i++) { if (i%3==0) paint.SelectObject(&thickpen); else paint.SelectObject(&thinpen); startx = 15; stopx = 305; starty = stopy = (i*30) + 25; paint.MoveTo(startx,starty); paint.LineTo(stopx,stopy); } //Draw vertical lines for(i=0; i<10; i++) { if (i%3==0) paint.SelectObject(&thickpen); else paint.SelectObject(&thinpen); starty = 15; stopy = 305; startx = stopx = (i*30) + 25; paint.MoveTo(startx,starty); paint.LineTo(stopx,stopy); } paint.SelectObject(&thinpen); //Fill the matrix with the numbers for (i=0; i<9;i++) { for (j=0; j<9; j++) { if ( m_sudoku.getNumber(i,j) > 0 ) { if (m_sudoku.getUser(i,j)) paint.SetTextColor(RGB(0,0,255)); else paint.SetTextColor(RGB(0,0,0)); CString tmp; tmp.Format("%d", m_sudoku.getNumber(i,j)); paint.TextOut((i*30)+36,(j*30)+33, tmp); } } } }