Windows API: Edit Box Help

This is a discussion on Windows API: Edit Box Help within the Windows Programming forums, part of the Platform Specific Boards category; Im trying to get two edit boxes on the screen but they are not showing up. Can anyone tell me ...

  1. #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);
    }

  2. #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

  3. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-23-2005, 08:00 AM
  2. display a file in dropdown edit box
    By sunburnbyRA in forum Windows Programming
    Replies: 2
    Last Post: 03-10-2004, 12:58 PM
  3. Colouring Edit Box Child Windows
    By face_master in forum Windows Programming
    Replies: 3
    Last Post: 10-06-2002, 02:58 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 02:12 AM

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