Hello and first sorry for my very bad english.

i have little knowledge about c and c++ but im trying to learn win32 programming with c++. its little hard and im very new at that. im trying to do very very simple thing: i have an main window, 1 button, 1 edit control. when user clicks button, i wanna show edit controls text with messagebox. there is a problem, i guess i should get text from editcontrol with gettext api, but i have to pass hwnd for that. actually im already have hwnd when i calling createwindowex function, but its not public. so in message switch block i dont have hwnd for edit control. what should i do? findwindow? or maybe public variable? Thanks.
Edit/Delete Message

Take a look at this tutorial ...

actually i looked that tutorials for 2 hours. i think my answer is not there or i cant find it. maybe i should explain my question (because of my english) i dont want just get text, i will maybe change button text, or scrollbars position in message switch block. so i need hwnd all these cases, should i store hwnd for every control? hope i dont need this? if i dont need this what sould i do? i mean how can i retrive hwnd for my controls? like i said i can store but which is the rigth way to do it? sorry for english again :)

:'(

Member Avatar for stephen.id
#include <windows.h>

HWND EditControl                   = NULL;
HWND ButtonShowText                = NULL;

enum {
    ID_EDITCONTROL,
    ID_SHOWTEXT,
};

void GetEditControlText(HWND);

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
                                            LPSTR lpCmdLine, int iCmdShow) {
    HWND MainWindow;
    MSG WinMessages;
    BOOL GetQuit = FALSE;
    WNDCLASSEX WinClass;

    WinClass.hInstance     = hThisInstance;
    WinClass.lpszClassName = "MainWindow";
    WinClass.lpfnWndProc   = WindowProcedure;
    WinClass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    WinClass.cbSize        = sizeof(WNDCLASSEX);
    WinClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    WinClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    WinClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    WinClass.lpszMenuName  = NULL;
    WinClass.cbClsExtra    = 0;
    WinClass.cbWndExtra    = 0;
    WinClass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
    if(!RegisterClassEx(&WinClass))
        MessageBox(NULL, "Window Failed To Register", "Window", MB_OK | MB_ICONERROR);

    MainWindow             = CreateWindowEx(
        0,
        "MainWindow",
        "Getting User Text",
        0,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        480,
        110,
        HWND_DESKTOP,
        NULL,
        hThisInstance,
        NULL);
    ShowWindow(MainWindow, iCmdShow);

        while(!GetQuit) {
            if(!PeekMessage(&WinMessages, NULL, 0, 0, PM_REMOVE)) {
                } else {
                    if(WinMessages.message == WM_QUIT) {
                        GetQuit = TRUE;
                    } else {
                        TranslateMessage(&WinMessages);
                        DispatchMessage(&WinMessages);
                    }
                }
        }
    return WinMessages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND GetWindow, UINT GetMessages, WPARAM wParam, LPARAM lParam) {
    switch(GetMessages) {

        case WM_CREATE:
            EditControl = CreateWindowEx(0, TEXT("Edit"), NULL,
            WS_CHILD | WS_VISIBLE | WS_BORDER,
            160, 10, 300, 25,
            GetWindow, (HMENU)ID_EDITCONTROL, NULL, 0);

            ButtonShowText = CreateWindowEx(0, "Button", TEXT("Show Text"),
            BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
            160, 39, 300, 25,
            GetWindow, (HMENU)ID_SHOWTEXT, NULL, 0);
        break;

        case WM_PAINT:
            PAINTSTRUCT Paint;
            HDC DeviceContext;
            DeviceContext = BeginPaint(GetWindow, &Paint);
                TextOut(DeviceContext, 10, 12, TEXT("User Text                  :"), 28);
                TextOut(DeviceContext, 10, 30, TEXT("Press Escape To Exit"), 20);
            EndPaint(GetWindow, &Paint);
        break;

        case WM_COMMAND:
            switch(wParam) {
                case ID_SHOWTEXT:
                    GetEditControlText(EditControl);
                break;
            } break;
        break;

        case WM_KEYDOWN:
            if(GetAsyncKeyState(VK_ESCAPE)) {
                PostQuitMessage(0);
            }
        break;

        case WM_QUIT:
            PostQuitMessage(0);
        break;

        default:
            return DefWindowProc(GetWindow, GetMessages, wParam, lParam);
    }
return(0);
}

void GetEditControlText(HWND GetEditControl) {
    int TextLength = GetWindowTextLength(GetEditControl) + 1;
    TCHAR Text[30];
    GetWindowText(GetEditControl, Text, TextLength);
    if(TextLength > 1) {
        MessageBox(NULL, Text, Text, MB_OK | MB_ICONEXCLAMATION);
    } else {
        MessageBox(NULL, "No Text", "Error", MB_OK | MB_ICONERROR);
    }
}

Thank you but i have 2 questions
1- why we are creating controls in "WM_CREATE" block?
2- if i have 1000 controls in window, will i define 1000 public variable?
Thank you again

Member Avatar for stephen.id

WM_CREATE block is basically what its called, its usaully for creating buttons, edit controls, child windows, etc...if i had 1000 public variables then you would have to right a routine that read all of them, some kind of loop, i might post an example

Member Avatar for stephen.id

Mark this tread as solved :P

Mark this tread as solved :P

Actually the OP has to decide whether his problem is solved or not, you don't have to do this for him ...

Member Avatar for stephen.id

Thank you but i have 2 questions
1- why we are creating controls in "WM_CREATE" block?
2- if i have 1000 controls in window, will i define 1000 public variable?
Thank you again

ummm lol i thought that meant it was solved, maybe not, lol, i made a program that did exaclty what he wanted :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.