Advertisement

Fill something in!

Started by April 23, 2002 06:09 PM
24 comments, last by Wachar 22 years, 8 months ago
I have drawn a triangle and now, I''m trying to fill it in with a brush. How to do it?
  
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	PAINTSTRUCT		ps;
	HDC				hdc;

	switch(msg)
	{
	case WM_CREATE:
		{
			return(0);
		} break;

	case WM_PAINT:
		{
			hdc = BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);
			return(0);
		} break;

	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return(0);
		} break;

	default:break;
	}
	return(DefWindowProc(hwnd, msg, wparam, lparam));
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
{
	WNDCLASSEX	winclass;
	HWND		hwnd;
	MSG			msg;
	HDC			hdc;
	
	winclass.cbSize = sizeof(WNDCLASSEX);
	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

	winclass.lpfnWndProc	= WindowProc;
	winclass.cbClsExtra		= 0;
	winclass.cbWndExtra		= 0;
	winclass.hInstance		= hinstance;
	winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName	= NULL;
	winclass.lpszClassName	= "WINCLASS1";
	winclass.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&winclass))
		return(0);

	if(!(hwnd = CreateWindowEx(NULL,
						    "WINCLASS1",
						    "My Awesome Window!",
						    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
							0,0,
							400,400,
							NULL,
							NULL,
							hinstance,
							NULL)))

							return(0);

	while(1)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		hdc = GetDC(hwnd);

		//Create the white pen and the old pen for later storage.

		HPEN white_pen = CreatePen(PS_SOLID, 1, RGB(255,255,255));

		HPEN old_pen = (HPEN)SelectObject(hdc, white_pen); // Does not work!!


		//Create a brush

		HBRUSH white_brush = CreateSolidBrush(RGB(255,255,255));

		HBRUSH first_brush = (HBRUSH)SelectObject(hdc, white_brush);
		//Get ready to draw the triangle

		MoveToEx(hdc, 20, 20, NULL);

		//Draw the triangle

		LineTo(hdc, 70, 90);
		LineTo(hdc, 110,20);
		LineTo(hdc, 20, 20);
		
		//WHERE TO USE THE BRUSH!!??


		//Get ready to destroy the white pen

		SelectObject(hdc, old_pen);
		
		//Destroy the pens and brushes!!!

		DeleteObject(white_pen);
		
		//Let go of the Device Context

		ReleaseDC(hwnd, hdc);

	
	}//while loop


	return(msg.wParam);

}//winmain

  
---------- Windows is superior to Macs!!! ----------
Wachar's Eternity <-<-<-<-<- Me own site!
Look up Polygon()

I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
Advertisement
Wachar, your sig is very flammable. I''d change it if I were you.
Affirmative.

----------
Take it to the Xtreme!
----------
Wachar's Eternity <-<-<-<-<- Me own site!
Select the pen in before you draw your lines, and to fill it in, you''d have to use the Polygon() function as InvaderX said and simply select in the brush in the same manner as you did the pen and delete the brush the same as the pen.

-AJ

C:\DOS
C:\DOS\RUN
RUN\DOS\RUN

-Comic Book Store Guy''s t-shirt that I saw on the Simpsons, although it didn''t actually come from the Simpsons.

http://vdsoft.netfirms.com/home.html
C:DOSC:DOSRUNRUNDOSRUN-Comic Book Store Guy's t-shirt that I saw on the Simpsons, although it didn't actually come from the Simpsons.http://vdsoft.netfirms.com/home.html
But, how do you make it a fixed figure?

----------
Take it to the Xtreme!
----------
Wachar's Eternity <-<-<-<-<- Me own site!
Advertisement
I tried it and it doesn''t work!


  #define WIN32_LEAN_AND_MEAN#include <windows.h>LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){	PAINTSTRUCT		ps;	HDC				hdc;	switch(msg)	{	case WM_CREATE:		{			return(0);		} break;	case WM_PAINT:		{			hdc = BeginPaint(hwnd, &ps);			EndPaint(hwnd, &ps);			return(0);		} break;	case WM_DESTROY:		{			PostQuitMessage(0);			return(0);		} break;	default:break;	}	return(DefWindowProc(hwnd, msg, wparam, lparam));}int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd){	WNDCLASSEX	winclass;	HWND		hwnd;	MSG			msg;	HDC			hdc;		winclass.cbSize = sizeof(WNDCLASSEX);	winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;	winclass.lpfnWndProc	= WindowProc;	winclass.cbClsExtra		= 0;	winclass.cbWndExtra		= 0;	winclass.hInstance		= hinstance;	winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);	winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);	winclass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);	winclass.lpszMenuName	= NULL;	winclass.lpszClassName	= "WINCLASS1";	winclass.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);	if(!RegisterClassEx(&winclass))		return(0);	if(!(hwnd = CreateWindowEx(NULL,						    "WINCLASS1",						    "My Awesome Window!",						    WS_OVERLAPPEDWINDOW | WS_VISIBLE,							0,0,							400,400,							NULL,							NULL,							hinstance,							NULL)))							return(0);	while(1)	{		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))		{			if(msg.message == WM_QUIT)				break;			TranslateMessage(&msg);			DispatchMessage(&msg);		}		hdc = GetDC(hwnd);		//Create a pen		HPEN white_pen = CreatePen(PS_SOLID, 1, RGB(255,255,255));		HPEN first_pen = (HPEN)SelectObject(hdc, white_pen);				//Create a brush		HBRUSH white_brush = CreateSolidBrush(RGB(255,255,255));		HBRUSH first_brush = (HBRUSH)SelectObject(hdc, white_brush);		//Get ready to draw the triangle		POINT poly[3] = {p0x,p0y,p1x,p1y,p2x,p2y};		Polygon(hdc, poly, 3);				SelectObject(hdc, first_pen);		SelectObject(hdc, first_brush);		DeleteObject(white_pen);		DeleteObject(white_brush);		//Let go of the Device Context		ReleaseDC(hwnd, hdc);		}//while loop	return(msg.wParam);}//winmain  


Also, I still need the answer to my last question!

----------
Take it to the Xtreme!
----------
Wachar's Eternity <-<-<-<-<- Me own site!
Where did you declare p0x, p0y, p1x, p1y, p2x, and p2y?

Also, saying "It doesn''t work" tells me nothing at all. It would make it much easier for me and everyone else if you told us how it doesn''t work and if you can where it doesn''t work.

I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
Here is the code to draw a solid red triangle, just replace red with whatever color you want bada-bing, there ya go:


    HPEN redPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));	HBRUSH redBrush = CreateSolidBrush(RGB(255,0,0));SelectObject(hdc, redPen);SelectObject(hdc, redBrush);POINT poly[2] = {0,50,50,0,100,50};		Polygon(hdc, &poly, 3);						DeleteObject(redPen);		DeleteObject(redBrush);    


That's all you need to insert into your WinMain function to make it work. Take out all the triangle drawing code you had before and replace it with that. That will draw a red filled triangle with points (0,50), (50,0), and (100,50). And you forgot to tell the Polygon() function to look for the address of the POINT array instead of the actual point array itself. And remember, array counting starts at 0, so you only have to have put the number two in the array definition to get three POINT structures. Hope that helps.

-AJ



C:\DOS
C:\DOS\RUN
RUN\DOS\RUN

-Comic Book Store Guy's t-shirt that I saw on the Simpsons, although it didn't actually come from the Simpsons.

http://vdsoft.netfirms.com/home.html

[edited by - redneckCoder on April 24, 2002 10:36:15 AM]
C:DOSC:DOSRUNRUNDOSRUN-Comic Book Store Guy's t-shirt that I saw on the Simpsons, although it didn't actually come from the Simpsons.http://vdsoft.netfirms.com/home.html
i was interested in making a triangle to... so i copied the code and made that agustment you did this is the 2 errors i got from the 2 poly lines of code

error C2078: too many initializers
error C2664: ''Polygon'' : cannot convert parameter 2 from ''struct tagPOINT (*)[2]'' to ''const struct tagPOINT *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

...

This topic is closed to new replies.

Advertisement