在主主界面上有一个按钮,点击之后会弹出一个对话框,
我在这个弹出的对话框中简单的处理一下,想让其只显示一朵玫瑰,但是就是实现不了,代码绝对
没有问题,因为在主界面当中按照这段代码,就可以实现但是在这个弹出的对话框中
就是是实现不了,实在是没有办法了,帮我看看吧
我觉得是在调用模式对话框的时候,MFC在处理方面有点问题,但是还找不出来
问题在什么地方。
BOOL zzy::OnInitDialog() 
{
CDialog::OnInitDialog();
m_bmBitmap = new CBitmap;
m_bmBitmap->LoadBitmap(IDB_BITMAP1);
// TODO: Add extra initialization here
CRect rcWnd;
GetWindowRect (rcWnd); CRgn rgn;
rgn.CreateRectRgn (rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom); CRect rcClient;
GetClientRect (rcClient);
ClientToScreen (rcClient); CRgn rgnClient;
rgnClient.CreateRectRgn (rcClient.left, rcClient.top, rcClient.right,
rcClient.bottom); // Subtract rgnClient from rgn
rgn.CombineRgn (&rgn, &rgnClient, RGN_XOR); // Get a DC for the bitmap
CDC dcImage;
dcImage.CreateCompatibleDC (NULL);
CBitmap *pOldBitmap = dcImage.SelectObject (m_bmBitmap); // Get the bitmap for width and height information
BITMAP bm;
m_bmBitmap->GetBitmap (&bm); // Get window width and height
CRect rc;
GetClientRect (rc); // Use the minimum width and height
int width = min (bm.bmWidth, rc.Width());
int height = min (bm.bmHeight, rc.Height()); // Use RLE (run-length) style because it goes faster.
// Row start is where the first opaque pixel is found.  Once
// a transparent pixel is found, a line region is created.
// Then row_start becomes the next opaque pixel.
int row_start; // Go through all rows
for (int y=0; y<height; y++)
{
// Start looking at the beginning
row_start = 0; // Go through all columns
for (int x=0; x<width; x++)
{
// If this pixel is transparent
if (dcImage.GetPixel(x, y)==RGB(0,0,0))
{
// If we haven't found an opaque pixel yet, keep searching
if (row_start == x) row_start ++;
else
{
// We have found the start (row_start) and end (x) of
// an opaque line.  Add it to the region.
CRgn rgnAdd;
rgnAdd.CreateRectRgn (rcClient.left+row_start,
rcClient.top+y, rcClient.left+x, rcClient.top+y+1);
rgn.CombineRgn (&rgn, &rgnAdd, RGN_OR);
row_start = x+1;
}
}
} // If the last pixel is still opaque, make a region.
if (row_start != x)
{
CRgn rgnAdd;
rgnAdd.CreateRectRgn (rcClient.left+row_start, rcClient.top+y,
rcClient.left+x, rcClient.top+y+1);
rgn.CombineRgn (&rgn, &rgnAdd, RGN_OR);
}
}

SetWindowRgn (rgn, TRUE);

return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}void zzy::OnPaint() 
{
CPaintDC dc(this); // device context for painting
CRect rc;
GetClientRect (rc); // Get a DC for the bitmap
CDC dcImage;
dcImage.CreateCompatibleDC (&dc);
CBitmap *pOldBitmap = dcImage.SelectObject (m_bmBitmap); // Get bitmap width and height
BITMAP bm;
m_bmBitmap->GetBitmap (&bm); // Use the minimum width and height
int width = min (bm.bmWidth, rc.Width());
int height = min (bm.bmHeight, rc.Height());
     // Draw the bitmap as the window background
dc.BitBlt (0, 0, rc.Width(), rc.Height(), &dcImage, 0, 0, SRCCOPY);
// TODO: Add your message handler code here

解决方案 »

  1.   

    把 OnInitDialog() 中 m_bmBitmap->LoadBitmap(IDB_BITMAP1);
    后面的代码移到 OnPaint() 再综合处理吧
      

  2.   

    应该是窗口重画的问题,你可以上楼上的说的那样,将代码放到OnPaint()函数中去!
    我想你的要求应该可以实现!祝你成功!
      

  3.   

    对话框中画图,为什么不把它放到OnPaint中!
      

  4.   

    在OnPaint我已经作出了位图显示的处理
    而且用上面的代码,在主界面当中都好用
    但是在调用的对话框中就不好用了,谁可以说的在具体一些
    在调用对话框的初始化中是把黑色的部分祛除,并没有在初始化的函数中进行显示
      

  5.   

    楼主处理好大一段,我没看懂,就写了一个从资源中显示图片的,不知道是否有帮助。void CAboutDlg::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting

    // TODO: Add your message handler code here
    CBitmap m_bmp;
    CRect rec;
    CDC dc2; CDC* dc1 = GetDC();
    GetClientRect(&rec);
    m_bmp.LoadBitmap("IDB_BITMAP1");

    dc2.CreateCompatibleDC(dc1);
    dc2.SelectObject(m_bmp); dc1->BitBlt(0, 0, rec.right, rec.bottom, &dc2, 0, 0, SRCCOPY);
    }
      

  6.   

    在MFC中已经不大区别模态和非模态的对话框了。
      

  7.   

    void CAboutDlg::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting
    static bool Mark = true; if (Mark) 
    {
    Mark = false;
    CBitmap m_bmp;
    BITMAP bm;

    CDC  dc2, dc3;
    COLORREF pt; m_bmp.LoadBitmap("IDB_BITMAP1");
    m_bmp.GetBitmap(&bm);

    m_menbmp.CreateCompatibleBitmap(&dc, bm.bmWidth, bm.bmHeight); dc2.CreateCompatibleDC(&dc);
    dc2.SelectObject(m_bmp); dc3.CreateCompatibleDC(&dc);
    dc3.SelectObject(m_menbmp); for (int i = 0; i < bm.bmWidth; i++)
    for (int j = 0; j < bm.bmHeight; j++)
    {
    pt = dc2.GetPixel(i, j);
    if(pt != RGB(0,0,0)) 
    {
    dc.SetPixel(i, j, pt);
    }
    };
    dc3.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dc, 0, 0, SRCCOPY); 
    } CDC dc1;
    CRect rec;
    this->GetClientRect(&rec);
    dc1.CreateCompatibleDC(&dc);
    dc1.SelectObject(m_menbmp);
    dc.BitBlt(0, 0, rec.right, rec.bottom, &dc1, 0, 0, SRCCOPY); // TODO: Add your message handler code here
    }
      

  8.   

    增加一个成员private:
    CBitmap m_menbmp;
      

  9.   

    上面的难道不需要
    CRGN这个类么?