Pbr: afficher une image

neofenix Messages postés 145 Date d'inscription mercredi 12 avril 2006 Statut Membre Dernière intervention 11 mars 2010 - 14 avril 2006 à 09:26
neofenix Messages postés 145 Date d'inscription mercredi 12 avril 2006 Statut Membre Dernière intervention 11 mars 2010 - 19 avril 2006 à 13:11
salut tout le monde voila je pense que mon code est bon mais kan je
lance le debug et que j'insere le point d'arret juste apres le
LoadImage la variable HBitmap ne s'initialise pas ce qui fait que
ne n'arrive pas a afficher mon image voila merci

mon code:



include <windows.h>



LRESULT CALLBACK MainProc(HWND hWnd, UINT messages, WPARAM wParam, LPARAM lParam)



{



switch (messages)



{







case WM_PAINT:



{



HDC hdc;



POINT pt;



HBITMAP hBitmap;



HDC hMemDC;



PAINTSTRUCT ps;



BITMAP bm;



hdc = BeginPaint(hWnd, &ps);


hBitmap = (HBITMAP)
LoadImage( NULL, "d:\\but_valid.bmp", IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE);



hMemDC = CreateCompatibleDC (hdc);



SelectObject (hMemDC, hBitmap);



GetObject (hBitmap, sizeof (BITMAP), &bm) ;



pt.x = bm.bmWidth;



pt.y = bm.bmHeight;



BitBlt (hdc, 0, 0, pt.x, pt.y, hMemDC, 0, 0, SRCCOPY) ;



EndPaint (hWnd, &ps);



break;



}







case WM_CLOSE:



case WM_DESTROY:



PostQuitMessage(0);



break;



}



return DefWindowProc(hWnd, messages, wParam, lParam);



}







int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)



{



WNDCLASSEX main;



main.cbSize = sizeof(WNDCLASSEX);



main.style = CS_HREDRAW|CS_VREDRAW;



main.lpfnWndProc = MainProc;



main.cbClsExtra = 0;



main.cbWndExtra = 0;



main.hInstance = hInstance;



main.hIcon = LoadIcon(hInstance, "APPICON");



main.hIconSm = LoadIcon(hInstance, "WINICON");



main.hCursor = LoadCursor(NULL, IDC_ARROW);



main.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);



main.lpszMenuName = NULL;



main.lpszClassName = "std";



RegisterClassEx(&main);



HWND hWnd;



hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "std", "Installation",
WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX, CW_USEDEFAULT,
CW_USEDEFAULT, 609, 429, NULL, NULL, hInstance, NULL);



ShowWindow(hWnd, SW_SHOW);



MSG messages;







while(GetMessage(&messages, NULL, 0, 0) == TRUE)



{



TranslateMessage(&messages);



DispatchMessage(&messages);



}







return 0;



}

30 réponses

cs_chris91 Messages postés 54 Date d'inscription jeudi 20 novembre 2003 Statut Membre Dernière intervention 4 avril 2007
16 avril 2006 à 23:07
salut, il est pas mal ce dernier exemple...

HDC hMemDC;
HDC hdc;
HBITMAP hBitmap;
BITMAP bm;

hdc = GetDC(hWnd);
hMemDC = CreateCompatibleDC(hdc);

hBitmap = (HBITMAP) LoadImage( NULL, "c:\\test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
GetObject(hBitmap, sizeof(BITMAP), &bm) ;

hBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
BitBlt (hdc, 0, 0, bm.bmWidth, bm.bmWidth, hMemDC, 0, 0, SRCCOPY);

DeleteObject(SelectObject(hMemDC, hBitmap));

DeleteDC(hMemDC);
DeleteDC(hdc);

InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);

exemple testé.
0
cs_chris91 Messages postés 54 Date d'inscription jeudi 20 novembre 2003 Statut Membre Dernière intervention 4 avril 2007
16 avril 2006 à 23:09
si encore un problème, faire le test avec une image bmp avec mspaint...bye.
0
neofenix Messages postés 145 Date d'inscription mercredi 12 avril 2006 Statut Membre Dernière intervention 11 mars 2010
18 avril 2006 à 10:19
alor chris91 j'ai testé ton code mais il ne marchait pas donc j'ai
bidouillé un peu et maintenant j'arrive a afficher mon bitmap mais hors
de la fenetre ( je dois faire hdc = GetDC(0);) donc si quelqu'un sait
comment faire... merci
0
cs_chris91 Messages postés 54 Date d'inscription jeudi 20 novembre 2003 Statut Membre Dernière intervention 4 avril 2007
18 avril 2006 à 16:35
salut,
je te confirme que ce code fonctionne sans modification.
évidemment que l'image est en dehors de la fenêtre :
MSDN :
"If this value is NULL, GetDC retrieves the DC for the entire screen"
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
neofenix Messages postés 145 Date d'inscription mercredi 12 avril 2006 Statut Membre Dernière intervention 11 mars 2010
18 avril 2006 à 16:56
je suis daccord pour le GetDc sauf que lorsque je veux la mettre dans une fenetre l'immage ne s'affiche plus :s
0
cs_chris91 Messages postés 54 Date d'inscription jeudi 20 novembre 2003 Statut Membre Dernière intervention 4 avril 2007
18 avril 2006 à 17:45
exemple complet avec la fonction WndProc :

HDC hMemDC;
HBITMAP hBitmap;
BITMAP bm;

int WINAPI WinMain(...)
{
.
.
}

LRESULT CALLBACK WndProc(HWND hWnd, ...)
{
HDC hdc;
PAINTSTRUCT ps;

switch(message)
{
case WM_CREATE:
hdc = GetDC(hWnd);
hMemDC = CreateCompatibleDC(hdc);
DeleteDC(hdc);

hBitmap = (HBITMAP) LoadImage( NULL, "c:\\test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
GetObject(hBitmap, sizeof(BITMAP), &bm) ;

hBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);

return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

BitBlt (hdc, 0, 0, bm.bmWidth, bm.bmWidth, hMemDC, 0, 0, SRCCOPY);

EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:

DeleteObject(SelectObject(hMemDC, hBitmap));
DeleteDC(hMemDC);

PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;
}
0
Taron31 Messages postés 199 Date d'inscription vendredi 16 avril 2004 Statut Membre Dernière intervention 28 février 2008
18 avril 2006 à 19:22
chris91 : y'a pas un problème dans le code puisque tu DeleteDC(hdc) dans WM_CREATE et après tu l'utilises dans WM_PAINT ?? Et d'abord on utilise pas ReleaseDC lorsqu'on a récuperé un DC avec GetDC(HWND) ??

___________________
MVS - Most Valuable Student ( Microsoft)
0
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
18 avril 2006 à 21:29
GetDC réussi => ReleaseDC

ciao...
BruNews, MVP VC++
0
cs_chris91 Messages postés 54 Date d'inscription jeudi 20 novembre 2003 Statut Membre Dernière intervention 4 avril 2007
18 avril 2006 à 21:35
ahhhhh!!!
oui, c'est vrai avec GetDC(hWnd); c'est ReleaseDC(hWnd, hdc);
petite erreur de copier coller...

pour le reste, non c'est normal...création d'un HDC à la création de la fenêtre de l'appli.
utilisation de GetDC pour avoir un HDC compatible.

le message WM_CREATE est seulement à la création de la fenêtre.
dès le moindre petit recouvrement de la fenêtre de l'appli, il y a un message WM_PAINT.

cela évite simplement d'avoir le chargement de l'image à chaque fois (méthode du back buffer).
0
neofenix Messages postés 145 Date d'inscription mercredi 12 avril 2006 Statut Membre Dernière intervention 11 mars 2010
19 avril 2006 à 13:11
merci c cool sa marche une petie erreur d'inattention
0
Rejoignez-nous