C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-30-2005, 04:02 PM   #1
Programmer
 
Frantic-'s Avatar
 
Join Date: Dec 2004
Posts: 114
Windows API: Edit Box Help

Im trying to get two edit boxes on the screen but they are not showing up. Can anyone tell me what Im doing wrong?

Code:
#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);
}
Frantic- is offline   Reply With Quote
Old 08-30-2005, 04:35 PM   #2
Registered User
 
Tonto's Avatar
 
Join Date: Jun 2005
Location: New York
Posts: 1,465
Don't be afraid to put tab your code, use lots of whitespace, break it into multiple files, etc. Other than that, I saw the problem was that your width and height specifications for your edit box were CW_USEDEFAULT, and the computer has no idea what the default width and height in that case would be, so I modified your code to look something like this:

Code:
	CreateWindowEx(NULL,"Edit","",ES_LEFT | WS_VISIBLE | WS_CHILD,
		140,40,100,20,main_hwnd,NULL,main_hinstance,NULL);
	CreateWindowEx(NULL,"Edit","",ES_LEFT | WS_VISIBLE | WS_CHILD,
		140,80,100,20,main_hwnd,NULL,main_hinstance,NULL);
Also not knowing what WINCLASS1 was, I changed it to an Edit class. MSDN explains that their are predefined system classes such as the "edit" box class so that your CreateWindow[Ex] will be properly made a user editable window. Also, if you were ever going to GetDlgItemText, or whatever, from your edit boxes, you would give your Edit boxes something more like:

Code:
//----In some header---//
#define ID_PASSBOX 1001
#define ID_USERBOX 1002
//---End header---//

...

	CreateWindowEx(NULL,"WINCLASS1","",ES_LEFT | WS_VISIBLE | WS_CHILD,
		140,40,100,20,hwnd,(HMENU)ID_USERBOX,hinstance,NULL);
	CreateWindowEx(NULL,"WINCLASS1","",ES_LEFT | WS_VISIBLE | WS_CHILD,
		140,80,100,20,hwnd,(HMENU)ID_PASSBOX,hinstance,NULL);

...

//---Later in WinProc---//
char szBuffer[128];
GetDlgItemText(hwnd, ID_USERBOX, szBuffer, 128);
The hMenu parameter of CreateWindowEx doubles as an ID for your child window controls, it's pretty nifty
Tonto is offline   Reply With Quote
Old 08-30-2005, 07:00 PM   #3
Programmer
 
Frantic-'s Avatar
 
Join Date: Dec 2004
Posts: 114
I got it to work with slightly different code, but the major changes you listed were made :-) thanks for the help.
Frantic- is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclassed editbox: Do I need to create 2 subclasses for 2 edit controls? BenPage C++ Programming 3 07-23-2005 08:00 AM
display a file in dropdown edit box sunburnbyRA Windows Programming 2 03-10-2004 01:58 PM
Colouring Edit Box Child Windows face_master Windows Programming 3 10-06-2002 02:58 AM
Tab Controls - API -KEN- Windows Programming 7 06-02-2002 09:44 AM
Rich edit control example Win API Echidna Windows Programming 1 09-17-2001 02:12 AM


All times are GMT -6. The time now is 01:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22