woaidaima2016 发表于 2017-6-13 11:34:40

大众麻将【源码】

http://img.woaidaima.com//upload/image/20170517/1494983521993034028.jpghttp://img.woaidaima.com//upload/image/20170517/1494983523942066038.jpg#include "Stdafx.h"
#include "GameScore.h"
#include

//////////////////////////////////////////////////////////////////////////

//按钮标识
#define IDC_CLOSE_SCORE                100                                    //关闭成绩

//////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CGameScore, CDialog)
    ON_WM_PAINT()
    ON_WM_LBUTTONDOWN()
    ON_WM_ERASEBKGND()
    ON_BN_CLICKED(IDC_BT_NEXT, OnBnNext)
END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////////

//构造函数
CGameScore::CGameScore() : CDialog(IDD_GAME_SCORE)
{
    //初始化变量
    ZeroMemory( m_cbWeaveCount,sizeof(m_cbWeaveCount) );
    ZeroMemory(&m_ScoreInfo,sizeof(m_ScoreInfo));
    m_cbCurrentIndex = 0;

    HINSTANCE hInst = AfxGetInstanceHandle();
    m_ImageBack.LoadFromResource( hInst,IDB_GAME_SCORE );
    m_ImageFangPao.LoadImage( hInst,TEXT("FANG_PAO") );
    m_ImageZiMo.LoadImage( hInst,TEXT("ZI_MO") );
    m_PngBanker.LoadImage( hInst,TEXT("ZHUANG") );

}

//析构函数
CGameScore::~CGameScore()
{
}

//控件绑定
void CGameScore::DoDataExchange(CDataExchange * pDX)
{
    __super::DoDataExchange(pDX);
    DDX_Control(pDX, IDOK, m_btCloseScore);
    DDX_Control(pDX, IDC_BT_NEXT, m_btNext);
}

//初始化函数
BOOL CGameScore::OnInitDialog()
{
    __super::OnInitDialog();

    //删除窗口标题和获取属性
    ModifyStyle(WS_CAPTION,0,0);
    SetClassLong(m_hWnd,GCL_HBRBACKGROUND,NULL);

    //创建区域
    CRgn RgnControl;
    m_ImageBack.CreateImageRegion(RgnControl,RGB(255,0,255));

    //设置窗口
    if (RgnControl.GetSafeHandle()!=NULL)
    {
      //移动窗口
      SetWindowRgn(RgnControl,TRUE);
      SetWindowPos(NULL,0,0,m_ImageBack.GetWidth(),m_ImageBack.GetHeight(),SWP_NOMOVE);
    }

    m_btCloseScore.SetButtonImage(IDB_BT_SCORE_CLOSE,AfxGetInstanceHandle(),false,false);
    m_btNext.SetButtonImage(IDB_BT_NEXT,AfxGetInstanceHandle(),false,false);

    //调整按钮
    CRect rcClient;
    GetClientRect(&rcClient);
    CRect rcBtClose;
    m_btCloseScore.GetWindowRect(&rcBtClose);
    m_btCloseScore.SetWindowPos(NULL,(rcClient.Width()-rcBtClose.Width())/2,rcClient.Height()-rcBtClose.Height()-10,0,0,SWP_NOSIZE|SWP_NOZORDER);

    m_btNext.SetWindowPos(NULL,100,16,0,0,SWP_NOSIZE|SWP_NOZORDER);

    //创建字体
    //LOGFONT lg;
    //ZeroMemory( &lg,sizeof(lg) );
    //lg.lfCharSet = GB2312_CHARSET;
    //strcpy( lg.lfFaceName,"黑体" );
    //lg.lfHeight = -20;
    //if( !m_InfoFont.CreateFontIndirect( &lg ) )
    //    m_InfoFont.Attach(CSkinResourceManager::GetDefaultFont());

    //设置透明
    ModifyStyleEx(0,0x00080000);
    HINSTANCE hInst = LoadLibrary(TEXT("User32.DLL"));
    if(hInst)
    {
      typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
      MYFUNC fun = NULL;
      //取得SetLayeredWindowAttributes()函数指针
      fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
      if(fun)fun(this->GetSafeHwnd(),0,220,2);
      FreeLibrary(hInst);
    }

    return TRUE;
}

//复位数据
void CGameScore::RestorationData()
{
    //设置变量
    ZeroMemory( m_cbWeaveCount,sizeof(m_cbWeaveCount) );
    ZeroMemory(&m_ScoreInfo,sizeof(m_ScoreInfo));
    m_cbCurrentIndex = 0;

    for( BYTE i = 0; i < GAME_PLAYER; i++ )
    {
      m_cbWeaveCount = 0;

      for( BYTE j = 0; j < CountArray(m_WeaveCard); j++ )
            m_WeaveCard.SetCardData(NULL,0);

      m_ChiHuRight.SetEmpty();
    }

    //隐藏窗口
    if (m_hWnd!=NULL) ShowWindow(SW_HIDE);

    return;
}

//设置积分
void CGameScore::SetScoreInfo(const tagScoreInfo & ScoreInfo, const tagWeaveInfo WeaveInfo)
{
    //设置变量
    m_ScoreInfo=ScoreInfo;

    BYTE cbChiHuCount = 0;
    for( BYTE j = 0; j < GAME_PLAYER; j++ )
    {
      m_cbWeaveCount=WeaveInfo.cbWeaveCount;

      for (BYTE i=0;i<m_cbWeaveCount;i++)
      {
            bool bPublicWeave=(WeaveInfo.cbPublicWeave==TRUE);
            m_WeaveCard.SetCardData(WeaveInfo.cbCardData,WeaveInfo.cbCardCount);
            m_WeaveCard.SetDisplayItem(true);
      }
      
      m_ChiHuRight.SetRightData( m_ScoreInfo.dwChiHuRight,MAX_RIGHT_COUNT );

      if( !m_ChiHuRight.IsEmpty() )
            cbChiHuCount++;
    }

    //确定显示索引
    for( BYTE j = 0; j < GAME_PLAYER; j++ )
    {
      if( !m_ChiHuRight.IsEmpty() )
      {
            m_cbCurrentIndex = j;
            break;
      }
    }

    m_btNext.ShowWindow( cbChiHuCount>1?SW_SHOW:SW_HIDE );

    //显示窗口
    ShowWindow(SW_SHOW);

    return;
}

//关闭按钮
void CGameScore::OnOK()
{
    //隐藏窗口
    RestorationData();

    __super::OnOK();
}

//重画函数
void CGameScore::OnPaint()
{
    CPaintDC dc(this);

    //获取位置
    CRect rcClient;
    GetClientRect(&rcClient);

    //创建缓冲
    CDC DCBuffer;
    CBitmap ImageBuffer;
    DCBuffer.CreateCompatibleDC(&dc);
    ImageBuffer.CreateCompatibleBitmap(&dc,rcClient.Width(),rcClient.Height());

    //设置 DC
    DCBuffer.SetBkMode(TRANSPARENT);
    DCBuffer.SelectObject(&ImageBuffer);
    //DCBuffer.SelectObject(&m_InfoFont);
    DCBuffer.SetTextColor(RGB(250,250,250));

    //绘画背景
    if (m_ImageBack.IsNull()==false) m_ImageBack.BitBlt(DCBuffer,0,0);

    //位置变量
    int nCardSpace=5;
    int nItemWidth=g_CardResource.m_ImageTableBottom.GetViewWidth();
    int nTotalWidth=m_cbWeaveCount*(nItemWidth*3+nCardSpace)+nItemWidth*m_ScoreInfo.cbCardCount+nCardSpace;

    //计算位置
    int nYCardPos=94-g_CardResource.m_ImageTableBottom.GetViewHeight();
    int nXCardPos=(rcClient.Width()-nTotalWidth)/2;

    //绘画组合
    for (BYTE i=0;i<m_cbWeaveCount;i++)
    {
      //绘画扑克
      m_WeaveCard.DrawCardControl(&DCBuffer,nXCardPos,nYCardPos,false);

      //设置位置
      nXCardPos+=(nCardSpace+nItemWidth*3);
    }

    //绘画扑克
    for (BYTE i=0;i<m_ScoreInfo.cbCardCount;i++)
    {
      //绘画扑克
      g_CardResource.m_ImageTableBottom.DrawCardItem(&DCBuffer,m_ScoreInfo.cbCardData,nXCardPos,nYCardPos,false);

      //设置位置
      nXCardPos+=nItemWidth;
      if ((i+2)==m_ScoreInfo.cbCardCount) nXCardPos+=nCardSpace;
    }

    TCHAR szBuffer = TEXT("");
    //胡牌信息
    if( m_ScoreInfo.wProvideUser != INVALID_CHAIR )
    {
      //胡牌者
      CRect rcName(20,18,90,38);
      //DCBuffer.SelectObject(CSkinResourceManager::GetDefaultFont());
      DCBuffer.SetTextColor(RGB(252,255,0));
      DCBuffer.DrawText(m_ScoreInfo.szUserName,lstrlen(m_ScoreInfo.szUserName),&rcName,
            DT_SINGLELINE|DT_END_ELLIPSIS|DT_VCENTER|DT_CENTER);

      DCBuffer.SelectObject(&m_InfoFont);
      DCBuffer.SetTextColor( RGB(250,250,250) );
      CRect rcDraw;
      //放炮
      if( m_ScoreInfo.wProvideUser != m_cbCurrentIndex )
      {
            rcDraw.SetRect(15,102,190,129);
            DCBuffer.DrawText( m_ScoreInfo.szUserName,lstrlen(m_ScoreInfo.szUserName),
                &rcDraw,DT_SINGLELINE|DT_END_ELLIPSIS|DT_RIGHT|DT_BOTTOM );
            m_ImageFangPao.DrawImage( &DCBuffer,205,104 );
            rcDraw.SetRect(295,102,482,129);
            DCBuffer.DrawText( m_ScoreInfo.szUserName,lstrlen(m_ScoreInfo.szUserName),
                &rcDraw,DT_SINGLELINE|DT_END_ELLIPSIS|DT_LEFT|DT_BOTTOM );
      }
      //自摸
      else
      {
            rcDraw.SetRect(15,102,190,129);
            DCBuffer.DrawText( m_ScoreInfo.szUserName,lstrlen(m_ScoreInfo.szUserName),
                &rcDraw,DT_SINGLELINE|DT_END_ELLIPSIS|DT_RIGHT|DT_BOTTOM );
            m_ImageZiMo.DrawImage( &DCBuffer,215,104 );
      }
    }

    //得分信息
    DCBuffer.SetTextColor( RGB(250,250,250) );
    for( WORD i = 0; i < GAME_PLAYER; i++ )
    {
      //庄家
      if( i == m_ScoreInfo.wBankerUser )
            m_PngBanker.DrawImage( &DCBuffer,100-m_PngBanker.GetWidth()-10,143+i*26 );
      CRect rcDrawPlayer(100,138+i*26,275,164+i*26);
      DCBuffer.SetTextColor( RGB(250,250,250) );
      //玩家
      DCBuffer.DrawText( m_ScoreInfo.szUserName,lstrlen(m_ScoreInfo.szUserName),
            &rcDrawPlayer,DT_SINGLELINE|DT_END_ELLIPSIS|DT_LEFT|DT_BOTTOM );

      //分数
      CRect rcDrawScore(295,138+i*26,355,164+i*26);
      _sntprintf( szBuffer,CountArray(szBuffer),TEXT("%+I64d"),m_ScoreInfo.lGameScore );
      DCBuffer.SetTextColor( RGB(252,255,0) );
      DCBuffer.DrawText( szBuffer,lstrlen(szBuffer),&rcDrawScore,DT_SINGLELINE|DT_BOTTOM|DT_RIGHT );

      DCBuffer.SetTextColor( RGB(250,250,250) );
      CRect rcDrawFen(360,138+i*26-46,512,164+i*26);
      DCBuffer.DrawText( CString(TEXT("分")),&rcDrawFen,DT_SINGLELINE|DT_LEFT|DT_BOTTOM );
    }

    //牌型信息
    LPCTSTR pszKind[] = {
      TEXT("自摸"),TEXT("放炮胡")
    };

    //牌型区域
    CRect rcFanXing(34,255,159,281);
    DCBuffer.SetTextColor( RGB(250,250,250) );
   
    if( m_ScoreInfo.wLeftUser == INVALID_CHAIR )
    {
      //放炮
      if( m_ScoreInfo.wProvideUser != m_cbCurrentIndex )
            DCBuffer.DrawText( pszKind,lstrlen(pszKind),rcFanXing,DT_SINGLELINE|DT_LEFT|DT_BOTTOM );
      else DCBuffer.DrawText( pszKind,lstrlen(pszKind),rcFanXing,DT_SINGLELINE|DT_LEFT|DT_BOTTOM );
    }
    //玩家逃跑
    else
    {
      LONGLONG lTemp = __max(m_ScoreInfo.lGameScore,-m_ScoreInfo.lGameScore);
      _sntprintf( szBuffer,CountArray(szBuffer),TEXT("玩家【%s】逃跑,扣除%I64d分!"),m_ScoreInfo.szUserName,
            lTemp );
      DCBuffer.DrawText( szBuffer,lstrlen(szBuffer),CRect(34,258,36+400,258+100),DT_LEFT|DT_WORDBREAK|DT_END_ELLIPSIS );
    }

    //绘画界面
    dc.BitBlt(0,0,rcClient.Width(),rcClient.Height(),&DCBuffer,0,0,SRCCOPY);

    //清理资源
    DCBuffer.DeleteDC();
    ImageBuffer.DeleteObject();

    return;
}

//鼠标消息
void CGameScore::OnLButtonDown(UINT nFlags, CPoint Point)
{
    __super::OnLButtonDown(nFlags,Point);

    //消息模拟
    PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(Point.x,Point.y));

    return;
}

//背景函数
BOOL CGameScore::OnEraseBkgnd(CDC * pDC)
{
    Invalidate(FALSE);
    UpdateWindow();
    return TRUE;
}

//
void CGameScore::OnBnNext()
{
    BYTE cbIndexTemp = m_cbCurrentIndex;
    cbIndexTemp = (cbIndexTemp+1)%GAME_PLAYER;
    while( cbIndexTemp != m_cbCurrentIndex )
    {
      if( !m_ChiHuRight.IsEmpty() ) break;
      cbIndexTemp = (cbIndexTemp+1)%GAME_PLAYER;
    }
    if( cbIndexTemp != m_cbCurrentIndex )
    {
      m_cbCurrentIndex = cbIndexTemp;
      Invalidate(FALSE);
      UpdateWindow();
    }
    return;
}

//////////////////////////////////////////////////////////////////////////


页: [1]
查看完整版本: 大众麻将【源码】