Fehler in einfacher Anwendung



  • Ich habe folgende Anwendung geschrieben:

    //CCreativeButtons
    #include <windows.h>
    
    class CCreativeButtons
    {
    public:
    	bool CreateButton(HDC hdc,HWND hwnd, HBITMAP bmp_off, HBITMAP bmp_on, bool clickbutton); 
    };
    
    bool CCreativeButtons::CreateButton(HDC hdc,HWND hwnd, HBITMAP bmp_off, HBITMAP bmp_on, bool clickbutton)
    {
    	//variables
    	HDC bmp_hdc = CreateCompatibleDC(NULL);
    	hdc = GetDC(hwnd);
    	//show bmp_off
    	SelectObject(bmp_hdc, bmp_off);
    	BitBlt(hdc, 100, 100, 100, 100, bmp_hdc, 0, 0, SRCCOPY);
    
    	return(true);
    }
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR lpCmdLine, int iCmdShow)
    {
    	WNDCLASS wc;
    	char szName[] = "CreativeButtons";
    
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.hCursor = LoadCursor(0, IDC_ARROW);
    	wc.hIcon = LoadIcon(0, IDI_WINLOGO);
    	wc.hInstance = hI;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = szName;
    	wc.lpszMenuName = 0;
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    
    	RegisterClass(&wc);
    
    	HWND hwnd = CreateWindow(szName, "CreativeButtonTest", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
    								CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hI, 0);
    
    	ShowWindow(hwnd, iCmdShow);
    	UpdateWindow(hwnd);
    
    	MSG msg;
    
    	while(GetMessage(&msg, 0, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	UnregisterClass(szName, hI);
    
    	return(msg.wParam);
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	CCreativeButtons buttoninstance;
    	HDC hdc = GetDC(hwnd);
    	HBITMAP bmp_on = 0;
    	HBITMAP bmp_off = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), "C:\\bmp_off.bmp",IMAGE_BITMAP,0, 0, LR_LOADFROMFILE); 
    
    	switch(message)
    	{
    	case WM_PAINT:
    	//show bmp_off
    		buttoninstance.CreateButton(hdc,hwnd, bmp_off, bmp_on, true);
    		return(0);
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return(0);
    	}
    
    	return(DefWindowProc(hwnd, message, wParam, lParam));
    }
    

    Wenn ich das Programm nun aber ausführe und das Fenster verschiebe bleibt es hängen, ich verstehe aber nicht wieso. Könnte mir bitte jemand den Fehler nennen und erklären?



  • Ich habe den Fehler schon entdeckt:
    Als ich "BeginPaint()" und "EndPaint()" unter "case WM_PAINT:" verwendet habe lief alles wie erwünscht.



  • Erstelle bitte nicht jedesmal bei WM_PAINT einen neuen Button ⚠



  • Dort wird ja gar keine Button erstellt sondern nur gezeichnet. :p

    The_incredible_Guest: Du solltest dich unbedingt mit der GDI Speicherverwaltung beschäftigen.



  • lagger schrieb:

    Dort wird ja gar keine Button erstellt sondern nur gezeichnet. :p

    Stimm ja, hatte mir das gar nicht so genau angeschaut 🙄 aber CreateButton ist dann auch nicht der optimale Name für diese Funktion 😉



  • Die Klasse ist ja auch noch lange nicht fertig ihr Heinzen, das ist ja erstmal nur die grafische Ausgabe.


Anmelden zum Antworten