C++编程逻辑讲解step by step:用MFC类库开发一个小游戏-军官能力测验

news2024/11/15 15:38:17

先给出最终效果

代码(没法一点一点讲了,太长)

checkvw.h 

checkvw.cpp

// checkvw.h : interface of the CCheckerView class
//
/

class CCheckerView : public CView
{
protected: // create from serialization only
	CCheckerView();
	DECLARE_DYNCREATE(CCheckerView)

// Attributes
public:
	CCheckerDoc* GetDocument();
	COLORREF m_background;
// Operations
public:
	void DrawCell(CDC *dc,CCheckerDoc *doc,int x,int y);

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CCheckerView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo = NULL);
	protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CCheckerView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:
    BOOL Screen2Grid(CPoint &pt);
	BOOL Grid2Screen(CPoint &pt);
	// Generated message map functions
protected:
	//{{AFX_MSG(CCheckerView)
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnStatus();
	afx_msg void OnBackground();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in checkvw.cpp
inline CCheckerDoc* CCheckerView::GetDocument()
   { return (CCheckerDoc*)m_pDocument; }
#endif

/



// checkvw.cpp : implementation of the CCheckerView class
//

#include "stdafx.h"
#include "checker.h"

#include "checkdoc.h"
#include "checkvw.h"
#include "actdlg.h"
#include "statuspg.h"
#include "customco.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/
// CCheckerView

IMPLEMENT_DYNCREATE(CCheckerView, CView)

BEGIN_MESSAGE_MAP(CCheckerView, CView)
        //{{AFX_MSG_MAP(CCheckerView)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(IDM_STATUS, OnStatus)
	ON_COMMAND(IDM_BACKGROUND, OnBackground)
	//}}AFX_MSG_MAP
        // Standard printing commands
        ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/
// CCheckerView construction/destruction

CCheckerView::CCheckerView()
{
       m_background=RGB(0x80,0x80,0x80);

}

CCheckerView::~CCheckerView()
{
}

/
// CCheckerView drawing


void CCheckerView::DrawCell(CDC *dc,CCheckerDoc *doc,int x,int y)
  {
  CBrush white(RGB(0xFF,0xFF,0xFF)),black(RGB(0,0,0)),red(RGB(0xFF,0,0));
  CBrush grey(m_background),cyan(RGB(0,0xff,0xff));
  CBrush *old,*circ=NULL;
  CPoint sel;
  STATE state=doc->CellState(x,y);
  old=dc->SelectObject(x%2==y%2?&white:&grey);
  if (doc->GetSelect(sel)&&sel.x==x&&sel.y==y)
    dc->SelectObject(&cyan);   // draw selection if appropriate
  dc->Rectangle(x*75,y*50,(x+1)*75,(y+1)*50);
  switch(state)
    {
	case CELL_RED:
	case CELL_REDKING:
	  circ=&red;
	  break;
	case CELL_BLACK:
	case CELL_BLACKKING:
	  circ=&black;
	  break;
	}
  if (circ)
    {
	dc->SelectObject(circ);
	dc->Ellipse(x*75+5,y*50+5,(x+1)*75-5,(y+1)*50-5);
	}
  if (state==CELL_REDKING||state==CELL_BLACKKING)
    {
	CPen *oldpen=(CPen *)dc->SelectStockObject(WHITE_PEN);
	dc->Ellipse(x*75+10,y*50+10,(x+1)*75-10,(y+1)*50-10);
	dc->SelectObject(oldpen);
	}
  dc->SelectObject(old);
  }

void CCheckerView::OnDraw(CDC* pDC)
{
        CCheckerDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        int i,j;
        for (i=0;i<8;i++)
          for (j=0;j<8;j++)
            DrawCell(pDC,pDoc,j,i);


}

/
// CCheckerView printing

BOOL CCheckerView::OnPreparePrinting(CPrintInfo* pInfo)
{
        // default preparation
        return DoPreparePrinting(pInfo);
}

void CCheckerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
        // TODO: add extra initialization before printing
}

void CCheckerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
        // TODO: add cleanup after printing
}

/
// CCheckerView diagnostics

#ifdef _DEBUG
void CCheckerView::AssertValid() const
{
        CView::AssertValid();
}

void CCheckerView::Dump(CDumpContext& dc) const
{
        CView::Dump(dc);
}

CCheckerDoc* CCheckerView::GetDocument() // non-debug version is inline
{
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCheckerDoc)));
        return (CCheckerDoc*)m_pDocument;
}
#endif //_DEBUG

BOOL CCheckerView::Screen2Grid(CPoint &pt)
  {
  pt.x/=75;
  pt.y/=50;
  return TRUE;
  }

BOOL CCheckerView::Grid2Screen(CPoint &pt)
  {
  pt.x*=75;
  pt.y*=50;
  return TRUE;
  }

/
// CCheckerView message handlers



void CCheckerView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CCheckerDoc *doc=GetDocument();
	CPoint select;
	Screen2Grid(point);
	if (doc->GetSelect(select))
	  {
	  if (select!=point)
	    {
		doc->movenum++;
	    doc->SetCellState(point.x,point.y,doc->CellState(select.x,select.y));
	    doc->SetCellState(select.x,select.y,CELL_EMPTY);		
		}
	  doc->ClrSelect();
	  }
	else
	  if (doc->CellState(point.x,point.y)!=CELL_EMPTY) 
	    doc->SetSelect(point);
	CView::OnLButtonDown(nFlags, point);
}

void CCheckerView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CCheckerDoc *doc=GetDocument();
	Screen2Grid(point);
	STATE state=doc->CellState(point.x,point.y);
	if (state==CELL_EMPTY) return;
	ActionDialog action;
	action.m_movenum=doc->movenum;
	action.m_action=0;
	
	if (action.DoModal()==IDOK)
	  {
	  doc->movenum=action.m_movenum;
	  switch (action.m_action)
	    {
		case 0:
		  doc->SetCellState(point.x,point.y,CELL_EMPTY);
          doc->ClrSelect();
		  break;

		case 1:
		  if (state==CELL_RED||state==CELL_BLACK)
		    doc->SetCellState(point.x,point.y,(STATE)(state+2));
		  break;
		}
	  doc->ClrSelect();
	  doc->UpdateAllViews(NULL,(LPARAM)&point);
      }
	  CView::OnRButtonDown(nFlags, point);
}

void CCheckerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (!lHint)
	  {
	  CView::OnUpdate(pSender,lHint,pHint);	  
	  return;
	  }
	CPoint pt=*(CPoint *)lHint;
	CSize sz(75,50);
	Grid2Screen(pt);
	CRect r(pt,sz);
	InvalidateRect(&r);
}

void CCheckerView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	if (pDC->IsPrinting())
	  {
	  pDC->SetMapMode(MM_ANISOTROPIC);
	  pDC->ScaleViewportExt(2,1,2,1);  // double size
	  }
	CView::OnPrepareDC(pDC, pInfo);
}

void CCheckerView::OnStatus() 
{
    CCheckerDoc *doc=GetDocument();
	CPropertySheet sheet("Game Status",this);
	StatusPage redpg(IDS_RED), blackpg(IDS_BLACK);
	// Init page data
	redpg.m_pieces.Format("%d",doc->Count(CELL_RED));
	redpg.m_kings.Format("%d",doc->Count(CELL_REDKING));
	blackpg.m_pieces.Format("%d",doc->Count(CELL_BLACK));
	blackpg.m_kings.Format("%d",doc->Count(CELL_BLACKKING));
	sheet.AddPage(&redpg);
	sheet.AddPage(&blackpg);
	sheet.DoModal();
		
}

void CCheckerView::OnBackground() 
{
	CCustomColor dlg(m_background,0,this);
	if (dlg.DoModal()==IDOK)
	  {
	  if (dlg.m_reset)
	    m_background=RGB(0x80,0x80,0x80);
	  else
	    m_background=dlg.GetColor();
	  InvalidateRect(NULL);
	  }
}
/
// CAboutDlg dialog used for App About

class CCircButton : public CButton
{
public:
  void DrawItem(LPDRAWITEMSTRUCT di);
};

void CCircButton::DrawItem(LPDRAWITEMSTRUCT di)
  {
  CString title;
  int oldbk; 
  // Could set background/foregroud colors but Windows
  // sets reasonable defaults for you
  CDC *dc=CDC::FromHandle(di->hDC);
  dc->Ellipse(&di->rcItem);
  GetWindowText(title);
  oldbk=dc->SetBkMode(TRANSPARENT);
  dc->DrawText(title,-1,&di->rcItem,DT_CENTER|DT_SINGLELINE|DT_VCENTER);
  dc->SetBkMode(oldbk);
  }
class CCustomColor : public CColorDialog
{
// Construction
public:
	CCustomColor(COLORREF color=0,DWORD flag=0,CWnd* pParent = NULL);   // standard constructor
	
	BOOL m_reset;
// Dialog Data
	//{{AFX_DATA(CCustomColor)
	enum { IDD = IDD_COLOR };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA


// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CCustomColor)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:

	// Generated message map functions
	//{{AFX_MSG(CCustomColor)
	afx_msg void OnReset();
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};
// Nested splitter class -- Williams

class CNestSplit : public CSplitterWnd
  {
  protected:
  BOOL SplitRow(int cybefore);
  BOOL SplitColumn(int cxbefore);
  private:
  void SetMeActive(void);
  };
/* Stateary.h */
#include <afxtempl.h>

enum STATE { CELL_EMPTY=0, CELL_RED, CELL_BLACK, CELL_REDKING, CELL_BLACKKING }; 

typedef CArray<STATE,STATE> CStateArray;

class CStateArray2 : public CStateArray
  {
  protected:
  int maxx,maxy;
  public:
  CStateArray2(int mx,int my) { maxx=mx; maxy=my; SetSize(mx*my); };
  STATE GetAt(int x,int y) { return CStateArray::GetAt(x*maxx+y); };
  void SetAt(int x,int y,STATE v) { CStateArray::SetAt(x*maxx+y,v); };
  void Serialize(CArchive &ar);
  };
// textdoc.h : header file
//

/
// CTextDoc document

class CTextDoc : public CDocument
{
protected:
	CTextDoc();           // protected constructor used by dynamic creation
	DECLARE_DYNCREATE(CTextDoc)

// Attributes
public:

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CTextDoc)
	protected:
	virtual BOOL OnNewDocument();
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CTextDoc();
	virtual void Serialize(CArchive& ar);   // overridden for document i/o
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

	// Generated message map functions
protected:
	//{{AFX_MSG(CTextDoc)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};
// debugvw.h : header file
//

/
// DebugView view

class DebugView : public CScrollView
{
public:
	DebugView();           // class wizard makes this protected but I want pubic!
protected:
	DECLARE_DYNCREATE(DebugView)

// Attributes
public:

protected:
    int m_linehi;

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(DebugView)
	protected:
	virtual void OnDraw(CDC* pDC);      // overridden to draw this view
	virtual void OnInitialUpdate();     // first time after construct
	//}}AFX_VIRTUAL

// Implementation
protected:
	virtual ~DebugView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

	// Generated message map functions
	//{{AFX_MSG(DebugView)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/
// statuspg.h : header file
//

/
// StatusPage dialog

class StatusPage : public CPropertyPage
{
	DECLARE_DYNCREATE(StatusPage)
	// only for serialization
    StatusPage() : CPropertyPage(StatusPage::IDD) {};
// Construction
public:
	StatusPage(UINT title);
	~StatusPage();

// Dialog Data
	//{{AFX_DATA(StatusPage)
	enum { IDD = IDD_STATUS };
	CString	m_kings;
	CString	m_pieces;
	//}}AFX_DATA


// Overrides
	// ClassWizard generate virtual function overrides
	//{{AFX_VIRTUAL(StatusPage)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	// Generated message map functions
	//{{AFX_MSG(StatusPage)
		// NOTE: the ClassWizard will add member functions here
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()

};
// actdlgh : header file
//

/
// ActionDialog dialog

class ActionDialog : public CDialog
{
// Construction
public:
        ActionDialog(CWnd* pParent = NULL);   // standard constructor

// Dialog Data
        //{{AFX_DATA(ActionDialog)
	enum { IDD = IDD_ACTIONDLG };
        UINT    m_movenum;
	int		m_action;
	//}}AFX_DATA
       


// Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(ActionDialog)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        //}}AFX_VIRTUAL

// Implementation
protected:

        // Generated message map functions
        //{{AFX_MSG(ActionDialog)
        virtual void OnOK();
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
};
class CAboutDlg : public CDialog
{
public:
        CAboutDlg();

// Dialog Data
        //{{AFX_DATA(CAboutDlg)
        enum { IDD = IDD_ABOUTBOX };
        //}}AFX_DATA

// Implementation
protected:
	    CCircButton okbutton;
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
        //{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	//}}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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CCheckerApp::OnAppAbout()
{
        CAboutDlg aboutDlg;
        aboutDlg.DoModal();
}

/
// CCheckerApp commands

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	okbutton.SubclassDlgItem(IDOK,this);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
// checker.h : main header file for the CHECKER1 application
//

#ifndef __AFXWIN_H__
        #error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"       // main symbols

/
// CCheckerApp:
// See checker.cpp for the implementation of this class
//

class CCheckerApp : public CWinApp
{
public:
        CCheckerApp();

// Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CCheckerApp)
        public:
        virtual BOOL InitInstance();
        //}}AFX_VIRTUAL

// Implementation

        //{{AFX_MSG(CCheckerApp)
        afx_msg void OnAppAbout();
                // NOTE - the ClassWizard will add and remove member functions here.
                //    DO NOT EDIT what you see in these blocks of generated code !
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
};


/
// checkdoc.h : interface of the CCheckerDoc class
//
/



#include "stateary.h"

class CCheckerDoc : public CDocument
{
protected: // create from serialization only
	CCheckerDoc();
	DECLARE_DYNCREATE(CCheckerDoc)
	CStateArray2 m_statearray;
	CPoint m_selected;
	BOOL m_selectflag;
	unsigned movenum; // move number
// Attributes
public:
   BOOL GetSelect(CPoint &pt) { pt=m_selected; return m_selectflag; };
   void SetSelect(CPoint pt) { m_selected=pt; m_selectflag=TRUE; UpdateAllViews(NULL,(LPARAM)&pt); };
   void ClrSelect(void) { if (m_selectflag) m_selectflag=FALSE; UpdateAllViews(NULL,(LPARAM)&m_selected); };
   int Count(STATE typ);
// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CCheckerDoc)
	public:
	virtual BOOL OnNewDocument();
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CCheckerDoc();
	virtual void Serialize(CArchive& ar);   // overridden for document i/o
	STATE CellState(int,int);
	void SetCellState(int x,int y,STATE s);
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CCheckerDoc)
	afx_msg void OnDebugwin();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/
// checkdoc.cpp : implementation of the CCheckerDoc class
//

#include "stdafx.h"
#include "checker.h"


#include "checkdoc.h"
#include "debugvw.h"


#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/
// CCheckerDoc

IMPLEMENT_DYNCREATE(CCheckerDoc, CDocument)

BEGIN_MESSAGE_MAP(CCheckerDoc, CDocument)
        //{{AFX_MSG_MAP(CCheckerDoc)
	ON_COMMAND(IDM_DEBUGWIN, OnDebugwin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CCheckerDoc construction/destruction

CCheckerDoc::CCheckerDoc() : m_statearray(8,8)
{
	    int x,y;
		m_selectflag=FALSE;
		for (y=0;y<3;y++)
		  for (x=!(y%2);x<8;x+=2)
		     m_statearray.SetAt(x,y,CELL_BLACK);
	 	for (y=5;y<8;y++)
		  for (x=!(y%2);x<8;x+=2)
        	m_statearray.SetAt(x,y,CELL_RED);
		movenum=0;
}

CCheckerDoc::~CCheckerDoc()
{
}

BOOL CCheckerDoc::OnNewDocument()
{
        if (!CDocument::OnNewDocument())
                return FALSE;

        // TODO: add reinitialization code here
        // (SDI documents will reuse this document)

        return TRUE;
}

/
// CCheckerDoc serialization

void CCheckerDoc::Serialize(CArchive& ar)
{
       if (ar.IsStoring())
	     ar<<(DWORD)movenum;
	   else
	     {
		 DWORD move;
		 ar>>move;
		 movenum=move;
		 }
       m_statearray.Serialize(ar);
	   
}

/
// CCheckerDoc diagnostics

#ifdef _DEBUG
void CCheckerDoc::AssertValid() const
{
        CDocument::AssertValid();
}

void CCheckerDoc::Dump(CDumpContext& dc) const
{
        CDocument::Dump(dc);
}
#endif //_DEBUG

/
// CCheckerDoc commands

STATE CCheckerDoc::CellState(int x,int y)
  {
  return m_statearray.GetAt(x,y);
  }			   

void CCheckerDoc::SetCellState(int x,int y,STATE s)
  {
  CPoint pt(x,y);
  m_statearray.SetAt(x,y,s);
  UpdateAllViews(NULL,(LPARAM)&pt);
  SetModifiedFlag();
  }

// Count cells -- if typ==CELL_RED count CELL_RED and CELL_REDKING
// if typ==CELL_BLACK count CELL_BLACK and CELL_BLACKKING 
int CCheckerDoc::Count(STATE typ)
  {
  int i,max=m_statearray.GetUpperBound(),ct=0;
  if (max==-1) return 0;
  for (i=0;i<=max;i++)
    {
	STATE s=m_statearray[i];
	if (typ==s) ct++;
	if (typ==CELL_RED && s==CELL_REDKING) ct++;
	if (typ==CELL_BLACK && s==CELL_BLACKKING) ct++;
	} 
  return ct;
  }


void CCheckerDoc::OnDebugwin() 
{
#if 1  // Easy way to make a new view -- use doc templates
    CMultiDocTemplate temptemplate(IDR_DEBUGTYPE,RUNTIME_CLASS(CCheckerDoc),
      RUNTIME_CLASS(CMDIChildWnd),RUNTIME_CLASS(DebugView));
	CFrameWnd *frame=temptemplate.CreateNewFrame(this,NULL);
	if (!frame)
	  AfxMessageBox("Can't create debug window");
	else
	  frame->InitialUpdateFrame(this,TRUE);
#else // hard way -- do all the work yourself
		CMDIChildWnd *frame=new CMDIChildWnd;
	if (!frame->Create(NULL,"Debug output: "+GetTitle()))
	  {
	  AfxMessageBox("Can't create debug window frame");
	  return;
	  }

	DebugView *view=new DebugView;
	// Don't care about size -- RecalcLayout will resize anyway.
	if (!view->Create(NULL,NULL,AFX_WS_DEFAULT_VIEW,CRect(0,0,0,0),frame,AFX_IDW_PANE_FIRST))
	  {
	  AfxMessageBox("Can't create debug window view");
	  frame->DestroyWindow();
	  }
	// add view to document (this)
    AddView(view);
	// Make sure OnInitialUpdate fires
	frame->InitialUpdateFrame(this,TRUE);
	// Layout frame
	frame->RecalcLayout();
#endif
}
// checkvw.cpp : implementation of the CCheckerView class
//

#include "stdafx.h"
#include "checker.h"

#include "checkdoc.h"
#include "checkvw.h"
#include "actdlg.h"
#include "statuspg.h"
#include "customco.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/
// CCheckerView

IMPLEMENT_DYNCREATE(CCheckerView, CView)

BEGIN_MESSAGE_MAP(CCheckerView, CView)
        //{{AFX_MSG_MAP(CCheckerView)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(IDM_STATUS, OnStatus)
	ON_COMMAND(IDM_BACKGROUND, OnBackground)
	//}}AFX_MSG_MAP
        // Standard printing commands
        ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/
// CCheckerView construction/destruction

CCheckerView::CCheckerView()
{
       m_background=RGB(0x80,0x80,0x80);

}

CCheckerView::~CCheckerView()
{
}

/
// CCheckerView drawing


void CCheckerView::DrawCell(CDC *dc,CCheckerDoc *doc,int x,int y)
  {
  CBrush white(RGB(0xFF,0xFF,0xFF)),black(RGB(0,0,0)),red(RGB(0xFF,0,0));
  CBrush grey(m_background),cyan(RGB(0,0xff,0xff));
  CBrush *old,*circ=NULL;
  CPoint sel;
  STATE state=doc->CellState(x,y);
  old=dc->SelectObject(x%2==y%2?&white:&grey);
  if (doc->GetSelect(sel)&&sel.x==x&&sel.y==y)
    dc->SelectObject(&cyan);   // draw selection if appropriate
  dc->Rectangle(x*75,y*50,(x+1)*75,(y+1)*50);
  switch(state)
    {
	case CELL_RED:
	case CELL_REDKING:
	  circ=&red;
	  break;
	case CELL_BLACK:
	case CELL_BLACKKING:
	  circ=&black;
	  break;
	}
  if (circ)
    {
	dc->SelectObject(circ);
	dc->Ellipse(x*75+5,y*50+5,(x+1)*75-5,(y+1)*50-5);
	}
  if (state==CELL_REDKING||state==CELL_BLACKKING)
    {
	CPen *oldpen=(CPen *)dc->SelectStockObject(WHITE_PEN);
	dc->Ellipse(x*75+10,y*50+10,(x+1)*75-10,(y+1)*50-10);
	dc->SelectObject(oldpen);
	}
  dc->SelectObject(old);
  }

void CCheckerView::OnDraw(CDC* pDC)
{
        CCheckerDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        int i,j;
        for (i=0;i<8;i++)
          for (j=0;j<8;j++)
            DrawCell(pDC,pDoc,j,i);


}

/
// CCheckerView printing

BOOL CCheckerView::OnPreparePrinting(CPrintInfo* pInfo)
{
        // default preparation
        return DoPreparePrinting(pInfo);
}

void CCheckerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
        // TODO: add extra initialization before printing
}

void CCheckerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
        // TODO: add cleanup after printing
}

/
// CCheckerView diagnostics

#ifdef _DEBUG
void CCheckerView::AssertValid() const
{
        CView::AssertValid();
}

void CCheckerView::Dump(CDumpContext& dc) const
{
        CView::Dump(dc);
}

CCheckerDoc* CCheckerView::GetDocument() // non-debug version is inline
{
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCheckerDoc)));
        return (CCheckerDoc*)m_pDocument;
}
#endif //_DEBUG

BOOL CCheckerView::Screen2Grid(CPoint &pt)
  {
  pt.x/=75;
  pt.y/=50;
  return TRUE;
  }

BOOL CCheckerView::Grid2Screen(CPoint &pt)
  {
  pt.x*=75;
  pt.y*=50;
  return TRUE;
  }

/
// CCheckerView message handlers



void CCheckerView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CCheckerDoc *doc=GetDocument();
	CPoint select;
	Screen2Grid(point);
	if (doc->GetSelect(select))
	  {
	  if (select!=point)
	    {
		doc->movenum++;
	    doc->SetCellState(point.x,point.y,doc->CellState(select.x,select.y));
	    doc->SetCellState(select.x,select.y,CELL_EMPTY);		
		}
	  doc->ClrSelect();
	  }
	else
	  if (doc->CellState(point.x,point.y)!=CELL_EMPTY) 
	    doc->SetSelect(point);
	CView::OnLButtonDown(nFlags, point);
}

void CCheckerView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	CCheckerDoc *doc=GetDocument();
	Screen2Grid(point);
	STATE state=doc->CellState(point.x,point.y);
	if (state==CELL_EMPTY) return;
	ActionDialog action;
	action.m_movenum=doc->movenum;
	action.m_action=0;
	
	if (action.DoModal()==IDOK)
	  {
	  doc->movenum=action.m_movenum;
	  switch (action.m_action)
	    {
		case 0:
		  doc->SetCellState(point.x,point.y,CELL_EMPTY);
          doc->ClrSelect();
		  break;

		case 1:
		  if (state==CELL_RED||state==CELL_BLACK)
		    doc->SetCellState(point.x,point.y,(STATE)(state+2));
		  break;
		}
	  doc->ClrSelect();
	  doc->UpdateAllViews(NULL,(LPARAM)&point);
      }
	  CView::OnRButtonDown(nFlags, point);
}

void CCheckerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	if (!lHint)
	  {
	  CView::OnUpdate(pSender,lHint,pHint);	  
	  return;
	  }
	CPoint pt=*(CPoint *)lHint;
	CSize sz(75,50);
	Grid2Screen(pt);
	CRect r(pt,sz);
	InvalidateRect(&r);
}

void CCheckerView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
{
	if (pDC->IsPrinting())
	  {
	  pDC->SetMapMode(MM_ANISOTROPIC);
	  pDC->ScaleViewportExt(2,1,2,1);  // double size
	  }
	CView::OnPrepareDC(pDC, pInfo);
}

void CCheckerView::OnStatus() 
{
    CCheckerDoc *doc=GetDocument();
	CPropertySheet sheet("Game Status",this);
	StatusPage redpg(IDS_RED), blackpg(IDS_BLACK);
	// Init page data
	redpg.m_pieces.Format("%d",doc->Count(CELL_RED));
	redpg.m_kings.Format("%d",doc->Count(CELL_REDKING));
	blackpg.m_pieces.Format("%d",doc->Count(CELL_BLACK));
	blackpg.m_kings.Format("%d",doc->Count(CELL_BLACKKING));
	sheet.AddPage(&redpg);
	sheet.AddPage(&blackpg);
	sheet.DoModal();
		
}

void CCheckerView::OnBackground() 
{
	CCustomColor dlg(m_background,0,this);
	if (dlg.DoModal()==IDOK)
	  {
	  if (dlg.m_reset)
	    m_background=RGB(0x80,0x80,0x80);
	  else
	    m_background=dlg.GetColor();
	  InvalidateRect(NULL);
	  }
}
// checkfrm.cpp : implementation file
//

#include "stdafx.h"
#include "checker.h"
#include "checkfrm.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/
// CCheckFrame

IMPLEMENT_DYNCREATE(CCheckFrame, CMDIChildWnd)

CCheckFrame::CCheckFrame()
{
}

CCheckFrame::~CCheckFrame()
{
}


BEGIN_MESSAGE_MAP(CCheckFrame, CMDIChildWnd)
	//{{AFX_MSG_MAP(CCheckFrame)
	ON_WM_GETMINMAXINFO()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/
// CCheckFrame message handlers

void CCheckFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) 
{
	CRect sz(0,0,601,401);
	DWORD styles,exstyles;
	styles=::GetWindowLong(m_hWnd,GWL_STYLE);
	exstyles=::GetWindowLong(m_hWnd,GWL_EXSTYLE);
	// Leave room for view's border
	sz.InflateRect(::GetSystemMetrics(SM_CXBORDER),::GetSystemMetrics(SM_CYBORDER));
	::AdjustWindowRectEx(&sz,styles,FALSE,exstyles);
    CPoint size(sz.Width(),sz.Height());
	lpMMI->ptMaxTrackSize=lpMMI->ptMinTrackSize=lpMMI->ptMaxSize=size;
	lpMMI->ptMaxPosition=CPoint(0,0);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1940052.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

掌握这4种翻译方式,阅读外语文件不再困难

如果你作为学生需要学习或者研究外国文件&#xff0c;或者出国旅游前也需要了解一些外国文件。如果掌握文件翻译工具&#xff0c;那这些问题就不是问题啦。这里我给你介绍几个效果不错的文件翻译工具吧。 1.福.昕文献翻译网站 这个工具只要在线就能使用&#xff0c;而且在线丝…

腾讯技术创作特训营 -- SUPERWINNIE -- AI重塑社交内容

目录 1 什么是AI社交内容 2 案例拆解 3 用LLM做爆文选题 4 用LLM出爆文脚本提示词 1 什么是AI社交内容 任何一个因素被AI取代都是AI社交内容 2 案例拆解 数字人 资讯素材 录屏产品的素材&#xff08;小红书测试AI产品&#xff09; 脚本 素材 剪辑 3 用LLM做爆文选题 &…

突破•指针二

听说这是目录哦 复习review❤️野指针&#x1fae7;assert断言&#x1fae7;assert的神奇之处 指针的使用和传址调用&#x1fae7;数组名的理解&#x1fae7;理解整个数组和数组首元素地址的区别 使用指针访问数组&#x1fae7;一维数组传参的本质&#x1fae7;二级指针&#x…

mq基础入门

前言 黑马商城导入了mq依赖 但是没有改service发消息 因为下单业务一直有问题 所以先没改 作业时间不够也没处理 1.异步调用 就是所谓的发短信 可以不用立即恢复 比如下单业务 下了单更新信息 就相当于发个消息通知一下 不用立即更改 但是支付就比较重要 不需要因为故障导…

数据结构——队列(顺序结构)

一、队列的定义 队列是一种线性数据结构,具有先进先出(FIFO)的特性。它可以理解为排队的队伍,先到先得,后到后得。队列有两个基本操作:入队(enqueue)和出队(dequeue)。入队在队列的末尾插入新元素,出队则是从队列的头部移除元素。这种数据结构常用于需要按照顺序处…

C语言中的运算符(二)

文章目录 &#x1f34a;自我介绍&#x1f34a;位运算符&#x1f34a;赋值复合运算符&#x1f34a;逗号运算符和赋值运算符&#x1f34a;运算符优先级 你的点赞评论就是对博主最大的鼓励 当然喜欢的小伙伴可以&#xff1a;点赞关注评论收藏&#xff08;一键四连&#xff09;哦~ …

Qt开发网络嗅探器03

数据包分析 想要知道如何解析IP数据包&#xff0c;就要知道不同的IP数据包的包头结构&#xff0c;于是我们上⽹查查资料&#xff1a; 以太网数据包 ARP数据包 IPv4 IPv6 TCP UDP ICMP ICMPv6 根据以上数据包头结构&#xff0c;我们就有了我们的protocol.h文件&#xff0c;声明…

HTML+CSS+JS精美气泡提示框

源代码在效果图后面 点赞❤️关注&#x1f49b;收藏⭐️ 主要实现&#xff1a;提示框出现和消失两种丝滑动画 弹出气泡提示框后延迟2秒自动消失 效果图 错误框 正确 警告 提示 源代码 <!DOCTYPE html> <html lang"en"> <head><meta cha…

聚焦IOC容器刷新环节prepareBeanFactory专项

目录 一、IOC容器的刷新环节快速回顾 二、prepareBeanFactory源码展示分析 三、设置基本属性深入分析 &#xff08;一&#xff09;设置类加载器 &#xff08;二&#xff09;设置表达式解析器 &#xff08;三&#xff09;设置属性编辑器 &#xff08;四&#xff09;设计目…

快速排序、快速选择算法、找最近公共祖先

快速排序&#xff08;用i和j双指针&#xff0c;左部分值小&#xff0c;当ij时&#xff0c;两部分按基准值已经排序好&#xff0c;将基准值放到j即可。 class Solution {public int[] sortArray(int[] nums) {sort(nums,0,nums.length-1);return nums;}void sort(int[] nums,int…

初阶数据结构的实现1 顺序表和链表

顺序表和链表 1.线性表1.1顺序表1.1.1静态顺序表&#xff08;不去实现&#xff09;1.1.2动态顺序表1.1.2.1 定义程序目标1.1.2.2 设计程序1.1.2.3编写代码1.1.2.3测试和调试代码 1.1.2 顺序表的问题与思考 1.2链表1.2.1链表的概念及结构1.2.1.1 定义程序目标1.2.1.2 设计程序1.…

51单片机嵌入式开发:15、STC89C52RC操作蜂鸣器实现一个music音乐播放器的音乐盒

STC89C52RC操作蜂鸣器实现一个music音乐播放器的音乐盒 1 概述2 蜂鸣器操作方法3 蜂鸣器发出音声4 硬件电路5 软件实现6 整体工程&#xff1a;7 总结 1 概述 要实现一个基于STC89C52RC单片机的音乐盒&#xff0c;可以按照以下步骤进行&#xff1a; &#xff08;1&#xff09;硬…

Golang | Leetcode Golang题解之第274题H指数

题目&#xff1a; 题解&#xff1a; func hIndex(citations []int) int {// 答案最多只能到数组长度left,right:0,len(citations)var mid intfor left<right{// 1 防止死循环mid(leftright1)>>1cnt:0for _,v:range citations{if v>mid{cnt}}if cnt>mid{// 要找…

ISO 14001:企业环境管理的黄金标准

在全球可持续发展潮流中&#xff0c;企业的环境责任愈发重要。ISO 14001环境管理体系&#xff0c;为各类企业提供了一条优化环境绩效的明路。无论企业规模大小&#xff0c;业务类型如何&#xff0c;ISO 14001都能帮助企业有效管理环境影响&#xff0c;实现绿色发展。 ISO 14001…

QT样式美化 之 qss入门

样例一 *{font-size:13px;color:white;font-family:"宋体"; }CallWidget QLineEdit#telEdt {font-size:24px;}QMainWindow,QDialog{background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #1B2534, stop: 0.4 #010101,stop: 0.5 #000101, stop: 1.0 #1F2B…

【LeetCode】day17:654 - 最大二叉树, 617 - 合并二叉树, 700 - 二叉树搜索树中的搜索, 98 - 验证二叉搜索树

LeetCode 代码随想录跟练 Day17 654.最大二叉树617.合并二叉树700.二叉搜索树中的搜索98.验证二叉搜索树 654.最大二叉树 题目描述&#xff1a; 给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建: 创建一个根节点&#xff0c;其值为 nums 中的…

PyCharm创建一个空的python项目

1.设置项目路径 2.配置python解释器 右下角可以选择always

提交(git-add git-commit git-push)

当修改好一个功能的时候&#xff0c;可以提交到远程仓库&#xff0c;这样就等于更新了一次版本&#xff0c;那么下次改修了文件的话&#xff0c;是跟这个版本做对比的 git status&#xff0c; 查看文件修改情况&#xff0c;git add 假如你只想提交1个文件&#xff0c;那么直接…

Python入门基础教程(非常详细)

现在找工作真的越来越难了&#xff01;今年更是难上加难 前几天在网上刷到这样一条热搜&#xff1a; #23岁找工作因年龄大被HR拒绝了# 是这个世界疯了还是我疯了&#xff1f; 合着只想要有20年以上工作经验的应届毕业生是吧 这好像就是现在的就业市场现状&#xff1a;“35岁…

【leetcode】二分查找本质

标题&#xff1a;【leetcode】二分查找本质 水墨不写bug 正文开始&#xff1a;&#xff08;点击题目标题转跳到OJ&#xff09; 目录 &#xff08;O&#xff09;前言* &#xff08;一&#xff09; 在排序数组中查找元素的第一个和最后一个位置 思路详解&#xff1a; 参考代…