Siddiqui
Siddiqui

Reputation: 7850

Window interface instead of console in c

I have console application in c. I want to convert into window application, kindly guide me so that I can make it possible.

Upvotes: 1

Views: 354

Answers (2)

Roman Dmitrienko
Roman Dmitrienko

Reputation: 4245

That is a huge topic that requires a separate discussion. You may want to learn some GUI toolkit. Qt or wxWidgets will do (though they are written in C++, not C). If you're into C crossplatform development, you may take a look at GTK+. If you are planning only to write programs for Windows, you may learn Windows API. Whichever way you choose, there's a lot of docs available, but each and every way requires a lot of study and cannot be explained here.

Upvotes: 3

Suma
Suma

Reputation: 34463

Outline of steps you need to take:

  • replace main with WinMain
  • change subsystem from Console to Window
  • create a main window or a dialog (resource editor may come handy here), and its corresponding procedure
  • create a message loop
    • change a structure of your program to be executed in a loop (the program should be always responsible)
    • or execute the "serial console-like" program in a separare thread, just sending its output into the "window"

First two are quite easy, the most work lies in the next steps. if you want the window be just your own replication of a console, you can design a dialogue containing one text or edit control, and implement a simple dialog procedure and a message loop. Some code snippets follow, but giving a complete and working sample would go beyond the reasonable space. If you understand the code below, I guess it should get you started. If not, I am afraid you will have to learn Windows progamming basics first.

Dialog procedure

HWND consoleEditHWnd;

static int CALLBACK ConsoleDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
    case WM_INITDIALOG:
    {
      InitWindow((HINSTANCE)hInstApp,hDlg);
      consoleEditHWnd = GetDlgItem(hDlg,IDC_CONSOLE_EDIT);
      return TRUE;
    }


    case WM_SIZE:
      if (consoleEditHWnd)
      {
        RECT rect;
        GetClientRect(hDlg, &rect);
        MoveWindow(
          consoleEditHWnd, rect.left, rect.top,
          rect.right - rect.left, rect.bottom - rect.top, TRUE
        );
      }
      break;
  }
  return FALSE;
}

Dialog creation and a message loop

    hwndApp = CreateDialog(hInst, MAKEINTRESOURCE(IDD_CONSOLE), NULL, ConsoleDlgProc);
    ShowWindow((HWND)hwndApp,SW_SHOW);
    UpdateWindow((HWND)hwndApp);

    MSG msg;
    while( PeekMessage(&msg, 0, 0, 0, PM_REMOVE) )
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }

Writing into the console

When you want to add some text into the "console", you can do it using

  int count = GetWindowTextLengthW(consoleEditHWnd);
  ... allocate a buffer
  GetWindowTextW(consoleEditHWnd,buffer,count+newTextSize);
  ... append
  SetWindowTextW(consoleEditHWnd,buffer);

Upvotes: 2

Related Questions