首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

Cimagelist :add//运用的疑惑

2013-05-02 
Cimagelist ::add//使用的疑惑BOOL CToolBarEx::SetButtonEx(LPCTSTR lpszResourceName){CClientDC rdc(th

Cimagelist ::add//使用的疑惑


BOOL CToolBarEx::SetButtonEx(LPCTSTR lpszResourceName)
{
CClientDC rdc(this);
CDC dc1;
CBitmap bmp1,bmpd;
bmp1.LoadBitmap(lpszResourceName);
dc1.CreateCompatibleDC(&rdc);
dc1.SelectObject(&bmp1);
LPBYTE lpbts;
int ncount=0;
ncount=(bitmap.bmHeight*bitmap.bmWidthBytes);
lpbts=new BYTE[ncount];
bmp1.GetBitmapBits(ncount,lpbts);
bmpd.SetBitmapBits(ncount,lpbts);
m_imglstNormal.DeleteImageList();
m_imglstNormal.Create(m_nButtonWidth, m_nButtonHeight, bitmap.bmBitsPixel|ILC_MASK, 1, 1);
m_imglstNormal.Add(&bmpd, maskcolor);

//为什么换成bmp1;返回0值图片加载成功;但是为什么是黑图呢
bmp1.GetBitmapBits(ncount,lpbts);
bmpd.SetBitmapBits(ncount,lpbts);执行这两步操作 就是彩图!

GetToolBarCtrl().SetImageList(&m_imglstNormal);
delete[] lpbts;
return TRUE;
}




BOOL CGDIxToolBar::LoadToolBar(UINT nIDResource)
{
      LPCTSTR lpszResourceName = MAKEINTRESOURCE(nIDResource);
      HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName,RT_TOOLBAR);
      HRSRC hRsrc = ::FindResource(hInst,lpszResourceName,RT_TOOLBAR);
}
BOOL CGDIxToolBar::LoadBitmap(LPCTSTR lpszResourceName)
{
        HINSTANCE hInstImageWell = AfxFindResourceHandle(lpszResourceName,RT_BITMAP);
HRSRC hRsrcImageWell = ::FindResource(hInstImageWell,lpszResourceName,RT_BITMAP);
if(hRsrcImageWell == NULL)
return FALSE;

HGLOBAL hglb;
if((hglb = LoadResource(hInstImageWell,hRsrcImageWell)) == NULL)
return FALSE;

LPBITMAPINFOHEADER lpBitmap = (LPBITMAPINFOHEADER)LockResource(hglb);
if(lpBitmap == NULL)
return NULL;

::FreeResource(hglb);

m_lpszResourceName = lpszResourceName;
SetButtonEx(lpszResourceName);
return TRUE;
}

[解决办法]


//对话框与单文档还是一起退了
void CFrameWnd::OnDestroy()
{
    DestroyDockBars();
 
    // reset menu to default before final shutdown
    if (m_hMenuDefault != NULL && ::GetMenu(m_hWnd) != m_hMenuDefault)
    {
        ::SetMenu(m_hWnd, m_hMenuDefault);
        ASSERT(::GetMenu(m_hWnd) == m_hMenuDefault);
    }
 
    // Automatically quit when the main window is destroyed.
    CWinApp* pApp = AfxGetApp();
    if (pApp != NULL && pApp->m_pMainWnd == this && pApp->m_eHelpType == afxWinHelp)
    {
        // closing the main application window
        ::WinHelp(m_hWnd, NULL, HELP_QUIT, 0L);
 
        // will call PostQuitMessage in CWnd::OnNcDestroy
    }
    CWnd::OnDestroy();


}


引用:
说实话,我看这两段代码也完全看晕了,纯粹为了增加代码行数的么?

[解决办法]
你不觉得无效代码太多了么?我给你归结一下有效代码:
BOOL CGDIxToolBar::LoadBitmap(LPCTSTR lpszResourceName)
{
        return SetButtonEx(lpszResourceName);
}

BOOL CToolBarEx::SetButtonEx(LPCTSTR lpszResourceName)
{
        m_imglstNormal.DeleteImageList();
        if (!m_imglstNormal.Create(lpszResourceName, m_nButtonWidth, 1, maskcolor))
        {
                return FALSE;
        }
        GetToolBarCtrl().SetImageList(&m_imglstNormal);
        return TRUE;
}


热点排行