Advertisement

Question About Ellipse Drawing

Started by September 13, 2002 06:58 PM
2 comments, last by bower12345 22 years, 4 months ago
Ok Folks, Here is my second question I am going to post. I have to say that everyone was a big help with my first problem, but now I have another problem. I want to draw an ellipse using the native Windows "Ellipse()" function, but it does not seem to be working. I created a pen and a brush, and I selected them using "SelectObject()". But when I go to draw my Ellipse, nothing shows on the window. I had added code underneath the "Ellipse()" function to make the ellipse grow, but even with that taken out I still cannot get a circle to be shown in my window.
  
// Basic Program to Draw a Window


#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <time.h>

bool RunApp = TRUE;

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
		case WM_PAINT:
			{
				PAINTSTRUCT ps;
				HDC hdc;
				hdc = BeginPaint(hwnd, &ps);
				EndPaint(hwnd, &ps);
				return(0);
			}
		case WM_QUIT:
			{
				RunApp = FALSE;
				return(0);
				break;
			}
		case WM_DESTROY:
			{
				RunApp = FALSE;
				PostQuitMessage(0);
				return(0);
				break;
			}
		case WM_CLOSE:
			{
				RunApp = FALSE;
				PostQuitMessage(0);
				return(0);
				break;
			}
		default:
			{
				return(DefWindowProc(hwnd, msg, wparam, lparam));
			}	
	}
}

int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
	srand((unsigned)time(NULL));
//	int x, y, red, green, blue;  Variables for random pixel drawing.

	int x1, y1, x2, y2;
	x1 = 500;
	y1 = 300;
	x2 = x1 + 10;
	y2 = y1 + 10;

	HDC hdc;
    MSG msg;
	HPEN defaultpen;
	HBRUSH defaultbrush;
	WNDCLASSEX BasicWindow;

	BasicWindow.cbSize = sizeof(WNDCLASSEX);
	BasicWindow.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	BasicWindow.lpfnWndProc = MsgHandler;
	BasicWindow.cbClsExtra = 0;
	BasicWindow.cbWndExtra = 0;
	BasicWindow.hInstance = hInstance;
	BasicWindow.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	BasicWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
	BasicWindow.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	BasicWindow.lpszMenuName = NULL;
	BasicWindow.lpszClassName = "Basic Window";
	BasicWindow.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
	
	RegisterClassEx(&BasicWindow);

	HWND hwnd;
	if(!(hwnd = CreateWindowEx(NULL,
							   "Basic Window",
							   "Basic Window",
							   WS_VISIBLE | WS_CAPTION | WS_TILEDWINDOW,
							   0,
							   0,
							   1024,
							   768,
							   NULL,
							   NULL,
							   hInstance,
							   NULL)))
	{
		return(0);
	}

	hdc = GetDC(hwnd);
	defaultbrush = CreateSolidBrush(RGB(0, 255, 0));
	defaultpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
	SelectObject(hdc, defaultbrush);
	while(RunApp)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

//		x = rand() % 1024 + 1;

//		y = rand() % 768 + 1;

//		red = rand() % 255 + 1;       Code to randomly generate pixel of different colors

//		green = rand() % 255 + 1;     on random areas of the screen.

//		blue = rand() % 255 + 1;

//		SetPixel(hdc, x, y, RGB(red, green, blue));

		Ellipse(hdc, x1, y1, x2, y1);
		x1 = x1 - 1;
		x2 = x2 + 1;
		y1 = y1 - 1;
		y2 = y2 + 1;
	}
	ReleaseDC(hwnd, hdc);
	return(msg.wParam);
}
  
Any help with this would be appreciated! Thanks in advance! Mike
Mikehttp://cs.wpunj.edu/~bowersom
As I look it over, I have to wonder why your code for the ellipse is in the main loop, and not in the WM_PAINT handler. What I''m confident is happening is this: You''re trying to draw to the screen when Windows doesn''t think anything should be allowed to be painted. The solution? Put the ellipse drawing code in the handler for the WM_PAINT message, between BeginPaint() and EndPaint();

I have an itching feeling you''ll also need to InvalidateRect() to tell Windows to let you draw on the window, but I could be wrong.

I could be way off base here, but I figured I''d put up an idea.

-Arek the Absolute
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Advertisement
I think Arek is absolutely right here. It looks very dangerous to have that GetDC()->MainLoop->ReleaseDC() thing. I don''t think you''re allowed to do so. I also believe you destroy/invalidate that DC in the WM_PAINT message. And Arek''s itchy feeling was correct, you''ll need to have an InvalidateRect() so windows sends you an WM_PAINT message whenever you want to update your ellipse.
Hey Thanks guys! It seemed to work and my ellipse is now on the screen. Just having trouble with making it move. I downloaded source code from a GDI pong game and I''m gunna take a look over that. But thanks again for the help!

Mike
Mikehttp://cs.wpunj.edu/~bowersom

This topic is closed to new replies.

Advertisement