
| | auteur : lucky |
void RempliDegradeRectHautBas(CDC* pDC,CRect rect
,short RedHaut,short GrHaut,short BlHaut
,short RedMilieu,short GrMilieu,short BlMilieu
,short RedBas,short GrBas,short BlBas)
{
short ColorR, ColorG, ColorB ;
int nWidth = rect.Width ();
int nHeight = rect.Height ();
short ColorRinterm = RedHaut - RedMilieu ;
short ColorGinterm = GrHaut - GrMilieu ;
short ColorBinterm = BlHaut - BlMilieu ;
for(int i = 0; i < nHeight/2; ++i)
{
rect.SetRect(0, i, nWidth, nHeight/2 + 1);
ColorR = RedHaut - MulDiv(i,ColorRinterm, nHeight/2) ;
ColorG = GrHaut - MulDiv(i,ColorGinterm, nHeight/2) ;
ColorB = BlHaut - MulDiv(i,ColorBinterm, nHeight/2) ;
pDC->FillSolidRect(&rect, RGB(ColorR ,ColorG,ColorB));
}
ColorRinterm = RedMilieu - RedBas;
ColorGinterm = GrMilieu - GrBas;
ColorBinterm = BlMilieu - BlBas;
for( i = nHeight/2; i < nHeight; ++i)
{
rect.SetRect(0, i, nWidth, nHeight + 1);
ColorR = RedMilieu - MulDiv(i-nHeight/2,ColorRinterm, nHeight/2) ;
ColorG = GrMilieu - MulDiv(i-nHeight/2,ColorGinterm, nHeight/2) ;
ColorB = BlMilieu - MulDiv(i-nHeight/2,ColorBinterm, nHeight/2) ;
pDC->FillSolidRect(&rect, RGB(ColorR ,ColorG,ColorB));
}
}
void RempliDegradeRectGaucheDroite(CDC* pDC,CRect rect
,short RedGauche,short GrGauche,short BlGauche
,short RedMilieu,short GrMilieu,short BlMilieu
,short RedDroit,short GrDroit,short BlDroit)
droite
{
short ColorR, ColorG, ColorB ;
int nWidth = rect.Width ();
int nHeight = rect.Height ();
short ColorRinterm = RedGauche - RedMilieu ;
short ColorGinterm = GrGauche - GrMilieu ;
short ColorBinterm = BlGauche - BlMilieu ;
for(int i = 0; i < nWidth/2; ++i)
{
rect.SetRect(i, 0, nWidth/2 + 1, nHeight);
ColorR = RedGauche - MulDiv(i,ColorRinterm, nWidth/2) ;
ColorG = GrGauche - MulDiv(i,ColorGinterm, nWidth/2) ;
ColorB = BlGauche - MulDiv(i,ColorBinterm, nWidth/2) ;
pDC->FillSolidRect(&rect, RGB(ColorR ,ColorG,ColorB));
}
ColorRinterm = RedMilieu - RedDroit;
ColorGinterm = GrMilieu - GrDroit;
ColorBinterm = BlMilieu - BlDroit;
for( i = nWidth/2; i < nWidth; ++i)
{
rect.SetRect(i,
0, nWidth + 1, nHeight);
ColorR = RedMilieu - MulDiv(i-nWidth/2,ColorRinterm, nWidth/2) ;
ColorG = GrMilieu - MulDiv(i-nWidth/2,ColorGinterm, nWidth/2) ;
ColorB = BlMilieu - MulDiv(i-nWidth/2,ColorBinterm, nWidth/2) ;
pDC->FillSolidRect(&rect, RGB(ColorR ,ColorG,ColorB));
}
}
void RempliDegradeSimpleHautBas(CDC* pDC,CRect rect
,short RedHaut,short GrHaut,short BlHaut
,short RedBas,short GrBas,short BlBas)
short ColorR, ColorG, ColorB ;
int nWidth = rect.Width ();
int nHeight = rect.Height ();
for(int i = 0; i < nHeight; ++i)
{
rect.SetRect(0, i, nWidth, nHeight + 1);
ColorR = RedHaut - MulDiv(i,RedHaut - RedBas, nHeight) ;
ColorG = GrHaut - MulDiv(i,GrHaut - GrBas, nHeight) ;
ColorB = BlHaut - MulDiv(i,BlHaut - BlBas, nHeight) ;
pDC->FillSolidRect(&rect, RGB(ColorR ,ColorG,ColorB));
}
}
void RempliDegradeSimpleGaucheDroit(CDC* pDC,CRect rect
,short RedGauche,short GrGauche,short BlGauche
,short RedDroit,short GrDroit,short BlDroit)
{
short ColorR, ColorG, ColorB ;
int nWidth = rect.Width ();
int nHeight = rect.Height ();
for(int i = 0; i < nWidth; ++i)
{
rect.SetRect(i, 0, nWidth + 1, nHeight);
ColorR = RedGauche - MulDiv(i,RedGauche - RedDroit, nWidth) ;
ColorG = GrGauche - MulDiv(i,GrGauche - GrDroit, nWidth) ;
ColorB = BlGauche - MulDiv(i,BlGauche - BlDroit, nWidth) ;
pDC->FillSolidRect(&rect, RGB(ColorR ,ColorG,ColorB));
}
}
|
|
| | auteur : Farscape | en utilisant la fonction SHGetFileInfo comme dans l'exemple ci dessous :
HICON GetIconFromFile(LPCTSTR sz)
{
CString key,ret;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
CString szpath,szext;
HIMAGELIST hSysImageList;
SHFILEINFO shfi;
int nic;
HICON hicon=NULL;
_splitpath(sz,drive,dir,fname,ext);
szext=ext;
szpath=sz;
if(!(szext.Find(".exe")!=-1 ||
szext.Find(".pif")!=-1 ||
szext.IsEmpty() ||
strlen(szext)<4 ||
szext.Find(".lnk")!=-1) &&
SHGetFileInfo(szext,FILE_ATTRIBUTE_NORMAL,&shfi,sizeof(shfi),
SHGFI_USEFILEATTRIBUTES|SHGFI_ICON|SHGFI_SMALLICON))
{
hicon=shfi.hIcon;
}
if(!hicon)
{
hSysImageList=(HIMAGELIST) SHGetFileInfo(szpath,0,&shfi,sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
if(hSysImageList)
{
nic=shfi.iIcon;
hicon=ImageList_GetIcon(hSysImageList,nic,ILD_NORMAL);
}
}
return hicon;
}
|
|
| | auteur : Farscape | On obtient la taille de l'écran avec la fonction GetSystemMetrics:
int nResolx=GetSystemMetrics(SM_CXSCREEN);
int nResoly=GetSystemMetrics(SM_CYSCREEN);
|
|
| | auteur : Farscape | L'exemple ci-dessous montre les étapes nécessaires pour lire et afficher un bitmap provenant des ressources (IDB_BMPESSAI) .
CMyView::OnDraw(CDC *pDC)
{
CBitmap Bitmap;
CDC MemDC;
Bitmap.LoadBitmap(IDB_BMPESSAI);
BITMAP InfosBmp;
Bitmap.GetBitmap(&InfosBmp);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&Bitmap);
pDC->BitBlt( 0,0,InfosBmp.bmWidth, InfosBmp.bmHeight,
&MemDC,
0,0,
SRCCOPY);
}
|
Note:
l'exemple ci-dessus fonctionne très bien à l'écran.
dans le cas d'une édition sur imprimante ce bitmap ne sera pas compatible, il faudra disposer d'un bitmap DIB: device-independent bitmap au lieu d'un DDB: device-dependent bitmap .
|
| | auteur : Farscape | Il faut utiliser la fonction StretchBlt à la place de BitBlt
CDC::StretchBlt
BOOL StretchBlt (
int x,
int y,
int nWidth,
int nHeight,
CDC* pSrcDC,
int xSrc,
int ySrc,
int nSrcWidth,
int nSrcHeight,
DWORD dwRop );
|
Exemple d'affichage avec un bitmap appartenant aux ressources.
CMyView::OnDraw(CDC *pDC)
{
CBitmap Bitmap;
CDC MemDC;
Bitmap.LoadBitmap(IDB_BMPESSAI);
BITMAP InfosBmp;
Bitmap.GetBitmap(&InfosBmp);
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&Bitmap);
CSize Size(InfosBmp.bmWidth *2 , InfosBmp.bmHeight *2);
pDC->DPtoLP(&Size);
pDC->StretchBlt( 0, 0,
Size.cx,-Size.cy,
&MemDC,
0, 0,
InfosBmp.bmWidth, InfosBmp.bmHeight,
SRCCOPY);
}
|
Note: Pour la réduction d'un bitmap il est conseillé d'appliquer un :
pDC->SetStretchBltMode( COLORONCOLOR );
|
Avant le StretchBlt,COLORONCOLOR donne un meilleur aspect aux bitmaps rétrécis.
|
| | auteur : Farscape | En utilisant la fonction :
CDC::GetTextExtent
CSize GetTextExtent( LPCTSTR lpszString, int nCount ) const;
CSize GetTextExtent( const CString& str ) const;
|
Exemple d'utilisation :
CClientDC dc(this);
CFont* pFont = dc.SelectObject(m_pMyFont);
CSize size=dc.GetTextExtent(string);
|
La variable size contiendra la hauteur (size.cy) et largeur (size.cx) de la chaîne pour la fonte en cours.
Après il est facile de calculer un rectangle d'occupation à partir d'une position x,y.
|
| | auteur : Farscape | Pour spécifier le mode d'impression portrait ou paysage sur une imprimante il faut disposer d'un pointeur sur la structure DEVMODE qui contient les données liées à l'imprimante.
Le code ci-dessous commence par récupérer l'imprimante par défaut pour utiliser le pointeur m_hDevMode qui est une donnée membre de la classe CWinApp.
void CMyApp::SetLandscapeMode()
{
PRINTDLG pd;
pd.lStructSize=(DWORD)sizeof(PRINTDLG);
BOOL bRet=GetPrinterDeviceDefaults(&pd);
if(bRet)
{
DEVMODE FAR *pDevMode=(DEVMODE FAR *)::GlobalLock(m_hDevMode);
pDevMode->dmOrientation=DMORIENT_LANDSCAPE;
::GlobalUnlock(m_hDevMode);
}
}
|
|
| | auteur : Farscape | Dans Visual Studio on peut définir pour le même identifiant de ressource une icône au format 32x32 et 16x16 pixels.
la fonction
HICON CWinApp::LoadIcon( UINT nIDResource ) const;
|
Lit par défaut l'icône au format 32x32 .
pour la lecture du format 16x16 on procédera comme suit:
HICON hIcon = (HICON)::LoadImage( AfxFindResourceHandle(MAKEINTRESOURCE(IDI_MYICONE),RT_GROUP_ICON),
MAKEINTRESOURCE(IDI_MYICONE),
IMAGE_ICON,
16,16,LR_DEFAULTSIZE);
m_MyStatic.SetIcon( hIcon );
|
IDI_MYICONE représente l'identifiant de l'icône à lire.
Note: l'icône devra étre libérée par un DestroyIcon
|
HICON hIcon =m_MyStatic.GetIcon();
if(hIcon) ::DestroyIcon(hIcon);
|
|
| | auteur : matazz |
HANDLE DDBToDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* pPal )
{
BITMAPbm;
BITMAPINFOHEADERbi;
LPBITMAPINFOHEADER lpbi;
DWORDdwLen;
HANDLEhDIB;
HANDLEhandle;
HDC hDC;
HPALETTEhPal;
ASSERT( bitmap.GetSafeHandle() );
if( dwCompression == BI_BITFIELDS )
return NULL;
hPal = (HPALETTE) pPal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
bi.biSize= sizeof(BITMAPINFOHEADER);
bi.biWidth= bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount= bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression= dwCompression;
bi.biSizeImage= 0;
bi.biXPelsPerMeter= 0;
bi.biYPelsPerMeter= 0;
bi.biClrUsed= 0;
bi.biClrImportant= 0;
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
if (!hDIB)
{
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
*lpbi = bi;
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi;
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
BOOL bGotBits = GetDIBits( hDC, (HBITMAP)bitmap.GetSafeHandle(),
0L,
(DWORD)bi.biHeight,
(LPBYTE)lpbi
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi,
(DWORD)DIB_RGB_COLORS);
if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}
|
|
| | auteur : matazz |
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADERhdr;
LPBITMAPINFOHEADERlpbi;
if (!hDIB)
return FALSE;
CFile file;
if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
return FALSE;
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = 1 << lpbi->biBitCount;
hdr.bfType= ((WORD) ('M' << 8) | 'B');
hdr.bfSize= GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits= (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD));
file.Write( &hdr, sizeof(hdr) );
file.Write( lpbi, GlobalSize(hDIB) );
file.Close();
return TRUE;
}
|
|
| | auteur : matazz |
HINSTANCEhIn= NULL;
HRSRCSrc= NULL;
HGLOBAL hMem= NULL;
DWORD rsize= NULL;
CStringRSNumb;
RSNumb.Format("#%d",IDR_RESOURCE_BINAIRE);
hIn= ::AfxGetResourceHandle();
tSrc= ::FindResource(hIn,RSNumb.GetBuffer(0),"BINARY");
rsize= ::SizeofResource(hModule,hRes);
hMem= ::LoadResource(hModule,hRes);
if (hMem)
{
BYTE *pData = (BYTE *) ::LockResource(hMem);
if (pData)
{
}
}
else
{
AfxmessageBox("Unable to load resource!");
}
|
|
| | auteur : Farscape | l'exemple ci-dessous attache directement une ressouce Bitmap (IDB_BITMAP1) à l'objet ImageList.
CImageList ImageList;
ImageList.Attach(ImageList_LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1), 35, 1, RGB(255, 0, 255)));
|
|
| | auteur : Farscape | Depuis Visual.Net nous disposons de la classe CImage permettant des manipulations sur les bitmaps, notamment la fonction SetPixel.
Exemple:
CBitmap Bmp;
CImage Image;
int i;
Bmp.LoadBitmap(IDB_MON_IMAGE);
Image.Attach(Bmp, Image.DIBOR_DEFAULT);
for(int i=0; i<16;i++)
{
Image.SetPixel(99,i,RGB(0,0,0));
}
|
|
| | auteur : matazz | En utilisant les 2 fonctions précédentes :
BOOL WriteWindowToDIB( LPTSTR szFile, CWnd *pWnd )
{
CBitmap bitmap;
CWindowDCdc(pWnd);
CDC memDC;
CRectrect;
memDC.CreateCompatibleDC(&dc);
pWnd->GetWindowRect(rect);
bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY);
CPalette pal;
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries =
GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );
pal.CreatePalette( pLP );
delete[] pLP;
}
memDC.SelectObject(pOldBitmap);
HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, &pal );
if( hDIB == NULL )return FALSE;
WriteDIB( szFile, hDIB );
GlobalFree( hDIB );
return TRUE;
}
|
|
| | auteur : Farscape | pour savoir si un CBitmap est déjà initialisé, on testera la valeur du handle GDI attaché, ici un HBITMAP.
if(!static_cast<HBITMAP>(m_bitmap))
m_bitmap.CreateCompatibleBitmap( dc, rcClient.Width(), rcClient.Height() );
|
|
Consultez les autres F.A.Q.
 
|
|