Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

In Intel fortran, How to open a console window in Windows applications

huanghe
Beginner
2,856 Views

I'm using compaque visual fortran/Intel visual fortranfor creating a Win32 API application. I'm try to create a split window with its upper part to show graphics and its low part to be a console window (like that in a console application or quickwin in which it can be easily created by using Open command) that I can interact with it easily such as output text and get data from input. I don't know how to open a console window in Windows environment (not quickWin or console applications). I'll appreciate it much if anybody teach me about it.

thanks,

0 Kudos
11 Replies
Steven_L_Intel1
Employee
2,856 Views

You can open a console window by calling the Win32 API routine AllocConsole. (Use USE KERNEL32 tomake its definition available.) Once you do that, you can do normal Fortran "console" I/O to it. It will open as a separate window - I don't know if there's a way to set its size or position.

0 Kudos
huanghe
Beginner
2,856 Views
Thanks a lot. Now I can open the console window and it works quite well. The problem now is how to adjust its position and size to make the split window work. I find some 32 API functions such as SetConsoleWindowInfo may be used for this purpuse, but they use character rows or columns not the pixels of window.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
2,856 Views
The alternative (and common) technique is to use a multi-line edit box, which gives you far more control than the normal console. See XSciCalc Xeffort sample.

However, that would require a replacement of WRITE(*) statements with edit-box handling functions; if you want to avoid that, you can redirect the output to a pipe and "listen" to the pipe in GUI code. See this thread.

(Yes, I know it's a bit complicated. Decide for yourself whether you want to do that--I just wanted to let you know about an alternative approach)
0 Kudos
huanghe
Beginner
2,856 Views

Thank you very much. The example and your related website seem very helpful. I appreciate it a lot.

If using edit box, output text is OK. Can I still get input from keyboard easily by "read (*,*)a,b,c " like that in a console window?

The commercial softwares AutoCAD, Matlab etc. all use this kind of interface for their main window which I like, since it is more direct and saving a lot of effort needed for dialog boxes.

I wonder if the win 32 API functions: createWindow() or createWindowEx() can provide a window type for console window like those for edit, list, treeview, or graphic windows instead of use allocConsole(). Thenit will be much easier for us.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
2,856 Views
I thought you need it only for output :-).

Console windows are fairly peculiar: while they have certain aspects of a window, it's fairly difficult to control them in a similar way as edit boxes. It's even difficult to grab their handle, for a start (GetConsoleWindow is relatively new API, check if it's implemented in your compiler). From then on, you can move it, maybe even change its style (to remove the borders and caption, for example, didn't try it myself), but you must resort to READ/WRITE and/or console APIs to control the text.

AutoCAD, Matlab etc. apparently "cook their own" solutions. I can't even figure using Spy++ out how Matlab 6.0 lies them out -- it appears it has only the frame window and that "sub-windows" including "Command window" are drawn over it. There is a window named "Matlab Command window", but it's overlapped and hidden--it seems that they do a lot of forth/back copying between it and its "image" on the screen.

You can also check out XTabEditor sample -- it uses the multi-line edit control to mimic the Notepad behavior (more or less). So, you should be able to "cook your own" solution, with some effort, to get the appropriate behavior. Note that it subclasses the control (XBindWindow->xEdit_OnKeyDown) to enable Col/Line display -- it should be possible to override the key behaviors to do something else (e.g. Arrow up/down repeat the last command instead of moving the cursor, Enter executes the command instead of breaking the line etc.) Feel free to ask if you encounter problems.
0 Kudos
huanghe
Beginner
2,856 Views

Thank you very much. All your suggestions are valuable to me. I've wondered a long time how to get the handle of the whole console window. The functions in the documentation seem to be only for handles of output, input or error. We've just brought Visual studio 2005 and IVF 9.1, so I'll switch from CVF to IVF. I shall first try GetConsolewindow, to see if then I can use the handle to change console window's size and position.

thanks again and best regards,

0 Kudos
drgfthomas
Beginner
2,856 Views

Here's how (in C, but it's easy to translate it to Fortran):

INT iret;
BOOL statConsole;
HWND hwnd;
HANDLE hStdOut;
CHAR ConsoleTitle[128];
DWORD dwCharsRead;
COORD ScrSize;
CONSOLE_SCREEN_BUFFER_INFO ScrBuff;
SMALL_RECT srWindowRect;

statConsole = AllocConsole();
statConsole = SetConsoleTitle("This is MyConsole");
dwCharsRead = GetConsoleTitle(ConsoleTitle, sizeof(ConsoleTitle));

hwnd = FindWindow(NULL,ConsoleTitle);

iret = SetWindowPos(hwnd,NULL,0,0,0,0,SWP_NOSIZE | SWP_NOZORDER); // Top lh corner
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

iret = GetConsoleScreenBufferInfo(hStdOut,&ScrBuff);
ScrSize.X = 2*ScrBuff.dwMaximumWindowSize.X; // The defaultis 80
ScrSize.Y = 50*ScrBuff.dwMaximumWindowSize.Y; // The defaultis 25
iret = SetConsoleScreenBufferSize(hStdOut,ScrSize);
srWindowRect.Right = 35; //width
srWindowRect.Bottom = 15; //height
srWindowRect.Left = srWindowRect.Top = (SHORT) 0;
statConsole = SetConsoleWindowInfo(hStdOut, TRUE, &srWindowRect);

0 Kudos
huanghe
Beginner
2,856 Views

Thanks a lot. I tried your sample code until line

iret = SetWindowPos(...)

It does change the position and size of the opened console window. I'm trying to generate a split window with its upper part as an openGL window to show the graphics while its lower partbeing theconsole window as an I/O interface such asking data for an rectangle, after getting necessary data, then the resulting rectangle may be draw in the upper openGL window.

Do you have any sample code like that? Maybe I'm asking too much. Sorry for that. I generated the two windows separately, however the split part doesn't work.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
2,856 Views
Gerry forgot to terminate the strings with char(0) (""C) -- as result, you'll get unpredictable behavior and likely failure of FindWindow. Otherwise, the code seems fine at first sight
0 Kudos
drgfthomas
Beginner
2,856 Views

Nah, Jugoslav. The snippet is written in C, not Fortran.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
2,856 Views
Ah, sorry, careless reading. Nevertheless, the required semantics was probably Lost in Translation.
0 Kudos
Reply