Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Windows programming - Errors that I cant seem to find?


  • Please log in to reply
7 replies to this topic

#1 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:05 PM

Posted 27 September 2009 - 12:05 PM

I have been following a tutorial of creating a basic window with C++, I understand 90% and I am researching some of the more complicated parts, although, when I go to compile with MinGW it throws two errors.

The first is: Undefined reference to 'GetStockObject@4'
And the second is: Undefined reference to 'Ellipse@20'

Do you think these are both to do with the painting because Ellipse obviously is so maybe there is something wrong there?

Here is my source code:

#include <windows.h>

// Fuction prototypes
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );

// Create WinMain()
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{
	#pragma region part 1 - STARTUP STUFF
	
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = TEXT("Philip");
	wc.lpszMenuName = 0;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	
	RegisterClass( &wc );
	
	HWND hwnd = CreateWindow(TEXT("Philip"), TEXT("My window's title"), WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, hInstance, NULL );
	
	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);
	
	#pragma endregion
	
	#pragma region part 2 - ENTER A LOOP TO CONTINUALLY KEEP CHECKING WITH WIN O/S FOR USER INTERACTION
	
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	#pragma endregion
	
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
	switch(message)
	{
		case WM_CREATE:
			Beep(50, 10);
			return 0;
			break;
		
		case WM_PAINT:
			{
				HDC hdc;
				PAINTSTRUCT ps;
				hdc = BeginPaint(hwnd, &ps); // Error here?
				Ellipse(hdc, 10, 10, 10, 10);
				EndPaint(hwnd, &ps);
			}
			return 0;
			break;
		
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
			break;
	}	
	return DefWindowProc(hwnd, message, wparam, lparam);
}

Thanks, Wolfy87.

BC AdBot (Login to Remove)

 


#2 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:05 PM

Posted 27 September 2009 - 12:24 PM

Ok, I have found that if I take out the line where it draws the ellipse and where the background colour is set then it compiles but the window will not show.

#3 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:05 PM

Posted 27 September 2009 - 03:27 PM

You need to link with gdi32.lib in Visual C++ and libgdi32.a in MINGW. If you have MINGW installed and its added to PATH variable, you can give following command:
gcc hello.c -l gdi32  -o hello.exe

Also try changing the Ellipse code to something like Ellipse(hdc, 10, 10, 100, 100); to make it visible on the window. If you keep both upper-left and lower-right corrdinates at (10,10) then radius of ellipse(circle) would be zero.

#4 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:05 PM

Posted 28 September 2009 - 09:54 AM

Legend! :thumbsup: it worked perfectly, I did'nt realise I ever had to link in things like that with MinGW, I thought that was just in VC++ etc anyway, now to learn how to put buttons on there etc =]

Thank you so much for that, Wolfy87.

#5 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:05 PM

Posted 28 September 2009 - 12:18 PM

yw :thumbsup:

#6 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:05 PM

Posted 28 September 2009 - 02:26 PM

I have removed my post, no longer needed..

Edited by Wolfy87, 28 September 2009 - 02:31 PM.


#7 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:01:05 PM

Posted 28 September 2009 - 02:42 PM

In C/C++ sin() function takes angle parameter in Radians.
As far as your calculator is concerned, you are using it in Degree mode.

180 degrees = PI radians

So you can make a simple function to convert degree values to radian like
double deg2rad(float degree){
	const float PI = 3.1415;
	return degree * (PI / 180.0);
}

Now you can find out value of sin(90 degree) as sin(deg2rad(90.0)) :thumbsup:

Edited by Romeo29, 28 September 2009 - 02:42 PM.


#8 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:05 PM

Posted 28 September 2009 - 04:59 PM

Cant try it right now, 11 o clock at night and im on my ipod but im sure it will work perfectly, you are the god of C++ in my eyes at the moment :thumbsup:

I spent over three hours trying to get it to work lol.

Thank you so much for all of your help, Wolfy87.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users