Afficher une bitmap

Description

Voila une petite source la plus courte possible pour afficher une bitmap.

Source / Exemple :


#include <windows.h>
#include "resource.h"
 
//----------------------------------------------------------
//	MAIN WNDPROC
//----------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
static int      cxClient,cyClient,cxImage,cyImage;
static HDC      hdcCompatible;
static HBITMAP  bmp,oldBmp;

switch (iMsg)
  {
  case WM_CREATE:
    {
    HDC     hdc;
    BITMAP  infoBmp;

    hdc = GetDC(hwnd);
    
    hdcCompatible = CreateCompatibleDC(hdc); 

    if(NULL == (bmp = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(ID_BMP))))
      {
      return -1;
      }

    if(0 == GetObject(bmp,sizeof(BITMAP),(LPVOID)(&infoBmp)))
      {
      return -1;
      }

    cxImage = infoBmp.bmWidth;
    cyImage = infoBmp.bmHeight;

    oldBmp = SelectObject(hdcCompatible,bmp);

    ReleaseDC(hwnd,hdc);
	  
    return 0;
    }

  case WM_SIZE:
    {
    cxClient = LOWORD(lParam);
    cyClient = HIWORD(lParam);

    InvalidateRect(hwnd,NULL,TRUE);
    return 0;
    }
  case WM_PAINT:
    {
    HDC		        hdc;
    PAINTSTRUCT   ps;

	  hdc = BeginPaint(hwnd,&ps);

    if(!BitBlt(
               hdc,
               (cxClient - cxImage)/2,
               (cyClient - cyImage)/2,
               cxImage,
               cyImage,
               hdcCompatible,
               0,
               0,
               SRCCOPY
               ))
      {
      PostQuitMessage(1);
      }

	  EndPaint(hwnd,&ps);
    return 0;
    }
  case WM_DESTROY:
    {
    SelectObject(hdcCompatible,oldBmp);
    DeleteObject(bmp);
    DeleteDC(hdcCompatible);

    PostQuitMessage(0);
	  return 0;
    }
  }

return DefWindowProc(hwnd,iMsg,wParam,lParam);
} // MainWndProc()
//----------------------------------------------------------
//	WIN MAIN
//----------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow)
{
MSG           msg;
HWND          hwnd;
WNDCLASSEX    wndclass;

wndclass.cbSize        = sizeof(wndclass);
wndclass.style         = 0;
wndclass.lpfnWndProc   = MainWndProc;
wndclass.cbClsExtra    = 0;
wndclass.cbWndExtra    = 0;
wndclass.hInstance     = hInstance;
wndclass.hIcon         = NULL;
wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
wndclass.lpszMenuName  = NULL;
wndclass.lpszClassName = "JCD_BITMAP";
wndclass.hIconSm       = NULL;

if(0 == RegisterClassEx(&wndclass))
  {
  return -1;
  }

if(NULL == (hwnd = CreateWindow("JCD_BITMAP","Comment afficher une Bitmap ...",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL)))
  {
  return -1;
  }

ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))
	{
	TranslateMessage(&msg);
	DispatchMessage(&msg);
	}
return 0;
} // WinMain()

Conclusion :


Une pensée spéciale pour Glipper ... :)

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.