?? jlview.cpp
字號:
// JLView.cpp : implementation of the CJLView class
//
#include "stdafx.h"
#include "JL.h"
#include "JLDoc.h"
#include "JLView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CJLView
IMPLEMENT_DYNCREATE(CJLView, CView)
BEGIN_MESSAGE_MAP(CJLView, CView)
//{{AFX_MSG_MAP(CJLView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONUP()
ON_COMMAND(IDM_BK_COLOR, OnBkColor)
ON_COMMAND(IDM_CARD_COLOR, OnCardColor)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CJLView construction/destruction
CJLView::CJLView()
{
// TODO: add construction code here
CWinApp *pApp = AfxGetApp();
m_hDown = pApp->LoadCursor(IDCSR_DOWN_ARROW);
m_hIcon = pApp->LoadIcon(IDR_MAINFRAME);
m_brushBk.CreateSolidBrush(RGB(255,255,255));//card bk
m_brushBkgnd.CreateSolidBrush(RGB(0,128,0));//client bk
//裝載位圖資源
{
//壓縮后的位圖文件被作為自定義資源,這里先裝載資源
HRSRC handle = FindResource(NULL,"IDR_BIN_ZIP_BMP","BIN");
HGLOBAL hGlobal = LoadResource(NULL,handle);
BYTE * pByte = (BYTE *) LockResource(hGlobal);
DWORD size = SizeofResource(NULL,handle);
CMemFile srcFile;
srcFile.Attach(pByte,size,0);
//準(zhǔn)備將壓縮的位圖解碼到臨時文件
char path[MAX_PATH],name[MAX_PATH];
DWORD len = GetTempPath(MAX_PATH,path);
if(len == 0 || len >= MAX_PATH || GetTempFileName(path,"bmp",0,name) == 0) {
MessageBox("無法創(chuàng)建臨時文件!",NULL,MB_ICONERROR | MB_OK);
}
CFile desFile(name,modeCrWr);
//解碼
Decoding(desFile,srcFile);
srcFile.Detach();
//裝載位圖資源之前要保證位圖文件已經(jīng)關(guān)閉
desFile.Flush();//這里兩步是必不可少的步驟!
desFile.Close();
HANDLE bmp = ::LoadImage(pApp->m_hInstance,name,IMAGE_BITMAP,596,86,LR_LOADFROMFILE);
m_AllBmps.Attach(bmp);
//刪除臨時位圖文件
CFile::Remove(name);
}
m_nCardLabelHit = 0;
}
//draw all things in memory dc!!!!!!!!!!!!!
//所有的繪牌動作必須都在內(nèi)存中進行否則會造成閃動
void CJLView::DrawCard(CPoint point,UINT card, CDC *pDC)
{
CJLDoc* pDoc = GetDocument();
ASSERT(pDoc->IsCard(card));
CPen penBlack(PS_SOLID,1,RGB(0,0,0));
CPen *oldPen = pDC->SelectObject(&penBlack);
CBrush *oldBrush = pDC->SelectObject(&m_brushBk);
CRect r(point,CSize(CARD_WID,CARD_HEI));
pDC->RoundRect(r,CPoint(5,5));//畫牌的背景與邊框
pDC->SelectObject(oldPen);
pDC->SelectObject(oldBrush);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap *poldbmp = memDC.SelectObject(&m_AllBmps);
//畫左上角 & 右下角數(shù)字(8*12)和數(shù)字下/上面的圖標(biāo)(8*8)
int type = TYPE(card), num = NUM(card);
int idxBS = type%2*26, idxUL = idxBS + num - 1, idxBR = idxBS + 26 - num;
pDC->BitBlt(r.left+3 , r.top+3 , 8 , 12 , &memDC , idxUL*8+32 , 0 , SRCAND);
pDC->BitBlt(r.right-3-8 , r.bottom-3-12 , 8 , 12 , &memDC , idxBR*8+32 , 0 , SRCAND);
pDC->BitBlt(r.left+3 , r.top+3+12+1, 8, 8, &memDC , clr[SML][type][0][0], clr[SML][type][0][1] , SRCAND);
pDC->BitBlt(r.right-3-8 , r.bottom-3-12-1-8 , 8, 8, &memDC , clr[SML][type][1][0], clr[SML][type][1][1] , SRCAND);
//在牌的主要部分畫花色
const UCHAR *p,*data[] = { cA , c2 , c3 , c4 , c5 , c6 , c7 , c8 , c9 , c10 };
if(num <= 10) {
p = ( card == CARD(8,3) ? c8FK : data[num-1] );
UINT n = num*3;
for(UINT j = 0; j < n ; j += 3) {
pDC->BitBlt(point.x + p[j] - 8, point.y + p[j+1] - 8, 16, 16, &memDC, clr[BIG][type][!p[j+2]][0], clr[BIG][type][!p[j+2]][1], SRCAND);
}
} else { //畫J Q K
CPoint p = r.CenterPoint();
pDC->BitBlt(p.x-23, p.y-37, 47, 74, &memDC, ( (num-11)*4 + type )*47 + 32, 12, SRCAND);
}
memDC.SelectObject(poldbmp);
}
void CJLView::OnDraw(CDC* pDC)
{
CJLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//顯示步數(shù)信息
CString strSteps;
CRect r = pDoc->RectOfStep();
if(!pDoc->m_pOps->IsEmpty()) {
strSteps.Format("%d", pDoc->m_pOps->GetCount());
CFont font;
font.CreatePointFont(stepFont * 10, "Arial", pDC);
CFont * prevFont = pDC->SelectObject(&font);
int prevMode = pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(strSteps,r, DT_CENTER);
pDC->SetBkMode(prevMode);
pDC->SelectObject(prevFont);
}
//在步數(shù)信息下面顯示圖標(biāo)
CPoint p = r.CenterPoint();
p.x -= 16;
p.y += 16;
pDC->DrawIcon(p,m_hIcon);
//繪制牌面提示按鈕(8*12) A-K
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap *poldbmp = memDC.SelectObject(&m_AllBmps);
CBrush brush(RGB(0,0,0));
for(UINT c = 1; c <= 13 ; c++) {
CRect r = pDoc->RectOf(c);
pDC->FrameRect(r,&brush);
CPoint p = r.CenterPoint();
p.x -= 4; p.y -= 6;
pDC->BitBlt(p.x,p.y,8,12,&memDC,(c-1)*8+32,0,SRCAND);
}
memDC.SelectObject(poldbmp);
//繪制牌局
p.x = p.y = 0;
for( UINT i = 1; i <= 16; i++ ) {
UINT nCards = pDoc->CntCardsIn(i);
if(!nCards) { //此列沒有牌則只繪制方框
//pDC->FrameRect(pDoc->RectOf(i,1,1),&brush);
CBrush *pPrevBrush = pDC->SelectObject(&m_brushBkgnd);
pDC->RoundRect(pDoc->RectOf(i,1,1),CPoint(5,5));
pDC->SelectObject(&pPrevBrush);
} else if(i <= 8) {
for( UINT j = 1; j <= nCards; j++ ) {
UINT card = pDoc->GetCard(i,j);
if(card == 0) continue; //這里本不該出現(xiàn)牌點數(shù)為0 的情況,都是多線程惹的禍
r = pDoc->RectOf(i, j, 1);
DrawCard(r.TopLeft() , card , pDC);
if(pDoc->m_nSel == i && j == nCards) {
pDC->InvertRect(r); //被選中列的底牌需要反色
}
}
} else { //繪制空檔列和回收列
r = pDoc->RectOf(i,1,1);
DrawCard(r.TopLeft(),pDoc->BottCard(i),pDC);
if(pDoc->m_nSel == i) { //如果此列是被選中狀態(tài),則此列牌被反色
pDC->InvertRect(r);
}
}
}
}
#ifdef _DEBUG
void CJLView::AssertValid() const
{
CView::AssertValid();
}
void CJLView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CJLDoc* CJLView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJLDoc)));
return (CJLDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CJLView message handlers
void CJLView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
UINT hit = m_nCardLabelHit;
m_nCardLabelHit = 0;
if(hit) {
ReleaseCapture();
CJLDoc *pDoc = GetDocument();
InvalidateRect(pDoc->RectOf(hit));//牌標(biāo)反色
CARD_POS pos[5], *p = pos, *pEnd = pDoc->FindCardForLabel(hit,pos);
while(p < pEnd) { //將與此牌標(biāo)對應(yīng)的牌正常顯示出來
CRect r = pDoc->RectOf(p->col,p->idx,1);
InvalidateRect(r);
++p;
}
}
CView::OnLButtonUp(nFlags, point);
}
void CJLView::OnLButtonDown(UINT nFlags, CPoint point)
{
//擊中提示牌對應(yīng)的按鈕則在此處理
bool bInverted = false;
UINT hit = CardLabelHitTest(point);
if(hit) {
CJLDoc *pDoc = GetDocument();
CARD_POS pos[5], *p = pos, *pEnd = pDoc->FindCardForLabel(hit,pos);
if(p < pEnd) { //如果有牌對應(yīng)牌標(biāo)
SetCapture();//捕獲鼠標(biāo)輸入
m_nCardLabelHit = hit;//記錄此牌標(biāo)
CClientDC cdc(this);
cdc.InvertRect(pDoc->RectOf(hit));//牌標(biāo)反色
CBrush brRed(RGB(255,0,0));
CBrush brBlk(RGB(0,0,0));
while(p < pEnd) { //將與此牌標(biāo)對應(yīng)的牌顯示出來并反色
CRect r = pDoc->RectOf(p->col,p->idx,1);
UINT card = pDoc->GetCard(p->col,p->idx);
DrawCard(r.TopLeft(),card,&cdc);
CBrush *pBr = TYPE(card)%2 == 0 ? &brBlk : &brRed;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -