Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

finished program suggestions to make better

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anything to make it more readable, fix anything that is redundant, fix anything that can be a bad habit, do something to make it tighter code, and stuff like that.

#include <windows.h>
#include <string.h>
#include <iostream>

using namespace std;

HINSTANCE hInst;
HWND wndHandle, textBut, editBox;

//Name of the main windows class name
char mainWin[] = "Color Words";

// forward declerations
int getSize(char* string);
bool initWindow( HINSTANCE hInstance ); // Initializes the window
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM); // Handles messages


//Main
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, 
				   int nCmdShow )
{
	//Initialize the window
	if ( !initWindow( hInstance) )
		return false;

	// main message loop
	MSG msg;
	ZeroMemory( &msg, sizeof( msg ) );
	while( msg.message != WM_QUIT )
	{
		/* I used peek message because I read that it returns messages immidiately so that you
		can call your own functions in the loop */
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
	}
	return (int) msg.wParam;
}

//Initializes the window
bool initWindow( HINSTANCE hInstance )
{
	WNDCLASSEX wc;

	// wc structure filled in to show what the window will look like to the system
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.style			= CS_HREDRAW | CS_VREDRAW;;
	wc.lpfnWndProc		= (WNDPROC)WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= hInstance;
	wc.hIcon			= 0;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= mainWin;
	wc.hIconSm			= 0;
	RegisterClassEx(&wc);

	// Creates the main window
	wndHandle = CreateWindow(	mainWin,				//class name
								"Project 1",			//title
								WS_OVERLAPPEDWINDOW,	// window style					   
								200,					// starting x coord
								100,					// starting y coord
								640,					// width
								480,					// height
								NULL,					// parent window (desktop)
								NULL,					// menu
								hInst,					// instance
								NULL );					// values passed to window

	// Makes sure the window handle created is valid
	if (!wndHandle)
		return false;
	
	// Creates a button that when pressed it displays what the user typed in the edit box
	textBut = CreateWindow(	"BUTTON", 
							"Push Me", 
							WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,	 
							265,					
							350,					 
							100,					 
							50,						
							wndHandle,					
							NULL,					
							hInst,					
							NULL);		

	if (!textBut)
		return false;

	// Creates an exit box
	editBox = CreateWindow(	"EDIT", 
							NULL, 
							WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,	 
							250,					
							50,					 
							155,					 
							20,						
							wndHandle,					
							NULL,					
							hInst,					
							NULL);	

	if (!editBox)
		return false;

	//Display the window to the screen
	ShowWindow(wndHandle, SW_SHOW);
	UpdateWindow(textBut);
	UpdateWindow(editBox);
	return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	string testStr; // stores what the user types in the edit box
	string prompt = "Please enter a word or two here:"; // user prompt

	HWND hwndCtl =  (HWND)lParam;// for if testing 
	switch( message )
	{
	case WM_COMMAND: // event involved buttons or menus
		switch (wParam) // button event type i.e. Click 
		{
				case BN_CLICKED: //button was clicked
					if (hwndCtl == textBut)
					{
						//gets what the user types in the edit box
						int size = GetWindowTextLength(editBox) + 1;
						char* text = new char[size];
						GetWindowText (editBox, text, size);
						/* convert it to a string.  Not much use here
						but for later projects strings are easier to work with so I 
						decided to do it */
						testStr = text;

						// Redraws the window.
						RedrawWindow (hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
					}
		}
	case WM_PAINT:
		hdc = BeginPaint( hWnd, &ps );

		//Prompts user
		SetTextColor(hdc,RGB(168, 79, 17));
		TextOut(hdc, 35,50, prompt.c_str(), prompt.length());

		//prints what the user typed in the edit box
		SetTextColor(hdc,RGB(255, 25, 2));
		TextOut(hdc, 290,80, testStr.c_str(), testStr.length());

		EndPaint(hWnd, &ps);

		break;
	//Quit
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
		break;
	}
}



Share this post


Link to post
Share on other sites
Advertisement
You'll have to be more specific than that if you want useful responses.

What, exactly, do you want to make better about your program?

Edit: Either I'm stupid, or unmarked edits make me look stupid! [embarrass]

Anyway, one thing I noticed is you seem to be using hInst without ever initializing it. I'm not sure if it has any ill-effects, but you might want to just use hInstance and just get rid of the global one.

[Edited by - bjle on September 22, 2005 8:46:44 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!