Bitmap (Windows CE)



  • Hallo,
    ich schreibe momentan ein Programm bei dem verschieden Elemente visualisiert werden sollen (Linien, Bitmaps, Rechtecke,...), dazu verwende ich Microsoft eMbedded Visual C++.
    Jetzt habe ich ein Problem. Ich habe in einem Testprogramm ein Bitmap in die Resource geladen und es dann mit der LoadBitmap-Funktion auch darstellen können. Jedoch habe ich jetzt das Problem, dass das Bitmap nicht dargestellt wird. Habe eine Klasse SIControl erstellt, von der die Klassen erben (SILine, SIBitmap,...).
    Die Linien werden gezeichnet und dargestellt nur eben nicht das Bitmap. 😕 😕

    Hier zuerst der Code in dem die Klassen definiert sind:

    // SIControl.cpp: implementation of the SIControl class.
    //
    //////////////////////////////////////////////////////////////////////
    #include "stdafx.h"
    #include <windows.h>
    #include "resource.h"
    #include <Commctrl.h>
    #include "SIControl.h"
    #include "SIBitmap.h"
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    SIControl::SIControl()
    {
    
    }
    
    SIControl::~SIControl()
    {
    
    }
    
    //////////////////////////////////////////////////////////////////////
    // SILine Class
    //////////////////////////////////////////////////////////////////////
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    SILine::SILine(int p_Xstart, int p_Ystart, int p_Xend, int p_Yend, COLORREF  p_penColor,
    			   int p_penWidth, int p_penDashed, int p_Dashlen, 
                                                       int p_Dashbreak)
    {
    	Xstart = p_Xstart;
    	Ystart = p_Ystart;
    	Xend = p_Xend;
    	Yend = p_Yend;
    	penColor = p_penColor;
    	penWidth = p_penWidth;
    	penDashed = p_penDashed ;
    	Dashlen = p_Dashlen;
    	Dashbreak = p_Dashbreak;
    }
    
    SILine::~SILine()
    {
    
    }
    
    BOOL SILine::Paint(HDC hdc)
    {
    
    	HPEN  mypen;
    	if(penDashed == 0){
    		mypen = CreatePen(PS_SOLID,penWidth,penColor);
    	}else{
    		mypen = CreatePen(PS_DASH,1,penColor);
    	}
    	SelectObject(hdc,mypen);
    
    	POINT p[2];
    	p[0].x=Xstart;
    	p[0].y=Ystart;
    	p[1].x=Xend;
    	p[1].y=Yend;
    
    	Polyline(hdc,p,2);
    
    	DeleteObject(mypen);
    	return	TRUE;
    }
    
    //////////////////////////////////////////////////////////////////////
    // SIBitmap Class
    //////////////////////////////////////////////////////////////////////
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    SIBitmap::SIBitmap(int p_Adress, int p_Align, int p_Autosize, int p_Stretch,
    				   int p_BitmapResourceBit0, 
                                                                       int p_BitmapResourceBit1,
                                                                       int p_BitmapFileBit0,
    				   int p_BitmapFileBit1, int p_Center, 
                                                                       int p_BitNr, int p_BitmapWidth, 
    				   int p_SubsheetEnable, int p_HelpActivate0, 
                                                                       int p_HelpActivate1, 
    				   int p_HelpKontext0, int p_HelpKontext1, 
                                                                       int p_Height, int p_Left, 
    				   int p_Top, BOOL p_Transparent,
                                                                       int p_Variable)
    {
    	Adress = p_Adress;
    	Align = p_Align;
    	Autosize = p_Autosize;
    	Stretch = p_Stretch;
    	BitmapResourceBit0 = p_BitmapResourceBit0;
    	BitmapResourceBit1 = p_BitmapResourceBit1;
    	BitmapFileBit0 = p_BitmapFileBit0;
    	BitmapFileBit1 = p_BitmapFileBit1;
    	Center = p_Center;
    	BitNr = p_BitNr;
    	BitmapWidth = p_BitmapWidth;
    	SubsheetEnable = p_SubsheetEnable;
    	HelpActivate0 = p_HelpActivate0;
    	HelpActivate1 = p_HelpActivate1;
    	HelpKontext0 = p_HelpKontext0;
    	HelpKontext1 = p_HelpKontext1;
    	Height = p_Height;
    	Left = p_Left;
    	Top = p_Top;
    	Transparent = p_Transparent;
    	Variable = p_Variable;
    }
    
    SIBitmap::~SIBitmap()
    {
    
    }
    BOOL SIBitmap::Paint(HDC hdc)
    {
    	PAINTSTRUCT ps;
    	HINSTANCE g_hInst;
    	HDC memory = CreateCompatibleDC(hdc);
    	HBITMAP bild = LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_BITMAP1)); 
    	SelectObject(memory, bild);
    	BitBlt(ps.hdc, 10,10, 50,50, memory, 0, 0, SRCCOPY);
    	DeleteObject(bild);
    	return TRUE;
    }
    

    Hier ist ein weiterer Codeschnipsel, indem die Funktionen in das Fenster eingefügt werden:

    LRESULT CALLBACK WndProc (HWND hwnd, UINT umsg, WPARAM wParam, 
                              LPARAM lParam)
    {
    
      int wmId, wmEvent;
    
      switch (umsg)
      {
        // Add cases such as WM_CREATE, WM_COMMAND, WM_PAINT if you don't 
        // want to pass these messages along for default processing.
    
        case WM_CLOSE:
          DestroyWindow (hwnd);
          return 0;
    
        case WM_DESTROY:
    	  CommandBar_Destroy(hwndCB);
          PostQuitMessage (0);
          return 0;
    
    	case WM_CREATE:
    			break;
    	case WM_COMMAND:
    			break;
    	case WM_SIZE:
       // Tell the command bar to resize itself to fill the top of the
    		break;
    	case WM_PAINT:
    
    		PAINTSTRUCT ps;
            HDC hdc;
            COLORREF crTxt, crBk;
    		RECT rcClient; 
            hdc = BeginPaint(hwnd, &ps);
    		GetClientRect(hwnd,&rcClient); 
    
    		SILine* pLine = new SILine(0,130,100,30,RGB(255,0,0),2,false,1,2);
    		pLine->Paint(ps.hdc);
    		delete pLine;
    
    		pLine = new SILine(10,130,220,130,RGB(255,0,255),20,true,1,2);
    		pLine->Paint(ps.hdc);
    		delete pLine;
    
    		SIBitmap* pBitmap = new SIBitmap(1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 100,
                                                                                        0, 0, 0, 0, 0, 100, 300, 100, 
                                                                                        true, 0);
    		pBitmap->Paint(ps.hdc);
    		delete pBitmap;
    
    		EndPaint(hwnd, &ps);
    
            break;
      }
      return DefWindowProc (hwnd, umsg, wParam, lParam);
    }
    

    Ich bekomme auch keine Fehlermeldung beim Kompilieren oder Bilden.
    Hoffe, dass mir jemand helfen kann.

    Mfg und schon einmal vielen Dank im voraus
    Heike



  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Erstmal vorweg: Du solltest bei SelectObject/DeleteObject dafür sorgen, dass das vorherige Objekt wieder eingesetzt, zum Beispiel so (hergott wie oft wir das jetzt schon hatten 🤡 ):

    // ...
    HPEN hpOldPen = SelectObject(hdcMem, CreatePen(...));
    // ...
    DeleteObject(SelectObject(hdcMem, hpOldPen));
    // ...
    

    Hier sind Deine Fehler:

    BOOL SIBitmap::Paint(HDC hdc)
    {
        PAINTSTRUCT ps;
        HINSTANCE g_hInst;
        HDC memory = CreateCompatibleDC(hdc);
        HBITMAP bild = LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_BITMAP1));
        SelectObject(memory, bild);
        BitBlt(ps.hdc, 10,10, 50,50, memory, 0, 0, SRCCOPY);
        DeleteObject(bild);
        return TRUE;
    }
    

    1.: 'ps' wird definiert und verwendet (siehe BitBlt) aber nicht initialisiert ⚠
    2.: 'g_hInst' wird definiert und ebenfalls nicht initialisiert, trotz Verwendung bei LoadBitmap, der Anfangswert ist also undefiniert - Wie soll da auch irgendetwas gezeichnet :p . Dies hättest Du auch herausbekommen, wenn Du den Return Wert von LoadBitmap (also den Inhalt von 'bild') überprüft hättest ( != NULL ).
    3.: 'memory' wird nicht freigegeben.
    4.: Siehe Hinweis zu SelectObject, oben.

    Warum hat die Funktion eigentlich den Return-Typ BOOL ? Dieser ist doch sowieso immer TRUE 🙄 .

    PS: Warum allokierst Du die Objekte unter WM_PAINT dynamisch (new/delete) ? Das ist überflüssig und fehleranfällig; es reicht doch auf dem Funktionsstack.



  • Danke für deine Antwort.
    Habe zuerst mal die DeleteFunktion geändert, sieht jetzt so aus:

    DeleteObject(memory);
    

    Jetzt funktioniert alles.

    Vielen Danke für die Hilfe.
    Lieben Gruß Heike


Anmelden zum Antworten