Dialog question

Hi
I'm new to windows programming.
I need to know how to get a user input, using a dialog or something similar (like scanf() from stdio.h)
And only C code if it's possible. :)
Thanks in advance.


Answer this question

Dialog question

  • Chidu

    Ok, here is a simple win32 app that uses no MFC or .NET. It is a simple window with an edit control and an OK button that when pressed will read the value from the edit and display it in a messagebox. You could of course expand and tailor this to what you need.



    #include <windows.h>
    HINSTANCE hInst;
    HWND hwndMain;
    HWND hwndEdit;
    HWND hwndBtn;

    LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
    int WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
    {
        MSG msg;
        BOOL bRet;
        WNDCLASS wc;

        // Register the window class for the main window.
        if (!hPrevInstance)
        {
            wc.style = 0;
            wc.lpfnWndProc = (WNDPROC) WndProc;
            wc.cbClsExtra = 0;
            wc.cbWndExtra = 0;
            wc.hInstance = hInstance;
            wc.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
            wc.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
            wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
            wc.lpszMenuName = L"MainMenu";
            wc.lpszClassName = L"MainWndClass";

            if (!RegisterClass(&wc))
            return FALSE;
        }

        hInst = hInstance; // save instance handle
        // Create the main window.
        hwndMain = CreateWindow(L"MainWndClass", L"Sample",
                                WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                                300, 100, (HWND) NULL, (HMENU) NULL, hInst, (LPVOID) NULL);

        // If the main window cannot be created, terminate
        // the application.
        if (!hwndMain)
            return FALSE;

        // Show the window and paint its contents.
        ShowWindow(hwndMain, nCmdShow);
        UpdateWindow(hwndMain);

        // Create and show the edit control
        hwndEdit = CreateWindow(L"Edit",L"",WS_BORDER|WS_CHILD |WS_VISIBLE,10,10,200,30,hwndMain,NULL,hInst,NULL);
        if( !hwndEdit )
        {
            MessageBox(hwndMain,L"Error creating edit control",L"Error",MB_OK);
            return FALSE;
        }

        ShowWindow(hwndEdit,SW_SHOW);
        UpdateWindow(hwndEdit);

        // Create and show the OK button
        hwndBtn = CreateWindow(L"Button",L"OK",WS_BORDER|WS_CHILD | WS_VISIBLE,220,10,30,30,hwndMain,NULL,hInst,NULL);
        if( !hwndBtn )
        {
            MessageBox(hwndMain,L"Error creating button control",L"Error",MB_OK);
            return FALSE;
        }

        ShowWindow(hwndBtn,SW_SHOW);
        UpdateWindow(hwndBtn);

        // Start the message loop.
        while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
        {
            if( msg.message == WM_QUIT )
            {
                break;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        // Return the exit code to the system.
        return msg.wParam;
    }

    LRESULT CALLBACK WndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    {
        wchar_t input[100];

        if( hwndBtn != NULL && (HWND)lParam == hwndBtn )
        {
            switch( wParam )
            {
            case BN_CLICKED:
                GetWindowText(hwndEdit,input,100);
                MessageBox(hwndMain,input,L"You typed...",MB_OK);
                break;
            default:
                break;
            }
        }
        else
        {
            switch( uMsg )
            {
            case WM_CLOSE:
                PostQuitMessage(0);
                break;
            }
        }
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
     

     

    EDIT: Formatting fixed


  • Lawrex

    Thanks for the answer
    I want to use UI for the input, but no classes and objects (MFC, .NET, etc) just C.
    thanks

  • mmmmBeeeer

    and.. hmm.. how to compile the code using unicode character set from the comand line

  • Dan Imbrogno

    Actually, never mind. The CEdit is also MFC. Let me see if I can think of something for you....


  • hashbrown

    If you want to stick to console style input, you can include <iostream> and use std::cin to get input.

    If you would like to use dialog boxes and a UI for gathering input, you may want to look into Windows Forms (.NET) to do this. If I'm not mistaken, I believe C++ Express comes with the CLR and Windows Forms project templates.

    If you don't want to use .NET, you could download the Platform SDK and set up your build environment so you can build MFC applications. This will make is much easier to develop graphical UIs in native C++.

    -Reza


  • Thilakavathy

    In that case, the easiest way I see to do this is to create a windows win32 .exe instead of a console app. Then set up your windowproc and create a window using CreateWindowEx and RegisterClassEx

    You can then create a CEdit class to represent the Edit control and capture its messages in your window proc.

    Give me a few minutes and I'll write you a quick example....

    -Reza


  • Bill Martin

    I found the answer, /D_UNICODE /DUNICODE :)
    thanks

  • Marko B. Simic

    Thank you very much :)

  • Dialog question