在 Visual c++ 中显示图像窗口应用程序

发布于 2024-12-11 21:02:58 字数 179 浏览 0 评论 0原文

我是 Visual C++ Windows 应用程序编程的初学者,我想在屏幕上显示图像,但我不知道如何。谁能帮助我吗?

我在网上找到的大部分内容都是关于 MFC 应用程序的,我对此不感兴趣。 我希望这个应用程序尽可能简单,因此我对使用任何其他库(如 OpenCV)或使用 Direct2D、OpenGl 不感兴趣也就不足为奇了。

I am a beginner to visual c++ Windows application programming and I want to display an image on the screen but I don't know how. Can anyone help me?

Most of the stuff I found on the net was about MFC application which I'm not interested in.
I want this application to be as simple as possible so it's not surprise that I'm not interested in using any additional library like OpenCV or use of Direct2D, OpenGl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-12-18 21:02:58

BitBlt,在 MSDN 上查找。另请参阅http://www.winprog.org/tutorial/bitmaps.html

BitBlt, look it up on MSDN. Also see, http://www.winprog.org/tutorial/bitmaps.html.

泪眸﹌ 2024-12-18 21:02:58

只需使用以下代码即可。它很丑陋,但足以用于调试

#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;
}

Just use the following code. It is ugly but works good enough for debug

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文