Bild in der Picturebox aktualisieren



  • Servus,
    ich bin grad dabei ein Bildanzeigeprogramm zu schreiben und bin dabei auf den Sourcecode gestoßen, wie man jpgs... einbindet(http://www.c-plusplus.net/forum/viewtopic-var-t-is-39384.html). Alledings weiß ich jetzt nicht, wie man eine Picturebox aktualisiert und ich weiß nicht, wo man das machen muss. Hier ist mein aktueller Quelltext:

    #include <windows.h> 
    #include <olectl.h> 
    #include <string.h> 
    
    class COlePicture { 
    public: 
        COlePicture() : pPicture(NULL) { 
        } 
        ~COlePicture() { 
            if(pPicture != NULL) {
                pPicture->Release();     
            } 
        } 
        BOOL Load(LPCTSTR szFile) {
    
            HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); 
            if(hFile == INVALID_HANDLE_VALUE) { 
                return FALSE; 
            } 
    
            DWORD dwFileSize = GetFileSize(hFile, NULL); 
            if(dwFileSize == -1) { 
                return FALSE; 
            } 
    
            LPVOID pvData = NULL; 
            HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); 
            if(hGlobal == NULL) { 
                return FALSE; 
            } 
    
            pvData = GlobalLock(hGlobal); 
            if(pvData == NULL) { 
                return FALSE; 
            } 
    
            DWORD dwBytesRead = 0; 
            BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL); 
            if(bRead == FALSE) { 
                return FALSE; 
            } 
            GlobalUnlock(hGlobal); 
            CloseHandle(hFile); 
    
            LPSTREAM pstm = NULL; 
            HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm); 
    
            if(FAILED(hr) || pstm == NULL) { 
                return FALSE; 
            } 
    
            if(pPicture) { 
                pPicture->Release(); 
            } 
            hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID*)&pPicture); 
            if(FAILED(hr) || pPicture == NULL) { 
                return FALSE; 
            }   
            pstm->Release(); 
            szFile="";
    
            return TRUE; 
        } 
    
        VOID Show(HWND hWindowHandle, LONG nX, LONG nY) { 
            HDC hDC = GetDC(hWindowHandle); 
            if(pPicture) { 
                LONG hmWidth; 
                LONG hmHeight; 
                pPicture->get_Width(&hmWidth); 
                pPicture->get_Height(&hmHeight); 
                INT nWidth  = MulDiv(hmWidth, GetDeviceCaps(hDC, LOGPIXELSX), 2540); 
                INT nHeight = MulDiv(hmHeight, GetDeviceCaps(hDC, LOGPIXELSY), 2540); 
                RECT rc; 
                GetClientRect(hWindowHandle, &rc); 
                pPicture->Render(hDC, nX, nY, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc); 
            } 
        } 
    private: 
        LPPICTURE pPicture; 
    }; 
    
    LRESULT CALLBACK MainWindowProcedure(HWND hWindowHandle, 
                                         UINT uMessage, 
                                         WPARAM wParam, 
                                         LPARAM lParam); 
    
    LRESULT CALLBACK PictureBoxProcedure(HWND hWindowHandle, 
                                         UINT uMessage, 
                                         WPARAM wParam, 
                                         LPARAM lParam);
    
    HWND        hButton1;
    HWND        hText1;
    HWND        hText2;
    HWND        hzurueck;
    HWND        hvor;
    
    RECT rc; 
    
    HINSTANCE hInst;
    
    struct Daten{
           char Name[900];
           char nName[900];
           };
    
    struct Daten Datei[800];
    
    char *buffer = NULL;
    char string[900];
    
    int i;
    int o=0;
    int ja=0;
    
    HANDLE fHandle; 
    WIN32_FIND_DATA wfd;
    DWORD code; 
    
    HBITMAP hBmp;
    HDC DC; 
    HDC MemDC; 
    PAINTSTRUCT ps;
    
    float hh;
    float hw;
    
    float xx=900;
    float yy=750;
    
    char z[900];
    
    char Oeffnung[900];
    char Ordnername[900]; 
    
    HWND hPictureBox2; 
    
    char Mal[]={"mspaint.exe "};
    
    int WINAPI WinMain(HINSTANCE hInstanceHandle, 
                       HINSTANCE hPreviousInstanceHandle, 
                       LPSTR lpszCommandLine, 
                       INT nShowState) { 
    
        WNDCLASSEX MainWindowClass = {0}; 
        MainWindowClass.cbSize = sizeof(WNDCLASSEX); 
        MainWindowClass.hInstance = hInstanceHandle; 
        MainWindowClass.lpszClassName = "Bild"; 
        MainWindowClass.lpfnWndProc = MainWindowProcedure; 
        MainWindowClass.style = CS_HREDRAW | CS_VREDRAW; 
        MainWindowClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE); 
        MainWindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
        MainWindowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
        MainWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW); 
        MainWindowClass.lpszMenuName = NULL; 
        MainWindowClass.cbClsExtra = 0; 
        MainWindowClass.cbWndExtra = 0; 
    
        if(RegisterClassEx(&MainWindowClass) == FALSE) { 
            MessageBox(NULL, "Can't register MainWindowClass!", 
                "Information", MB_OK); 
            return EXIT_FAILURE; 
        } 
    
        //strcpy(Datei[0].Name,"C:\\test1.jpg");
    
        WNDCLASSEX PictureBoxWindowClass = {0}; 
        PictureBoxWindowClass.cbSize = sizeof(WNDCLASSEX); 
        PictureBoxWindowClass.hInstance = hInstanceHandle; 
        PictureBoxWindowClass.lpszClassName = "PictureBox"; 
        PictureBoxWindowClass.lpfnWndProc = PictureBoxProcedure; 
        PictureBoxWindowClass.style = CS_HREDRAW | CS_VREDRAW; 
        PictureBoxWindowClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE); 
        PictureBoxWindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
        PictureBoxWindowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
        PictureBoxWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW); 
        PictureBoxWindowClass.lpszMenuName = NULL; 
        PictureBoxWindowClass.cbClsExtra = 0; 
        PictureBoxWindowClass.cbWndExtra = 0; 
    
        if(RegisterClassEx(&PictureBoxWindowClass) == FALSE) { 
            MessageBox(NULL, "Can't register PictureBoxWindowClass!", 
                "Information", MB_OK); 
            return EXIT_FAILURE; 
        } 
    
        HWND hMainWindow = CreateWindowEx( 
            0, "Bild", "PictureViewer", 
            WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
            CW_USEDEFAULT, CW_USEDEFAULT, 
            CW_USEDEFAULT, CW_USEDEFAULT, 
            HWND_DESKTOP, NULL, 
            GetModuleHandle(NULL), NULL); 
    
        if(hMainWindow == NULL) { 
            MessageBox(NULL, "Can't create main window!", "Information", MB_OK); 
            return EXIT_FAILURE; 
        } 
    
       hPictureBox2 = CreateWindowEx( 
            0, "PictureBox", Datei[o].Name, 
            WS_CHILD | WS_VISIBLE, 
            10, 120, 
            500, 500, 
            hMainWindow, (HMENU) 1001, 
            GetModuleHandle(NULL), NULL); 
    
        if(hPictureBox2 == NULL) { 
            MessageBox(NULL, "Can't create PictureBox 2!", "Information", MB_OK); 
            return EXIT_FAILURE; 
        } 
    
        hText1 = CreateWindow("STATIC", "Von welchem Ordner wollen Sie die Bilder anschauen:",
                        WS_VISIBLE | WS_CHILD, 10, 20, 300, 20, hMainWindow, NULL, hInstanceHandle, NULL);
        if(!hText1) return FALSE;
    
        hText2 = CreateWindow("STATIC", "",
                        WS_VISIBLE | WS_CHILD, 350, 80, 300, 20, hMainWindow, NULL, hInstanceHandle, NULL);
        if(!hText2) return FALSE;
    
        hButton1 = CreateWindow("BUTTON", "Bilder anschauen",
                           WS_VISIBLE | WS_CHILD, 350, 40, 200, 20,
                           hMainWindow, NULL, hInstanceHandle, NULL);
        if(!hButton1) return FALSE;
    
        hzurueck = CreateWindow("BUTTON", "letztes Bild",
                           WS_VISIBLE | WS_CHILD, 50, 80, 200, 20,
                           hMainWindow, NULL, hInstanceHandle, NULL);
        if(!hzurueck) return FALSE;
    
        hvor = CreateWindow("BUTTON", "nächstes Bild",
                           WS_VISIBLE | WS_CHILD, 650, 80, 200, 20,
                           hMainWindow, NULL, hInstanceHandle, NULL);
        if(!hvor) return FALSE;
    
        SendMessage(hText1, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
        SendMessage(hText2, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);    
        SendMessage(hButton1, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
        SendMessage(hzurueck, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
        SendMessage(hvor, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
    
        hInst = hInstanceHandle;
    
        MSG Message = {0}; 
        while(GetMessage(&Message, NULL, 0, 0)) { 
            TranslateMessage(&Message); 
            DispatchMessage(&Message); 
        } 
        return Message.wParam; 
    } 
    
    LRESULT CALLBACK MainWindowProcedure(HWND hWindowHandle, 
                                         UINT uMessage, 
                                         WPARAM wParam, 
                                         LPARAM lParam) { 
        static HWND hEdit;
    
        switch(uMessage) {
    
        case WM_CREATE: 
            { 
                hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
                                       "edit",
                                       buffer,    
                                       WS_CHILD | WS_VISIBLE,
                                       10, 40, 300, 20,
                                       hWindowHandle,
                                       NULL,
                                       ((LPCREATESTRUCT) lParam) -> hInstance,
                                       NULL);
    
                free(buffer);
                SendMessage(hEdit, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);    
            }                 
    
        case WM_COMMAND:
                            switch(LOWORD(wParam)) { 
                                                   case 1001: 
    
                                                   STARTUPINFO si;
    	                                           PROCESS_INFORMATION pi;
    
    	                                           ZeroMemory( &si, sizeof(si) );
    	                                           si.cb = sizeof(si);
    	                                           ZeroMemory( &pi, sizeof(pi) );
    	                                           strcat(Mal,Datei[o].Name);
    
                                                   CreateProcess( NULL,Mal, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);  
                                                   strcpy(Mal,"mspaint.exe "),
                                                   CloseHandle( pi.hThread );
                                                   CloseHandle( pi.hProcess );
                                                   break; 
                                                   } 
    
                            i=0;
                            if(lParam == (LPARAM)hButton1)        
                            {
                                      ja=1;
                                      int iLength;
    
                                      iLength = GetWindowTextLength(hEdit);
                                      buffer = (char*) malloc(iLength);
                                      GetWindowText(hEdit, buffer, iLength+1);
                                      strcpy(Ordnername,buffer);
                                      strcpy(Oeffnung,Ordnername);
                                      strcat(Oeffnung,"\\*");
    
                                      fHandle=FindFirstFile(Oeffnung,&wfd); 
    
                                      do 
                                      { 
                                                                            if (!( (wfd.cFileName[0]=='.') && ( (wfd.cFileName[1]=='.' && wfd.cFileName[2]==0) || wfd.cFileName[1]==0 ) )) 
                                                                            { 
                                                                                   if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
                                                                                   { 
                                                                                       continue;
                                                                                   } 
                                                                                   else 
                                                                                   {   
                                                                                        if(strstr(wfd.cFileName,".bmp")||strstr(wfd.cFileName,".BMP")||strstr(wfd.cFileName,".jpg")||strstr(wfd.cFileName,".JPG"))
                                                                                        {                    
                                                                                                             strcpy(Datei[i].Name,Ordnername);
                                                                                                             strcat(Datei[i].Name,"\\\\");
                                                                                                             strcat(Datei[i].Name,wfd.cFileName);
                                                                                                             strcpy(Datei[i].nName,wfd.cFileName);
                                                                                                             i++;
                                                                                        }
                                                                                   }    
                                                                            } 
                                      }while (FindNextFile(fHandle,&wfd)); 
                                      FindClose(fHandle);
                                      i--;
                                      o=0;
                                      SendMessage(hText2, WM_SETTEXT, 0,(LPARAM)Datei[0].nName);
                                      ja=1;
    
                                      UpdateWindow(hWindowHandle);
                                      UpdateWindow(hPictureBox2);
                                      InvalidateRect(hWindowHandle, NULL, TRUE); 
                                      GetClientRect(hWindowHandle,&rc); 
                                      RedrawWindow(hWindowHandle,&rc,NULL, RDW_ERASE | RDW_INVALIDATE);
                            } 
                      if(lParam == (LPARAM)hzurueck)        
                      {
                                o--;
                                SendMessage(hText2, WM_SETTEXT, 0,(LPARAM)Datei[o].nName);
    
                                UpdateWindow(hWindowHandle);
                                UpdateWindow(hPictureBox2);
                                InvalidateRect(hWindowHandle, NULL, TRUE); 
                                GetClientRect(hWindowHandle,&rc); 
                                RedrawWindow(hWindowHandle,&rc,NULL, RDW_ERASE | RDW_INVALIDATE);
                      }
                      if(lParam == (LPARAM)hvor)        
                      {
                                o++;
                                SendMessage(hText2, WM_SETTEXT, 0,(LPARAM)Datei[o].nName);
    
                                UpdateWindow(hWindowHandle);
                                UpdateWindow(hPictureBox2);
                                InvalidateRect(hWindowHandle, NULL, TRUE); 
                                GetClientRect(hWindowHandle,&rc); 
                                RedrawWindow(hWindowHandle,&rc,NULL, RDW_ERASE | RDW_INVALIDATE);
                      } 
            break; 
            case WM_DESTROY: 
                PostQuitMessage(0); 
                break; 
            default: 
                return DefWindowProc(hWindowHandle, uMessage, wParam, lParam); 
        } 
        return 0; 
    } 
    
    LRESULT CALLBACK PictureBoxProcedure(HWND hWindowHandle, 
                                         UINT uMessage, 
                                         WPARAM wParam, 
                                         LPARAM lParam) { 
        switch(uMessage) { 
        case WM_CREATE: 
            { 
                COlePicture* pPicture = new COlePicture; 
                CHAR WindowText[MAX_PATH]; 
                GetWindowText(hWindowHandle, WindowText, MAX_PATH);
                pPicture->Load(Datei[o].Name); 
                SetWindowLong(hWindowHandle, GWL_USERDATA, (LONG) pPicture);
    
            } 
            break; 
        case WM_DESTROY: 
            { 
                COlePicture* pPicture = (COlePicture*) GetWindowLong(hWindowHandle, GWL_USERDATA); 
                if(pPicture != NULL) { 
                    delete pPicture; 
                } 
            } 
            break; 
        case WM_LBUTTONDOWN: 
            SendMessage(GetParent(hWindowHandle), WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(hWindowHandle), 0), (LPARAM)hWindowHandle);
            break; 
        case WM_PAINT: 
            { 
                UpdateWindow(hWindowHandle);
                UpdateWindow(hPictureBox2);
                InvalidateRect(hWindowHandle, NULL, TRUE); 
                GetClientRect(hWindowHandle,&rc); 
                RedrawWindow(hWindowHandle,&rc,NULL, RDW_ERASE | RDW_INVALIDATE);
    
                PAINTSTRUCT ps; 
                HDC hDC = BeginPaint(hWindowHandle, &ps); 
                COlePicture* pPicture = (COlePicture*) GetWindowLong(hWindowHandle, GWL_USERDATA); 
                if(pPicture != NULL) {
                    pPicture->Show(hWindowHandle, 0, 0); 
                } 
                EndPaint(hWindowHandle, &ps); 
            } 
            break; 
        default: 
            return DefWindowProc(hWindowHandle, uMessage, wParam, lParam); 
        } 
        return 0; 
    }
    

    Danke,

    Felix



  • Was meinst Du mit aktualisieren? InvalidateRect? Vielleicht reduzierst Du den Code mal auf das Wesentliche...



  • Funktionsweise meines Programmes:
    Man gibt einen Ordner an. Mit dem Programm kann man dann die Bilder nach einander betrachten.

    Das Problem ist, dass das Programm die Bilder nicht anzeigt, obwohl diese gefunden werden.

    pPicture->Load(Datei[o].Name);
    

    Diese Zeile wird nicht aktualisiert, weil wenn ich den Code so umändere:

    pPicture->Load("C:\\test1.jpg");
    

    dann wird die ganze Zeit dieses Bild angezeigt.

    InvalidateRect: Wo müsste ich diese Funktion hinschreiben, damit dieses Problem gelöst wird?



  • Eij, ei, ei...was ein Spaghetti-Code 😮 .

    Also, Du solltest Dir erstmal globale Variablen abgewöhnen bzw. nur da verwenden, wo das wirklich nötig ist - sonst nie! Des weiteren sollte man nur da dynamisch allokieren, wenn es auch dynamisch sein muss; kennst Du das Schlüsselwort static? (vergleiche folgende Codeabschnitte und mach Dir darüber nochmal Gedanken 😉 :
    1.

    COlePicture* pPicture = new COlePicture;
    // ...
    COlePicture* pPicture = (COlePicture*) GetWindowLong(hWindowHandle, GWL_USERDATA);
    if(pPicture != NULL) {
        delete pPicture;
    }
    

    2.

    struct Daten{
           char Name[900];
           char nName[900];
           };
    
    struct Daten Datei[800];
    

    ). Dies hat aber nichts mit Deinem Fehler zu tun.

    Also, wenn ich nichts übersehen habe, würde ich sagen, der Fehler liegt hier (in der Funktion 'PictureBoxProcedure'):

    case WM_CREATE:
            {
                COlePicture* pPicture = new COlePicture;
                CHAR WindowText[MAX_PATH];
                GetWindowText(hWindowHandle, WindowText, MAX_PATH);
                pPicture->Load(Datei[o].Name);
                SetWindowLong(hWindowHandle, GWL_USERDATA, (LONG) pPicture);
    
            }
            break;
    

    Der WM_CREATE Handler wird nur einmal beim Erstellen des Controls aufgerufen. Damit wird das Objekt ('pPicture') der Klasse 'COlePicture' auch nur einmal mit der Bilddatei initialisiert. Lass den murks einfach raus und ändere den WM_PAINT-Handler wie folgt ab:

    case WM_PAINT:
            {
                /*
                UpdateWindow(hWindowHandle);
                UpdateWindow(hPictureBox2);
                InvalidateRect(hWindowHandle, NULL, TRUE);
                GetClientRect(hWindowHandle,&rc);
                RedrawWindow(hWindowHandle,&rc,NULL, RDW_ERASE | RDW_INVALIDATE);
                */
                PAINTSTRUCT ps;
                BeginPaint(hWindowHandle, &ps);
                COlePicture Image;
                Image.Load(Datei[o].Name);
                Image.Show(hWindowHandle, 0, 0);
                EndPaint(hWindowHandle, &ps);
            }
            break;
    

    PS: InvalidateRect und Kammerade in WM_PAINT zu verwenden ist nicht nur schwachsinnig, sondern eigentlich auch falsch (->Endlosschleife).



  • Jetzt klappt's. Vielen vielen Dank CodeFinder. 👍


Anmelden zum Antworten