Advertisement

What's up with selecting...

Started by April 22, 2002 03:00 PM
5 comments, last by Wachar 22 years, 7 months ago
What''s up with the selecting of the pens/brushes, and then restoring the old pen/brush? Tell me the answer to that question and tell me if I am correct on this one too: The following piece of code:
  
int old_brush = NULL;

old_brush = SelectObject(red_brush);
//assuming I already made it...


//also, I''m not sure it''s supposed to be an int on the first line

//(I did it off the top of my head!)

  
SelectObject returns the old object right? So the old object is put in the old_brush...correct?! And WHY do you restore the old brush/pen?? I see no point in doing this!!!
Wachar's Eternity <-<-<-<-<- Me own site!
Well you might want to put the old brush back when you are done, if you are using multiple brushes, and might say have one of five different brushes currently in use, then branch off for a special operation involoving a different brush, when done if you can just put the old one back it is easier than keeping check to move back to the right brush.

Also you might want to use HBRUSH to hold the brush as it might make the code alittle easier to read.

Ballistic Programs
Advertisement
Yeah, I see that I need to use HBRUSH. So, the only reason is to keep track of it? You don''t HAVE to do it?

----------
Windows is superior to Macs!!!
----------
Wachar's Eternity <-<-<-<-<- Me own site!
Why won''t this 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);		HPEN white_pen = CreatePen(PS_SOLID, 1, RGB(255,255,255));		HPEN old_pen = SelectObject(hdc, white_pen);		MoveToEx(hdc, 20, 20, NULL);		LineTo(hdc, 70, 90);		SelectObject(hdc, old_pen);				DeleteObject(white_pen);				ReleaseDC(hwnd, hdc);		}//while loop	return(msg.wParam);}//winmain  


----------
Windows is superior to Macs!!!
----------
Wachar's Eternity <-<-<-<-<- Me own site!
No you dont have to, but when you do alot of work with GDI and make some rather complex things you''ll see a reason to, maybe better to wait til then, just dont forget that it can.

The reason it isnt working is becaus ethe SelectObject returns a HGDIOBJ you need to typecast to a pen like this:

HPEN old_pen = (HPEN)SelectObject(hdc, white_pen);

Ballistic Programs
You have to store the old object in case you need to select it back. You want to select objects back so that you can delete the objects you created. Windows (at least NT) won''t allow you to delete an object selected into a DC. This is in the docs, btw.

As for your code: it''s great that you posted your entire program, but "doesn''t work" doesn''t help us find any bugs there. The drawing code looks ok to me.
Advertisement
Thanks, Drazgal. Mister Lamoth is having a lot of problems with the (HBRUSH) and (HPEN) thingys where they are needed.

----------
Windows is superior to Macs!!!
----------
Wachar's Eternity <-<-<-<-<- Me own site!

This topic is closed to new replies.

Advertisement