2011-10-23 6 views
0

Visual C++ Windows 응용 프로그램 프로그래밍 초보자이고 이미지를 화면에 표시하고 싶지만 어떻게 작동하는지 알 수 없습니다. 누구든지 나를 도울 수 있습니까?Visual C++ Windows 응용 프로그램에서 이미지 표시

내가 찾은 대부분의 물건은 내가 관심이없는 MFC 응용 프로그램에 관한 것입니다. 이 응용 프로그램을 가능한 한 간단하게 만들고 싶습니다. 그래서이 추가 응용 프로그램을 추가로 사용하는 데별로 관심이 없습니다 OpenCV와 같은 라이브러리 또는 Direct2D, OpenGl 사용

+1

잘못된 도구를 선택하기를 원한다면 간단하게 사용하십시오. 원시 winapi 간단하지 않습니다. 단순한 MFC 또는 WinForms 또는 VCL 것입니다. –

답변

1

다음 코드를 사용하십시오. 못생긴하지만 디버그를 위해 충분히 작동합니다.

#include <windows.h> 
#include <tchar.h> 

HBITMAP hBitmap; 
HDC localDC; 
HBITMAP hOld; 
BITMAP qB; 
HDC hDC;            // Handle (virtual memory pointer) to drawing characteristics 

LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam){ 
    switch(msg){ 
     case WM_CREATE:{ 
      //MessageBox(hwnd,_T("Window Procedure Received WM_CREATE Message!"),_T("Message Report!"),MB_OK); 
      return 0; 
     } 
     case WM_LBUTTONDOWN: { 
      //MessageBox(hwnd,_T("Window Procedure Received WM_LBUTTONDOWN Message!"),_T("Message Report!"),MB_OK); 
     return 0; 
     } 

     case WM_PAINT: {     //At program start up the whole window is invalid so must be drawn. 
      //TCHAR tmpText[]=_T("TempText"); 
      PAINTSTRUCT ps;  
      hDC = BeginPaint(hwnd,&ps); 
      BOOL qRetBlit = ::BitBlt(hDC,0,0,qB.bmWidth,qB.bmHeight,localDC,0,0,SRCCOPY); 
      // Draw text on top of the image 
      //int iBkMode=SetBkMode(hDC,TRANSPARENT);     // Save Background Mode characteristic of drawing context 
      //TextOut(hDC,40,20,tmpText,(int)_tcslen(tmpText));  // Draw Text 
      //SetBkMode(hDC,iBkMode);          // Return Drawing Context To Original State 
      EndPaint(hwnd, &ps); 
      return 0; 
     } 
     case WM_DESTROY: {      
      ::SelectObject(localDC,hOld); 
      ::DeleteDC(localDC); 
      ::DeleteObject(hBitmap); 
      PostQuitMessage(0); 
      return 0; 
     } 
    } 
    return (DefWindowProc(hwnd, msg, wParam, lParam)); 
} 


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int iShow){ 
    TCHAR szClassName[]=_T("Name"); 
    WNDCLASSEX wc; 
    MSG messages; 
    HWND hWnd; 

    wc.lpszClassName = szClassName;      //Important Field! Character string identifying window class 
    wc.lpfnWndProc = fnWndProc;      //Important Field! Function Pointer. Address of Window Procedure 
    wc.cbSize   = sizeof (WNDCLASSEX);    //Those top two fields I just listed are very important. The 
    wc.style   = 0;        //others are of course necessary too, but fully understanding all 
    wc.hIcon   = LoadIcon(NULL,IDI_APPLICATION); //the implications of the .szClassName and .lpfnWndProc fields will 
    wc.hInstance  = hInstance;      //go a long way to helping you understand Win32 coding. The 
    wc.hIconSm  = 0;        //.hBrushBackground field will be the color of the Window's 
    wc.hCursor  = LoadCursor(NULL,IDC_ARROW);  //background. The .cbWndExtra field is very useful as it allows 
    wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;   //you to associate object (Window) data to the instantiated Window's 
    wc.cbWndExtra  = 0;        //internal structure, i.e., accomodate member data. 
    wc.cbClsExtra  = 0; 
    wc.lpszMenuName = NULL; 
    RegisterClassEx(&wc); 
    hBitmap = (HBITMAP)::LoadImage(NULL, (LPCSTR)"P:/Resources/testing/a.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION); 
    GetObject(reinterpret_cast<HGDIOBJ>(hBitmap),sizeof(BITMAP),reinterpret_cast<LPVOID>(&qB)); 
    localDC = ::CreateCompatibleDC(hDC); 
    hOld = (HBITMAP)::SelectObject(localDC,hBitmap); 

    hWnd = CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,0,0,qB.bmWidth,qB.bmHeight,HWND_DESKTOP,0,hInstance,0); 
    ShowWindow(hWnd,iShow); 
    while(GetMessage(&messages,NULL,0,0)){             
     TranslateMessage(&messages);      
     DispatchMessage(&messages); 
    } 
    return (int)messages.wParam; 
} 
관련 문제