2011-05-04 10 views
1

je suis en train de montrer un bmp dans l'écran en utilisant ce code: (ce code est d'un livre, mais je ne peux pas montrer les fenêtres quand je Compile parais que la fenêtre blanche)Chargement et peinture bmp win32api C++

#include "resource2.h" 
#include <vector> 
#include <windows.h> 

using namespace std; 

HWND window1; 
HWND window2; 
HINSTANCE happ; 
HDC handledevice; 
PAINTSTRUCT ps; 
HBITMAP hbitmap; 
BITMAP bitmap; 
HDC bmhdc; 
HBITMAP oldbm; 

LRESULT CALLBACK WindTyp1(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) 
{ 
    switch (msg) 
    { 
     case WM_KEYDOWN: 
      if (wparam==VK_ESCAPE) 
      { 
       DestroyWindow(window1); 
      } 
      return 0; 
     case WM_PAINT: 
      handledevice=BeginPaint(hwnd,&ps); 
      BitBlt(handledevice,0,0,bitmap.bmWidth,bitmap.bmHeight,bmhdc,0,0,SRCCOPY); 
      EndPaint(hwnd,&ps); 
      return 0; 
    } 
    return DefWindowProc(hwnd,msg,wparam,lparam); 
} 

LRESULT CALLBACK WindTyp2(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam) 
{ 
    switch (msg) 
    { 
     case WM_KEYDOWN: 
      if (wparam==VK_F1) 
      { 
       DestroyWindow(window1); 
      } 
      if (wparam==VK_F2) 
      { 
       DestroyWindow(window2); 
      } 
      if (wparam==VK_ESCAPE) 
      { 
       DestroyWindow(window1); 
       DestroyWindow(window2); 
      } 

    } 
    return DefWindowProc(hwnd,msg,wparam,lparam); 
} 

int WINAPI WinMain(HINSTANCE histance,HINSTANCE hprevinstance,PSTR cmdline,int showcmd) 
{ 
    happ=histance; 
    MSG msg; 
    hbitmap=LoadBitmap(happ,MAKEINTRESOURCE(happ,IDB_BITMAP1)); 
    GetObject(hbitmap,sizeof(BITMAP),&bitmap); 
    bmhdc=CreateCompatibleDC(handledevice); 
    SelectObject(bmhdc,&bitmap); 

    //clase 1 
    WNDCLASS windowstyle1,windowstyle2; 
    windowstyle1.cbClsExtra=0; 
    windowstyle1.cbWndExtra=0; 
    windowstyle1.hbrBackground=(HBRUSH) ::GetStockObject(WHITE_BRUSH); 
    windowstyle1.hCursor=::LoadCursor(0,IDC_ARROW); 
    windowstyle1.hIcon=::LoadIcon(0,IDI_APPLICATION); 
    windowstyle1.hInstance=histance; 
    windowstyle1.lpfnWndProc=WindTyp1; 
    windowstyle1.lpszClassName="Class 1"; 
    windowstyle1.lpszMenuName=0; 
    windowstyle1.style= CS_HREDRAW | CS_VREDRAW; 

    //clase 2 
    windowstyle2.cbClsExtra=0; 
    windowstyle2.cbWndExtra=0; 
    windowstyle2.hbrBackground=(HBRUSH) ::GetStockObject(BLACK_BRUSH); 
    windowstyle2.hCursor=::LoadCursor(0,IDC_ARROW); 
    windowstyle2.hIcon=::LoadIcon(0,IDI_APPLICATION); 
    windowstyle2.hInstance=histance; 
    windowstyle2.lpfnWndProc=WindTyp2; 
    windowstyle2.lpszClassName="Class 2"; 
    windowstyle2.lpszMenuName=0; 
    windowstyle2.style= CS_HREDRAW | CS_VREDRAW; 

    //registrar ambas clases 
    RegisterClass(&windowstyle1); 
    RegisterClass(&windowstyle2); 

    //crear ventanas 
    window1=::CreateWindow("Class 1","Ventana Blanca",WS_OVERLAPPEDWINDOW,0,0,1400,1000,0,0,happ,0); 
    if (window1==0) 
     ::MessageBox(0,"error failed to create window","error",0); 

    //Show & Update 
    ShowWindow(window1,true); 
    UpdateWindow(window1); 

    //Message Loop 
    ZeroMemory(&msg,sizeof(MSG)); 
    while (GetMessage(&msg,0,0,0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
} 

Répondre

2

Jetez un oeil à cet exemple ici http://www.functionx.com/win32/Lesson13.htm

bmhdc=CreateCompatibleDC(handledevice); 

EN WinMain vous utilisez handledevice - qui n'a pas encore initialisés, donc sélectionner le bitmap causerait des problèmes, mais il est probablement 0, il utiliserait le bureau DC. Habituellement, je ne le ferais pas dans WinMain mais dans la peinture comme dans l'exemple de l'URL.

HTH

0

Vous rendez la vie plus dure pour vous que nécessaire. La façon dont vous êtes censé faire cela est avec une fenêtre STATIC avec le style SS_BITMAP.

Je réalise que ce n'est pas une réponse indirecte à votre question mais je l'offre car cela rendra votre tâche beaucoup plus simple - laissez le système faire le gros du travail pour vous!

+0

belle mais je fais cela parce que je suis auto-apprentissage si je charge le bmp, quelle est la fonction pour afficher dans la fenêtre du bmp ??? (Désolé pour mon mauvais anglais) – Rolilink