Wo liegt der Fehler?



  • Hallo,
    ich bin ziemlich neu in der WinAPi und habe versucht ein Rechteck zu zeichnen und das grau auszufüllen. Nur wenn es ich das Prog. debugge kommt nichteinmal ein Fenster..
    Wo liegt der Fehler?!
    Hier der Code:

    #include <Windows.h>
    
    LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	PSTR szCmdLine, int iCmdShow)
    {
    	static TCHAR	szAppName[] = TEXT("Test");
    	HWND			hwnd;
    	MSG				msg;
    	WNDCLASS		wndclass;
    
    	wndclass.style			= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc	= WndProc;
    	wndclass.cbClsExtra		= 0;
    	wndclass.cbWndExtra		= 0;
    	wndclass.hInstance		= hInstance;
    	wndclass.hIcon			= LoadIcon(NULL,IDI_APPLICATION);
    	wndclass.hCursor		= LoadCursor(NULL,IDC_ARROW);
    	wndclass.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    	wndclass.lpszMenuName	= NULL;
    	wndclass.lpszClassName	= szAppName;
    
    	if(!RegisterClass(&wndclass))
    	{
    		MessageBox(NULL, TEXT ("Ein unbekannter Fehler ist aufgetretten!"), szAppName, 
    			MB_OK | MB_ICONERROR);
    		return 0;
    	}
    
    	hwnd = CreateWindow (szAppName,
    		szAppName,
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,
    		CW_USEDEFAULT,
    		555,
    		700,
    		NULL,
    		NULL,
    		hInstance,
    		NULL );
    
    	ShowWindow (hwnd, iCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&msg,NULL,0,0))
    	{
    
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC				hdc;
    	PAINTSTRUCT		ps;
    	RECT			rect;
    	HBRUSH			hBrush;
    	RECT			rect2;
    	HWND			hwndButton[10];
    
    	SetRect(&rect, 0,0,555,120);
    	hBrush = CreateSolidBrush (RGB(140,140,140));
    
    	switch (message)
    	{
    	case WM_CREATE:
    		hwndButton[0] = CreateWindow(TEXT("Button"), TEXT("TEST"), 
    						WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 100, 100, 100, 100,hwnd, 
    						(HMENU) 1, ((LPCREATESTRUCT) lParam) -> hInstance, NULL);
    		return 0;
    
    	case WM_PAINT:
    
    		hdc = BeginPaint (hwnd, &ps);
    		FillRect(hdc, &rect, hBrush);
    		EndPaint(hwnd,&ps);
    
    	case WM_DESTROY:
    
    		DeleteObject(hBrush);
    		PostQuitMessage(0);
    
    	}
    	return 0;
    }
    


  • Zwei negative Sachen, die mir aufgefallen sind:
    1. In der CreateWindow-Funktion sollte neben WS_OVERLAPPEDWINDOW auch WS_VISIBLE stehen.
    2. Wieso hast du bei deiner WndProc-Funktion als return 0 angegeben? Dort solltest du schon die DefWindowProc-Funktion verwenden.

    Ich habe den Quellcode etwas umgeändert, welcher auch lauffähig ist.

    #include <Windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	PSTR szCmdLine, int iCmdShow) {
    	char *szAppName = "Test";
    	WNDCLASS wc;
    	HWND hwnd;
    	MSG msg;
    
    	wc.lpszClassName = szAppName;
    	wc.lpfnWndProc = WndProc;
    	wc.hInstance = hInstance;
    	wc.style = CS_HREDRAW | CS_VREDRAW;
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    
    	RegisterClass(&wc);
    
    	hwnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    		CW_USEDEFAULT, CW_USEDEFAULT, 555, 700, NULL, NULL, NULL, NULL);
    
    	while (GetMessage(&msg, 0, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC hdc;
    	PAINTSTRUCT ps;
    	RECT rect;
    	HBRUSH hBrush;
    	RECT rect2;
    	HWND hwndButton[10];
    
    	SetRect(&rect, 0, 0, 555, 120);
    	hBrush = CreateSolidBrush(RGB(140, 140, 140));
    
    	switch(message) {
    	case WM_CREATE:
    		hwndButton[0] = CreateWindow(TEXT("Button"), TEXT("TEST"),
    			WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 100, 100, 100, 100, hwnd, (HMENU)1,
    			((LPCREATESTRUCT)lParam)->hInstance, NULL);
    		return 0;
    
    	case WM_PAINT:
    		hdc = BeginPaint(hwnd, &ps);
    		FillRect(hdc, &rect, hBrush);
    		EndPaint(hwnd, &ps);
    		return 0;
    
    	case WM_DESTROY:
    		DeleteObject(hBrush);
    		PostQuitMessage(0);
    		return 0;
    
    	}
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    


  • Statt return 0 am Ende deiner WndProc sollte r return DefWindowProc(hwnd, message, wParam, lParam); stehen.
    Das alleine nützt dir aber nichts: Am Ende des WM_PAINT-Zweigs fehlt ein break und es würde direkt nach dem ersten Zeichnen PostQuitMessage aufgerufen werden.


Anmelden zum Antworten