0

您将如何掷骰子并显示相应的骰子面位图

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

void CMFCApplication12Dlg::OnBnClickedDiceroll()

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

// 无效 CMFCApplication12Dlg:: OnPaint 处理程序 ()

{
//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);
    }
4

1 回答 1

2

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

于 2013-04-25T00:58:44.820 回答