Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2005, 2:38 PM   #1
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 13 layer is on a distinguished road
Question Reading edit controls (I have tried, code provided)

Hey guys, I have one more question before I call it a day!! :p Sorry to be such a bother, but how do you read edit controls? I have tried, using MSDN's method... Heres what I got, but I'll explain what happens after the code tags.
#include <windows.h>
#include <windowsx.h>

HWND hconnectIP;
HWND hIPData;
HWND hPortData;
int ePortLen;
int eIpLen;
PSTR pszMem;


int clicked(){
    MessageBox(NULL, "u clcked connect", "u clicked it!", MB_OK);
}

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); // the windows procedure

char szClassName[ ] = "MyClass";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int iCmdShow)

{
    HWND hwnd;               // the window handle
    MSG messages;            // a pointer to MSG; message
    WNDCLASSEX MyClass;        // structure of MyClass

    MyClass.hInstance = hThisInstance; // the structure of the window
    MyClass.lpszClassName = szClassName;
    MyClass.lpfnWndProc = WindowProcedure;      
    MyClass.style = CS_DBLCLKS;                 
    MyClass.cbSize = sizeof (WNDCLASSEX);


    MyClass.hIcon = LoadIcon (NULL, IDI_APPLICATION); //all this default stuff..
    MyClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    MyClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    MyClass.lpszMenuName = NULL;          
    MyClass.cbClsExtra = 0;                     
    MyClass.cbWndExtra = 0;                     
    MyClass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    if (!RegisterClassEx (&MyClass)) // register "MyClass", if it doesnt work, then exit
        return 0;

    hwnd = CreateWindowEx ( // windows API "CreateWindowEx"
           0,            // any extra settings to the window       
           szClassName,         // the class
           "SweetTalk",   //the title   
           WS_OVERLAPPEDWINDOW,  // style of the window
           CW_USEDEFAULT,     // x position
           CW_USEDEFAULT,      // y position
           400,                // windows width
           300,               // the windows height
           HWND_DESKTOP,      // the parent of the window
           NULL,              // almost like an ID for the parent window to get your events as well as its own, in this case, we want to handle our own events.
           hThisInstance,    // windows instance
           NULL               // MSDN says: "[in] Pointer to a value to be passed to the window through the CREATESTRUCT structure passed in the lpParam parameter the WM_CREATE message."  
           );
           
           if (hwnd == NULL){
                    MessageBox(NULL, "Couldn't create the main window", "ERROR", MB_ICONEXCLAMATION | MB_OK);
                    return EXIT_SUCCESS;
           }
           
    HWND hIP;
    
    hIP = CreateWindowEx(
          WS_EX_CLIENTEDGE,
          "STATIC",
          "Ip address:   Port:",
          WS_CHILD | WS_VISIBLE,
          5, 10,
          70, 55,
          hwnd,
          NULL,
          GetModuleHandle(NULL),
          NULL);
          
          if (hIP == NULL){
                  MessageBox(NULL, "Couldn't create static control", "ERROR", MB_ICONEXCLAMATION | MB_OK);
                  return EXIT_SUCCESS;
          }
          
    
    hIPData = CreateWindowEx(
              WS_EX_CLIENTEDGE,
              "EDIT",
              "",
              WS_CHILD | WS_VISIBLE,
              80, 10,
              150, 20,
              hwnd,
              NULL,
              GetModuleHandle(NULL),
              NULL);
              
              if (hIPData == NULL){
                          MessageBox(NULL, "Couldn't create edit control", "ERROR", MB_ICONEXCLAMATION | MB_OK);
                          return EXIT_SUCCESS;
              }
              
    
    hPortData = CreateWindowEx(
                WS_EX_CLIENTEDGE,
                "EDIT",
                "",
                WS_CHILD | WS_VISIBLE,
                80, 45,
                150, 20,
                hwnd,
                NULL,
                GetModuleHandle(NULL),
                NULL);
                
                if (hPortData == NULL){
                              MessageBox(NULL, "Couldn't create edit control #2", "ERROR", MB_ICONEXCLAMATION | MB_OK);
                }
              
              hconnectIP = CreateWindowEx(
                         WS_EX_CLIENTEDGE,
                         "BUTTON",
                         "Connect",
                         WS_CHILD | WS_VISIBLE,
                         250, 17,
                         70, 40,
                         hwnd,
                         NULL,
                         GetModuleHandle(NULL),
                         NULL);
                         
              if (hconnectIP == NULL){
                       MessageBox(NULL, "Couldn't create button control", "ERROR", MB_ICONEXCLAMATION | MB_OK);        
                       return EXIT_SUCCESS;
              }
          
          

    ShowWindow (hwnd, SW_SHOW);

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages); // translate the received messages from message
        DispatchMessage(&messages); // dispatch them to message
    }
    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)     // now we can handle these translated events          
    {
        case WM_DESTROY: // if the user wants the window to be closed or exited then
            PostQuitMessage (0);  // exit
            break;
	case WM_COMMAND:
	    if((HWND)lParam == hconnectIP) 
	    pszMem = (PSTR) VirtualAlloc((LPVOID) NULL, (DWORD) (ePortLen + 1), MEM_COMMIT, PAGE_READWRITE); 
        GetWindowText(hPortData, pszMem, ePortLen + 1); 
		MessageBox(NULL, pszMem, "Port", MB_OK);
		VirtualFree(pszMem, 0, MEM_RELEASE);
	    break;
        default:               
            return DefWindowProc (hwnd, message, wParam, lParam);
            break;
    }

    return 0;
}
Ok, no compile errors this time! But, it seems, that when I click in the edit control to focus it so I can type some port number, the MessageBox pops up! Why!? It's only supposed to pop up when I click the "Connect" button. Maybe there is a more simple way to read the edit control then what I have already attempted?
Again, sorry for being such a noob :eek: , but I feel I have learned tons in these past few days...
Thanks in advance
layer is offline   Reply With Quote
Old Apr 27th, 2005, 3:49 PM   #2
kirkl_uk
Programmer
 
kirkl_uk's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 86
Rep Power: 13 kirkl_uk is on a distinguished road
Send a message via MSN to kirkl_uk
You're missing some rather important {}s. Code should be as follows:

case WM_COMMAND:
	if((HWND)lParam == hconnectIP) 
	{
		pszMem = (PSTR) VirtualAlloc((LPVOID) NULL, (DWORD) (ePortLen + 1), MEM_COMMIT, PAGE_READWRITE); 
		GetWindowText(hPortData, pszMem, ePortLen + 1); 
		MessageBox(NULL, pszMem, "Port", MB_OK);
		VirtualFree(pszMem, 0, MEM_RELEASE);
	}
	break;

Originally, the only thing in the if statement was the mem allocation. I assume the code you have between the if and the break are only to be executed on the button click.

Also, a much simpler way to get the text from the edit would be:

if((HWND)lParam == hconnectIP)
{
	char sPort[10];
	GetWindowText(hPortData, sPort, sizeof(sPort)); 
	MessageBox(NULL, sPort, "Port", MB_OK);
}

Obviously this is flawed on having to set the limit for the sPort string, but you can probably find better ways of doing this on these forums.

kirkl_uk

Last edited by kirkl_uk; Apr 27th, 2005 at 3:57 PM.
kirkl_uk is offline   Reply With Quote
Old Apr 27th, 2005, 7:34 PM   #3
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 13 layer is on a distinguished road
You made my day...

Hey, just wanted to say thanks for all the help you've given. I like your way of getting the text better because, I know that no one will have a port bigger than 20 numbers :eek: Hehe. But I do have 1 more question for you. I promise, it's easy, and the last one:

1.) Why won't my logical "&&" operaters work - they are returning an error.
Heres the line of error:
MessageBox(NULL, "Ip address: " && sIp && ":" && sPort, "Ip and Port", MB_OK);

And here is the error from the compiler:
Quote:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp" -o "C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -lwsock32
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp: In function `LRESULT WindowProcedure(HWND__*, UINT, WPARAM, LPARAM)':
C:\Documents and Settings\MYNAME\Desktop\WinSock #2\server.cpp:162: error: cannot convert `bool' to `const CHAR*' for argument `2' to `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'

Execution terminated
And that should be it for today! Thanks a bunch for all of this info, I'm learning so much!!!
layer is offline   Reply With Quote
Old Apr 27th, 2005, 7:39 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,428
Rep Power: 18 Ooble is on a distinguished road
You have to use the strcat function to concatenate strings in C. You need to include either cstring or string.h to do so.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 27th, 2005, 8:22 PM   #5
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 13 layer is on a distinguished road
Thanks Ooble, after a little research for searching for "strcat" I have found a valid solution I'll edit this post in a few when I write the code, just wanted to let you know I got it.
layer is offline   Reply With Quote
Old Apr 27th, 2005, 8:31 PM   #6
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 13 layer is on a distinguished road
Cool

Yessss! Got it, thanks a bunch guys, this should get me off to a great start.
if((HWND)lParam == hconnectIP)
             {
                         char IpMem[40];
                         char sPort[10];
	                     GetWindowText(hPortData, sPort, sizeof(sPort)); 
	                     char sIp[30];
	                     GetWindowText(hIPData, sIp, sizeof(sIp));
	                     strcat(IpMem, sIp);
	                     strcat(IpMem, ":");
	                     strcat(IpMem, sPort);
	                     MessageBox(NULL, IpMem, "Ip and Port", MB_OK);
             }
Again, thanks...

EDIT: I also realize I could have "strcat(IpMem, "Ip address:");" but since I'm only needing the IP and Port for another function, not "Ip address" :p

Last edited by layer; Apr 27th, 2005 at 8:34 PM.
layer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC