首先想问个基本问题,网上经常提到的picture控件和IMAGE控件是一种东西吗?我的是中文版C++只看到了图片控件另外,在图片控件关联了图片以后,发现图片不能自适应边框大小,一直都不变,选了属性->样式里的图像居中以后图片还是不变,就是边框可以调整而已,应该怎么调整啊?最后 想问下,要是想在一个对话框中画上组合框和图片,组合框选择以后在同一个位置显示出相对应的图片,应该怎么做?(几个小问题一起打包问了,就多放点分好了)
调试欢乐多
2、需要自绘,可以用CImage类Load图片,响应WM_PAINT消息用StretchBlt贴图。
3、Load新图片,然后InvalidateRect刷新,在响应WM_PAINT消息中贴图。如果用Picture控件,用CStatic类的SetBitmap函数。
CStrat 是一个对话框
class CStrat : public CDialog, public CBackgroundUtil 多重继承BOOL CStrat::OnInitDialog() {
SetBitmap(IDB_BITMAP3); 显示图片
}BOOL CStrat::OnEraseBkgnd(CDC* pDC) 对话框类
{
CRect rc;
GetClientRect(rc); m_pDC = pDC;
if (TileBitmap(pDC, rc) == TRUE)
return TRUE;
else
return CDialog::OnEraseBkgnd(pDC);
} // End of OnEraseBkgnd
*****************************************************************
TileBitmap来自类 CBackgroundUtil CBackgroundUtil代码: CBackgroundUtil.h
class CBackgroundUtil
{
public:
BOOL SetBitmap(UINT uResourceID = 0);
CBackgroundUtil();
virtual ~CBackgroundUtil(); protected:
BOOL TileBitmap(CDC* pDC, CRect rc);
BOOL GetBitmapAndPalette(UINT nIDResource, CBitmap& bitmap, CPalette& pal);
// For background bitmap
CBitmap m_BmpPattern;
CPalette m_BmpPalette;
int m_nBmpWidth;
int m_nBmpHeight;
}; #endif CBackgroundUtil.CPP:
CBackgroundUtil::CBackgroundUtil()
{
}
CBackgroundUtil::~CBackgroundUtil()
{
m_BmpPattern.Detach();
m_BmpPalette.Detach();
}
BOOL CBackgroundUtil::SetBitmap(UINT uResourceID)
{
BITMAP bm;
BOOL bRet; // Detach previous resources
m_BmpPattern.Detach();
m_BmpPalette.Detach(); // Default return value
bRet = TRUE; // Load new bitmap
if (uResourceID != 0)
{
bRet = GetBitmapAndPalette(uResourceID, m_BmpPattern, m_BmpPalette);
// If all ok
if (bRet == TRUE)
{
// Get dimension
m_BmpPattern.GetBitmap(&bm);
// Width of the bitmap
m_nBmpWidth = bm.bmWidth;
// Height of the bitmap
m_nBmpHeight = bm.bmHeight;
}
} return bRet;
} // End of SetBitmap
BOOL CBackgroundUtil::GetBitmapAndPalette(UINT nIDResource, CBitmap & bitmap, CPalette & pal)
{
LPCTSTR lpszResourceName = (LPCTSTR)nIDResource; HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(),
lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION); if (hBmp == NULL) return FALSE; bitmap.Attach(hBmp); // Create a logical palette for the bitmap
DIBSECTION ds;
BITMAPINFOHEADER &bmInfo = ds.dsBmih;
bitmap.GetObject(sizeof(ds), &ds); int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 < < bmInfo.biBitCount; // Create a halftone palette if colors > 256.
CClientDC dc(NULL); // Desktop DC if(nColors > 256)
pal.CreateHalftonePalette(&dc);
else
{
// Create the palette
RGBQUAD *pRGB = new RGBQUAD[nColors];
CDC memDC;
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject( &bitmap );
::GetDIBColorTable( memDC, 0, nColors, pRGB );
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for (int i=0; i < nColors; i++)
{
pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
}
pal.CreatePalette( pLP );
delete[] pLP;
delete[] pRGB;
}
return TRUE;
} // End of GetBitmapAndPalette
BOOL CBackgroundUtil::TileBitmap(CDC* pDC, CRect rc)
{
CDC MemDC; int x = 0, y = 0; // If there is a bitmap loaded
if (m_BmpPattern.m_hObject != NULL)
{
MemDC.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = MemDC.SelectObject(&m_BmpPattern); // Tile the bitmap
while (y < rc.Height())
{
while(x < rc.Width())
{
pDC->BitBlt(x, y, m_nBmpWidth, m_nBmpHeight, &MemDC, 0, 0, SRCCOPY);
x += m_nBmpWidth;
}
x = 0;
y += m_nBmpHeight;
} MemDC.SelectObject(pOldBitmap); return TRUE;
} return FALSE; // Normal behaviour
} // End of TileBitmap
CBitmap bitmap;
CDC *pDC = GetDC();
CDC dcCompatible;
BITMAP bmp;
if(m_sSel == 0 )
{
bitmap.LoadBitmap(IDB_BITMAP1);
bitmap.GetBitmap(&bmp);
dcCompatible.CreateCompatibleDC(pDC);
dcCompatible.SelectObject(&bitmap); pDC->StretchBlt(45,100,150,200,&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
// Invalidate();放在这边和开头好像都不行,都会闪烁
if(m_sSel==1){......}
谢谢大家的帮助