Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Newbie Question regarding GDI Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
igmeou

March 15, 2005, 10:55 AM

I'm having problem with compling of this sample code that I just found on a book.
I'm using WinXP and using Bloodshed Dev C++ 4.9.9.2 to compile the code as show below.
The code that I comments out is the part that is having error. Please enlighten me where did I go wrong.

  1. ::
  2. #include <windows.h>
  3.  
  4. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  5.  
  6. int APIENTRY WinMain( HINSTANCE hInstance,
  7.                     HINSTANCE hPrevInstance,
  8.                     LPSTR lpCmdLine,
  9.                     int nCmdShow )
  10. {
  11.     WNDCLASS WndClass;
  12.     WndClass.style = 0;
  13.     WndClass.cbClsExtra = 0;
  14.     WndClass.cbWndExtra = 0;
  15.     WndClass.lpfnWndProc = WndProc;
  16.     WndClass.hInstance = hInstance;
  17.     WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
  18.     WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
  19.     WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  20.     WndClass.lpszMenuName = 0;
  21.     WndClass.lpszClassName = "WinProg";
  22.    
  23.     RegisterClass(&WndClass);
  24.    
  25.     HWND hWindow;
  26.    
  27.     hWindow = CreateWindow("WinProg", "Window",
  28.                             WS_OVERLAPPEDWINDOW,
  29.                             0,0,600,460,NULL,NULL,
  30.                             hInstance, NULL);
  31.                            
  32.     ShowWindow (hWindow, nCmdShow);
  33.    
  34.     UpdateWindow (hWindow);
  35.    
  36.     MSG Message;
  37.     while (GetMessage(&Message, NULL, 0, 0))
  38.     {
  39.         DispatchMessage(&Message);
  40.     }
  41.     return (Message.wParam);
  42. }
  43.  
  44. LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage,
  45.                            WPARAM wParam, LPARAM lParam)
  46. {
  47.     switch(uiMessage)
  48.     {
  49.         case WM_PAINT:
  50.             HPEN hPen;
  51.             HPEN hPenalt;
  52.             HBRUSH hBrush;
  53.             HBRUSH hBrushalt;
  54.             hBrush = CreateSolidBrush (RGB(255,100,0));
  55.             hPen = CreatePen (PS_SOLID,2,RGB(0,255,255));
  56.             HDC hdc;
  57.             PAINTSTRUCT ps;
  58.             hdc = BeginPaint (hWnd, &ps);
  59.             //hBrushalt = SelectObject (hdc, hBrush);
  60.             //hPenalt = SelectObject (hdc, hPen);
  61.             //MoveToEx = (hdc, 20, 20, NULL);
  62.             LineTo (hdc, 100, 100);
  63.             Rectangle (hdc, 120, 20, 240, 140);
  64.             RoundRect (hdc, 260, 20, 420, 140, 20, 20);
  65.            
  66.             RECT rect;
  67.             SetRect (&rect, 20, 260, 240, 420);
  68.             FrameRect (hdc, &rect, hBrush);
  69.             SetRect (&rect, 260, 260, 420, 420);
  70.             FillRect (hdc, &rect, hBrush);
  71.             Ellipse (hdc, 440, 260, 480, 420);
  72.             SelectObject (hdc, hBrushalt);
  73.             SelectObject (hdc, hPenalt);
  74.             DeleteObject (hPen);
  75.             DeleteObject (hBrush);
  76.             EndPaint (hWnd, &ps);
  77.             return 0;
  78.         case WM_DESTROY:
  79.             PostQuitMessage(0);
  80.             return 0;
  81.         default:
  82.             return DefWindowProc (hWnd, uiMessage,
  83.                                   wParam, lParam);
  84.     }    
  85. }        
  86. ::

 
Rui Martins

March 15, 2005, 11:23 AM

Are we supposed to guess the error messages ?

Your problem is probably the fact that you are using the same function, and expecting it to return two different type objects, without doing any casting.

It is either an HPEN or HBRUSH, not both, unless they derive from the same class, then you should use that class or cast it accordingly.

Maybe this link will help you: http://www.flounder.com/drawing.htm

 
Illco

March 15, 2005, 11:32 AM

The problem is more likely in this line:

  1.  
  2. MoveToEx = (hdc, 20, 20, NULL);
  3.  


For one it is missing a name of the function to be called. But I suppose the variable for the assignment is the function name. It should probably read somehting like:

  1.  
  2. somevariable = MoveToEx( hdc, 20, 20, NULL );
  3.  


Or perhaps more likely:
  1.  
  2. MoveToEx( hdc, 20, 20, NULL );
  3.  


@Rui: the returned values are handles and are therefore interchangeable.

 
igmeou

March 15, 2005, 11:44 AM

Ops sorry for that.

The error is as follow below.

  1.  
  2. Compiler: Default compiler
  3. Building Makefile: "C:ProgrammingMakefile.win"
  4. Executing  make...
  5. make.exe -f "C:ProgrammingMakefile.win" all
  6. g++.exe -c tutorial_gdi.cpp -o tutorial_gdi.o -I"lib/gcc/mingw32/3.4.2/include"  -I"include/c++/3.4.2/backward"  -I"include/c++/3.4.2/mingw32"  -I"include/c++/3.4.2"  -I"include"  
  7.  
  8. tutorial_gdi.cpp: In function `LRESULT WndProc(HWND__*, UINT, WPARAM, LPARAM)':
  9. tutorial_gdi.cpp:58: error: invalid conversion from `void*' to `HBRUSH__*'
  10. tutorial_gdi.cpp:59: error: invalid conversion from `void*' to `HPEN__*'
  11. tutorial_gdi.cpp:60: error: assignment of function `BOOL MoveToEx(HDC__*, int, int, tagPOINT*)'
  12. tutorial_gdi.cpp:60: error: cannot convert `int' to `BOOL ()(HDC__*, int, int, tagPOINT*)' in assignment
  13.  
  14. make.exe: *** [tutorial_gdi.o] Error 1
  15.  
  16. Execution terminated
  17.  


Hope this help. Thanks for the advice guys. I'm now going through again.

 
igmeou

March 15, 2005, 12:06 PM

Ok. After trying to amending the code I only manage to solve the problem pointed out by Illco. It was my typo error as I shouldn't have an "=" in that statement at all. Dumb me!
But I still need help regarding these
hBrushalt = SelectObject (hdc, hBrush);
hPenalt = SelectObject (hdc, hPen);

Now the errors are left with this 2 lines.

  1. tutorial_gdi.cpp: In function `LRESULT WndProc(HWND__*, UINT, WPARAM, LPARAM)':
  2. tutorial_gdi.cpp:58: error: invalid conversion from `void*' to `HBRUSH__*'
  3. tutorial_gdi.cpp:59: error: invalid conversion from `void*' to `HPEN__*'
  4.  
  5. make.exe: *** [tutorial_gdi.o] Error 1
  6.  
  7. Execution terminated

 
Illco

March 15, 2005, 12:27 PM

How about:

  1.  
  2. hBrush = (HBRUSH*)CreateSolidBrush (RGB(255,100,0));
  3. hPen = (HPEN*)CreatePen (PS_SOLID,2,RGB(0,255,255));
  4.  


Just a guess...

 
igmeou

March 15, 2005, 12:56 PM

Nope! Don't work for your trail. It gives too much error.....

I had to remove the hBrushalt and hPenalt as like this:

  1.  
  2. SelectObject (hdc, hBrush);            
  3. SelectObject (hdc, hPen);
  4.  

The difference I had on this is that is show me a more colorful output as compare to when I comment them out. Do you think that the book is having a typo error on this??

I had read about the return values for SelectObject as below from http://www.technology.niagarac.on.ca/courses/ctec1638/win32/SelectObject.htm
  1.  
  2. Return Values
  3. If the selected object is not a region and the function succeeds, the return value is the handle of the object being replaced. If the selected object is a region and the function succeeds, the return value is one of the following values: Value
  4.  Meaning
  5.  
  6. SIMPLEREGION
  7.  Region consists of a single rectangle.
  8.  
  9. COMPLEXREGION
  10.  Region consists of more than one rectangle.
  11.  
  12. NULLREGION
  13.  Region is empty.
  14.  


I don't quite really understand the term region as what the above mention. Does it meant a window content area or a drawn object or what does it actually meant.

 
Nico

March 15, 2005, 02:12 PM

hBrush = (HBRUSH)CreateSolidBrush (RGB(255,100,0));
hPen = (HPEN)CreatePen (PS_SOLID,2,RGB(0,255,255));

(without the asterisks '*')

 
Nils Pipenbrinck

March 15, 2005, 02:22 PM

Right.. most of the gdi api has been written when everyone used C compilers. C-compilers don't complain about such assignments.

Just cast them, it's an annoyance, but that's the way it is (just wait until you have to cast function pointers, then you'll laugh about the (HPEN) cast.).


 
igmeou

March 15, 2005, 10:14 PM

Yes, Nico your code can be compile without error when I comments out this 2 lines of code.

  1.  
  2. //hBrushalt = SelectObject (hdc, hBrush);
  3. //hPenalt = SelectObject (hdc, hPen);            
  4.  


But, if I change this 2 lines of code to
  1.  
  2. SelectObject (hdc, hBrush);
  3. SelectObject (hdc, hPen);            
  4.  

there isn't any difference with or without your code. At least, I can't notice them :)

Thanks guys anyway for your prompt reply.

 
Nico

March 16, 2005, 02:05 AM

The difference is that you have no memory leak (because you SelectObject() the old pen and brush back into the dc before freeing yours - and the dc takes care of them. Otherwise, you had a resource leak).

 
This thread contains 11 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.