diceroll的位图

时间:2013-04-25 00:36:44

标签: c++ visual-studio-2012 mfc bitmap

你将如何进行滚动骰子并显示相应的骰子面部位图

一些快速有效的代码示例?

void CMFCApplication12Dlg::OnBnClickedDiceroll()

    {  
       int die1 = 1 + rand() % 6; // first die roll
       int die2 = 1 + rand() % 6; // second die roll
       //display die1 & die2
    }

//     void CMFCApplication12Dlg :: OnPaint handler()

{
//load the image file
    CString szFilename("C:\\Talla\\yourimg.bmp");
    HBITMAP hBmp = (HBITMAP)::LoadImage(NULL,szFilename,
    IMAGE_BITMAP,0,0,
    LR_LOADFROMFILE|LR_CREATEDIBSECTION);

//Create a Bitmap Object and Attach It to the Object
    CBitmap bmp;
    bmp.Attach(hBmp);

// Create a Memory DC and Select the BMP to It
    CClientDC dc(this);
    CDC bmDC;
    bmDC.CreateCompatibleDC(&dc);
    CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

//Get the BMP Height and Width 
    BITMAP bi;
    bmp.GetBitmap(&bi);

//Get the Block of Pixels from memoryDC to the Screen

    dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
    bmDC.SelectObject(pOldbmp);
    }

1 个答案:

答案 0 :(得分:2)

使用InvalidateRect使窗口无效,以便重新绘制自身。在WM_PAINT处理程序中,您需要将die数转换为资源ID。然后从LoadImage的资源加载位图。创建一个内存DC并在其中选择位图,然后使用BitBlt将其复制到窗口。