![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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;
} 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 ![]() |
|
|
|
|
|
#2 |
|
Programmer
|
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. |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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:
Thanks a bunch for all of this info, I'm learning so much!!! ![]() |
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You have to use the strcat function to concatenate strings in C. You need to include either cstring or string.h to do so.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Feb 2005
Posts: 112
Rep Power: 4
![]() |
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);
}![]() 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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|