How do I preserve PDcurses output in a Windows Console window after executable completes? - winapi

I'm writing an application for Windows 10 to display and update text at specific locations in a Windows console window. The program is launched by a command in the console window. Upon launch, it erases the window's previous contents, then displays its output, updating as it goes, until completion. Upon exit, it should leave the displayed output in place, and a new command prompt should appear below. Launch, display, and subsequent command prompt all occur in the same console window. (Old school, I know, but that's the requirement.) The program is written in C and uses calls to the PDcurses library to control cursor placement and to output display text to the screen. The application is built with GCC/MinGW on a Windows 10 platform.
Everything works until the application exits, but then the display output disappears and the previous window contents (from before the app was launched) reappear. From what I can tell, this seems to be the default behavior for curses, possibly due to the way it handles screen buffering.
I'm looking for ways to override this behavior, but I'm not sure how to approach it. Can I direct PDcurses to write to the standard screen buffer rather than the alternate screen buffer (if that's what's going on)? If so, how? Should I copy the contents of the screen buffer before I call endwin(), then copy those contents back to the screen buffer afterward? Again, how? I'm sure this problem has already been solved, probably many times, but I haven't found any solutions that seem to apply to a C executable running in a Windows console, and I have only limited experience with PDcurses and the Windows API library. Any help would be appreciated.

The official way to do it is to set an environment variable: set PDC_RESTORE_SCREEN=0. You can combine this with set PDC_PRESERVE_SCREEN=Y to prevent PDCurses from clearing the screen at startup.

Related

Showing Standard Out in Lazarus IDE when running program?

I am just trying out Lazarus IDE out of curiosity, with the simplest Pascal program possible:
program project1;
begin
WriteLn('Hi there');
end.
When I run it with F9 in the IDE, I thought I would see the standard output in some window, but I cannot find it.
Does Lazarus provide a standard out view?
Yes, it does and console apps work fine in Lazarus. Probably all you need to do is to add a Readln() statement at the very end of your program so that the console window stays open long enough to be visible, otherwise the console window will close automatically as soon as the program completes execution.
program project1;
begin
WriteLn('Hi there');
ReadLn();
end.
However, it seems that Lazarus behaves somewhat differently: in Windows, the terminal window (aka console) displays automatically when the app starts execution, but may require a final ReadLn as above for it to stay on-screen long enough to be visible. On Ubuntu v.1704, to display the console window, I need to go to View | Debug windows | Terminal Output to get it to display (this is what Ctrl-Alt-O does, of course); once I've done that, the console window stays on-screen even after I close and re-open Lazarus. I imagine that somewhere in Lazarus there is a setting that makes the console window visible by default in new projects.
ISTR that somewhere in Lazarus there is an option to not display the console window, but I can't find at at the moment, so try what I have suggested and see if that works for you.
Of course, if you put a debugger breakpoint somewhere before the end of your program code, you should find that the console window is on-screen when the breakpoint trips.

Grab certain windows in Windows 10

I try to grab specific windows on Windows 10. I read some articles from MSDN to get familiar with APIs. My goal is to grab some certain windows, even if there are some windows on top of them (equivalent to OS X CGWindowList API). So if there are 2 windows: A and B, and windows B partially overlaps window A, I would like to be able to capture window A content, without capturing window B that partially covers window it.
According to this link, there are 5 different ways to capture the screen, if I understood them correct, most of them can capture only some regions on the screen, i.e. they don't distinguish between windows. The only API which allows to grab specific windows is "old standby, GDI".
I tried to acquire windows' device contexts using GetWindowDC() function, create a compatible bitmap and then use bit block transfer (BitBlt()). However, it seems that it does not always work as expected.
I've noticed several problems on Windows 10 (did not test on other operating systems):
Window's title bar usually is not captured. I tried to open Notepad and capture the window, but it was not fully captured, part of the scroll bar was not captured as well as a title bar. I tried to capture child windows of Notepad, but it did not work as expected, moreover some child windows are seem to have coordinates which seem to be wrong (the msctls_statusbar32msctls_statusbar32 child window of Notepad had the width which was 3 times bigger than the actual width of the window).
Some apps are not captured at all. For instance applications like "Photos", "Calc", "Settings" are not captured with that approach, when I try to capture them I get a black bitmap. There should be an API which allows capturing such windows, for instance TeamViewer is able to capture those Windows. It seems that all such windows are rendered by ApplicationFrameHost.exe process.
Does anyone know how to solve those issues?

Capuring a screen of an exe than pops up for a few miliseconds

OK, indirectly related to programming, but does anyone know how to capture a a screen that pops up for a few milliseconds in Windows? (The screen popups up when I double click an exe) and then terminates the process.
Its too fast to actually focus on the information its bringing up, but it is defnintly saying something
Is there some kind of software than can playback screen in ultra slow motion?
Is it a console app or a winforms app? If it's a console app, you could drop to a command line and run it manually. Then you can clipboard the output.

Create a background process in windows

How do I make a process go the background programatically?
What I want is for the user to double-click the process executable, and it just goes into the background ... and does not open a window while executing.
Any code snippet in visual c++ would be very helpful
Have you considered creating a Windows Service instead? They're specifically designed to run in the background without showing a UI.
Otherwise, just create an application without a window.
I know this is old, but I thought I would post something for when people find this through search.
While I like Cody Gray's answer for design correctness, sometimes you don't have a choice.
If you want to launch a program without jumping to the new window (it appears in the background or minimized) or not create a window at all try looking at the ShellExecute and ShellExecuteEx functions. The argument nShowCmd (or nShow) gives you (among others) the options:
SW_HIDE
Hides the window and activates another window.
SW_SHOWMINNOACTIVE
Displays the window as a minimized window. The active window remains active.
As the documentation says, SW_HIDE creates a process running the executable you give it, but if this program would normally create a window, none appears.
This might help: http://ss64.com/nt/start.html
I tried this way and it worked fine:
Create a console application and write your codes in the sub main as any other console application.
Now change the application type in the project properties to windows Forms application from Console application
thats it

Launching a program so that it opens in center of the launching application

Launching a program so that it opens in center of the launching application.
Platform: Mac
Launching mechanism: a c++ program launches using system()
Currently I observe the program launched pushes it to left most part of the screen.
Launching a program so that it opens in center of the launching application.
On Mac OS X systems, programs don't launch into a specific area of the screen. When they launch, they become the "front" application, and take over the menubar area with their own menus. They may choose to open a window after they have opened, or not.
Find out if the program in question accepts apple events / applescript commands. If so, you may be able to ask the program to position a window in a particular location.
Also, if you still have to use system, you can send apple events with the osascript command.
If you're using system, there's really nothing you can do, since you've implicitly delegated everything about the process except its startup args to the OS. There may be ways to accomplish this using other OSX-specific APIs, but not with the parameters you've outlined here.

Resources