I have console application in c. I want to convert into window application, kindly guide me so that I can make it possible.
-
2Sorry, you cannot easily get there from here, or at least the instructions for doing so are beyond the scope of an SO answer. – Mar 17 '10 at 09:21
-
@Neil Butterworth, Well is it possible to display a picture when my console is running? – Siddiqui Mar 17 '10 at 09:28
-
See the answers to this http://stackoverflow.com/questions/1937163/drawing-in-a-win32-console-on-c/1937178 – Mar 17 '10 at 09:31
2 Answers
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.

- 3,375
- 3
- 37
- 48
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);

- 33,181
- 16
- 123
- 191
-
@Suma can you kindly explain little bit more, i have tried this WinMain but its only hide the console doesn't show the window. – Siddiqui Mar 17 '10 at 09:32