Win API: Edit Controls

Started by
15 comments, last by Fixxer 18 years, 7 months ago
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);


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

ace
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
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
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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);
That's because hInstance hasn't been declared at that scope.

The easiest solution is to make a global HINSTANCE variable, assign it to hInstance at the start of WinMain(), and use it henceforth.

Cheers,
Twilight Dragon
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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]
Well, you're passing "WINCLASS1" as the class name for the controls, so it would be trying to create windows that were the same as your main window. Change it to "EDIT" for your controls.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
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);}
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

This topic is closed to new replies.

Advertisement