Sign in to follow this  
Fixxer

Win API: Edit Controls

Recommended Posts

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);


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

SetWindowText (hEdit, "Hello world!");

int size = GetWindowTextLength (hEdit) + 1;
char *buffer = new char [size];
GetWindowText (hEdit, buffer, size);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this