Skip to main content
GameDev.net gamedev.net
🔒 Locked

Win API: Edit Controls

Started by Fixxer Aug 30, 2005 at 12:30 PM 15 replies 3.4k views
Original Post
Fixxer
Fixxer
I need help with making edit controls. I dont know where I need to put this code to make them appear. I have already put them in WinMain right before the main loop, but they are not showing up.

		CreateWindowEx(NULL,"Edit","Username:",ES_LEFT,60,40,CW_USEDEFAULT,
			CW_USEDEFAULT,hwnd,NULL,hinstance,NULL);
		CreateWindowEx(NULL,"Edit","Password:",ES_LEFT,60,80,CW_USEDEFAULT,
			CW_USEDEFAULT,hwnd,NULL,hinstance,NULL);


dave
dave
I wouldn't bother with Win32, well not directly. I use wxWidgets and it is fantastic for making GUIs easily.

ace
LessBread
LessBread
Those calls would go into the WM_CREATE handler of the frame window. I would suggest getting a copy of Petzold's book on Programming Windows. I would also check out the tutorials at winprog.org.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
TDragon
TDragon
Quote:
Original post by LessBread
Those calls would go into the WM_CREATE handler of the frame window. I would suggest getting a copy of Petzold's book on Programming Windows. I would also check out the tutorials at winprog.org.


Seconded.

In this specific situation, you should consider adding the WS_VISIBLE and WS_CHILD properties to the style parameter, and using "EDIT" for the class name rather than NULL. Also, specifying width and height is generally a good idea.

Cheers,
Twilight Dragon
Fixxer
Fixxer
Ok I changed it to this. I tried putting it in the WM_CREATE but then it tells me hinstance is an undeclared identifier.

///////////////////////////////////////////////////////////////////////// Edit Controls//////////////////////////// This section will create our edit controls///////////////////////////////////////////////////////////////////////		CreateWindowEx(NULL,"Edit","Username:",ES_LEFT | WS_VISIBLE | WS_CHILD,			60,40,CW_USEDEFAULT,CW_USEDEFAULT,hwnd,NULL,hinstance,NULL);		CreateWindowEx(NULL,"Edit","Password:",ES_LEFT | WS_VISIBLE | WS_CHILD,			60,80,CW_USEDEFAULT,CW_USEDEFAULT,hwnd,NULL,hinstance,NULL);
Fixxer
Fixxer
Ive changed my code to this but it aint workin still.

#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <windowsx.h>#include <mmsystem.h>#include <stdio.h>#include <stdlib.h>#include <math.h>///////////////////////////////////////////////////////////////////////// Globals//////////////////////////// This section will define globals///////////////////////////////////////////////////////////////////////HWND main_hwnd = NULL;HINSTANCE main_instance = NULL;///////////////////////////////////////////////////////////////////////// Events Handler//////////////////////////// This section will handle windows events///////////////////////////////////////////////////////////////////////LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){PAINTSTRUCT ps;HDC hdc;switch(msg){case WM_CREATE:	{		HDC hdc = GetDC(hwnd);		COLORREF TextColor = RGB(0,175,0);		SetTextColor(hdc,TextColor);		SetBkMode(hdc,TRANSPARENT);		return(0);	}break;case WM_PAINT:	{		hdc = BeginPaint(hwnd,&ps);		TextOut(hdc,55,40,"Username:",strlen("Username:"));		TextOut(hdc,55,80,"Password:",strlen("Password:"));		EndPaint(hwnd,&ps);		return(0);	}break;case WM_DESTROY:	{		PostQuitMessage(0);		return(0);	} break;default:break;}return(DefWindowProc(hwnd,msg,wparam,lparam));}///////////////////////////////////////////////////////////////////////// Window main //////////////////////////// This section is where the main program code goes.///////////////////////////////////////////////////////////////////////int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow){///////////////////////////////////////////////////////////////////////// Windows Class//////////////////////////// This section sets up the windows class structure.// This section creates the application, and determines its// properties.///////////////////////////////////////////////////////////////////////WNDCLASSEX winclass;HWND hwnd;MSG msg;winclass.cbSize = sizeof(WNDCLASSEX);winclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;winclass.lpfnWndProc = WindowProc;winclass.cbClsExtra = 0;winclass.cbWndExtra = 0;winclass.hInstance = hinstance;winclass.hIcon = LoadIcon(hinstance,MAKEINTRESOURCE(100));winclass.hCursor = LoadCursor(NULL,IDC_ARROW);winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);winclass.lpszMenuName = NULL;winclass.lpszClassName = "WINCLASS1";winclass.hIconSm = LoadIcon(NULL,IDI_WINLOGO);if(!RegisterClassEx(&winclass))return(0);main_instance = hinstance;main_hwnd = hwnd;///////////////////////////////////////////////////////////////////////// Create Window//////////////////////////// This section will create our window.///////////////////////////////////////////////////////////////////////if(!(main_hwnd = CreateWindowEx(NULL,"WINCLASS1","Lightning Round: Please Log In",   WS_OVERLAPPEDWINDOW | WS_VISIBLE,100,75,400,300,NULL,NULL,   hinstance, NULL)))   return(0);///////////////////////////////////////////////////////////////////////// Create Edit Controls//////////////////////////// This section will create our edit controls///////////////////////////////////////////////////////////////////////	CreateWindowEx(NULL,"WINCLASS1","Username:",ES_LEFT | WS_VISIBLE | WS_CHILD,		60,40,CW_USEDEFAULT,CW_USEDEFAULT,main_hwnd,NULL,main_instance,NULL);	CreateWindowEx(NULL,"WINCLASS1","Password:",ES_LEFT | WS_VISIBLE | WS_CHILD,		60,80,CW_USEDEFAULT,CW_USEDEFAULT,main_hwnd,NULL,main_instance,NULL);///////////////////////////////////////////////////////////////////////// Main Event Loop//////////////////////////// This section will be where the main program code goes///////////////////////////////////////////////////////////////////////while(TRUE){	if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))	{		if(msg.message == WM_QUIT){			break;		}		TranslateMessage(&msg);		DispatchMessage(&msg);	}}return(msg.wParam);}


[Edited by - Fixxer on August 30, 2005 4:31:28 PM]
Fixxer
Fixxer
ahh ok. Ill try that. I have another question.
I compile once, it finished compiling and then runs.
When I got to compile again I get the error below, and I need to restarted VC++ 6.0. Whats up with that?

--------------------Configuration: Code - Win32 Debug--------------------
Compiling...
main.cpp
c:\documents and settings\owner\desktop\lightning round\code\main.cpp(0) : fatal error C1033: cannot open program database 'c:\documents and settings\owner\desktop\lightning round\code\debug\vc60.pdb'
Error executing cl.exe.

Code.exe - 1 error(s), 0 warning(s)

Edit:

Ok I changed that and still no boxes >.<

#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <windowsx.h>#include <mmsystem.h>#include <stdio.h>#include <stdlib.h>#include <math.h>///////////////////////////////////////////////////////////////////////// Globals//////////////////////////// This section will define globals///////////////////////////////////////////////////////////////////////HWND main_hwnd = NULL;HINSTANCE main_instance = NULL;///////////////////////////////////////////////////////////////////////// Events Handler//////////////////////////// This section will handle windows events///////////////////////////////////////////////////////////////////////LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){PAINTSTRUCT ps;HDC hdc;switch(msg){case WM_CREATE:	{		HDC hdc = GetDC(hwnd);		COLORREF TextColor = RGB(0,175,0);		SetTextColor(hdc,TextColor);		SetBkMode(hdc,TRANSPARENT);		return(0);	}break;case WM_PAINT:	{		hdc = BeginPaint(hwnd,&ps);		TextOut(hdc,55,40,"Username:",strlen("Username:"));		TextOut(hdc,55,80,"Password:",strlen("Password:"));		EndPaint(hwnd,&ps);		return(0);	}break;case WM_DESTROY:	{		PostQuitMessage(0);		return(0);	} break;default:break;}return(DefWindowProc(hwnd,msg,wparam,lparam));}///////////////////////////////////////////////////////////////////////// Window main //////////////////////////// This section is where the main program code goes.///////////////////////////////////////////////////////////////////////int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow){///////////////////////////////////////////////////////////////////////// Windows Class//////////////////////////// This section sets up the windows class structure.// This section creates the application, and determines its// properties.///////////////////////////////////////////////////////////////////////WNDCLASSEX winclass;HWND hwnd;MSG msg;winclass.cbSize = sizeof(WNDCLASSEX);winclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;winclass.lpfnWndProc = WindowProc;winclass.cbClsExtra = 0;winclass.cbWndExtra = 0;winclass.hInstance = hinstance;winclass.hIcon = LoadIcon(hinstance,MAKEINTRESOURCE(100));winclass.hCursor = LoadCursor(NULL,IDC_ARROW);winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);winclass.lpszMenuName = NULL;winclass.lpszClassName = "WINCLASS1";winclass.hIconSm = LoadIcon(NULL,IDI_WINLOGO);if(!RegisterClassEx(&winclass))return(0);main_instance = hinstance;main_hwnd = hwnd;///////////////////////////////////////////////////////////////////////// Create Window//////////////////////////// This section will create our window.///////////////////////////////////////////////////////////////////////if(!(main_hwnd = CreateWindowEx(NULL,"WINCLASS1","Lightning Round: Please Log In",   WS_OVERLAPPEDWINDOW | WS_VISIBLE,100,75,400,300,NULL,NULL,   hinstance, NULL)))   return(0);///////////////////////////////////////////////////////////////////////// Create Edit Controls//////////////////////////// This section will create our edit controls///////////////////////////////////////////////////////////////////////HANDLE hwndEditUsername = CreateWindowEx(NULL,"EDIT","Username:",	ES_LEFT | WS_VISIBLE | WS_CHILD,60,40,CW_USEDEFAULT,CW_USEDEFAULT,	hwnd,NULL,main_instance,NULL);HANDLE hwndEditPassword = CreateWindowEx(NULL,"EDIT","Password:",	ES_LEFT | WS_VISIBLE | WS_CHILD,60,80,CW_USEDEFAULT,CW_USEDEFAULT,	hwnd,NULL,main_instance,NULL);///////////////////////////////////////////////////////////////////////// Main Event Loop//////////////////////////// This section will be where the main program code goes///////////////////////////////////////////////////////////////////////while(TRUE){	if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))	{		if(msg.message == WM_QUIT){			break;		}		TranslateMessage(&msg);		DispatchMessage(&msg);	}}return(msg.wParam);}
Galapaegos
Galapaegos
Fixxer,

The reason you are getting that error is because the executable wasn't completely killed. The pdb data is used for a debugging purposes (like a look-up-table to find the errorounus sections). You should handle the WM_CLOSE message rather than WM_DESTROY, since close is handled when you push the [x] button.

-brad
-brad
Fixxer
Fixxer
Ahh thank you. Both problems are solved now.
FreeTutorialNewbie
FreeTutorialNewbie
destroy is for when somebody uses end task to kill your window
it wouldnt hurt to use both
but close is for the x button
Kippesoep
Kippesoep
Quote:
Original post by FreeTutorialNewbie
destroy is for when somebody uses end task to kill your window
it wouldnt hurt to use both
but close is for the x button


No, WM_DESTROY is sent any time a window is destroyed, not just when somebody uses "end task". WM_CLOSE is sent when the x button is pressed, or Alt-F4, or "close" selected from the window menu. Normally, the response to WM_CLOSE is to call PostQuitMessage if it is your main application window. The DefWindowProc will call DestroyWindow when it receives a WM_CLOSE message. (If you let DefWindowProc handle WM_CLOSE, you should probably check for WM_DESTROY so you know your main window has been closed).
Kippesoep
Fixxer
Fixxer
How do I get the text inside the box?
Kippesoep
Kippesoep
SetWindowText (hEdit, "Hello world!");int size = GetWindowTextLength (hEdit) + 1;char *buffer = new char [size];GetWindowText (hEdit, buffer, size);
Kippesoep
Skute
Skute
www.winprog.org is your friend, it covers all these questions and alot more.
If you are serious about getting into Win32 API programming, buy the Petzold book, its invaluable.
Mark Ingramhttp://www.mark-ingram.com
Fixxer
Fixxer
Ok thank you for that link.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.