Win32 C++ console clearing screen without blinking - c++

I've seen some console games where the screen refreshes/clears itself without the annoying blinking. I've tried numerous solutions, here's what I got as of now:
while(true)
{
if(screenChanged) //if something needs to be drawn on new position
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
///printf all the time graphics on their right position with SetConsoleCursorPosition
Sleep(33.3f);
}
Still, I'm getting some minimal blinking. Anyone have any ideas?

The reason this is happening is because the display refreshes between the time you clear the console screen and actually draw to it. Usually this can happen so fast that you never see it but once in a while you do it at the right time and you experience flickering.
One great option is to create an offscreen buffer the same size and width as the console screen, do all of your text output and updating there, then send the entire buffer to the console screen using WriteConsoleOutput. Make sure you take into account that the screen buffer has to hold both text and attribute information, the same format as the console.
BOOL WINAPI WriteConsoleOutput(
_In_ HANDLE hConsoleOutput,
_In_ const CHAR_INFO *lpBuffer,
_In_ COORD dwBufferSize,
_In_ COORD dwBufferCoord,
_Inout_ PSMALL_RECT lpWriteRegion
);

You want to do the equivalent of double buffering. Using CreateConsoleScreenBuffer and SetConsoleActiveScreenBuffer api calls, you can modify an offscreen buffer, then switch buffers, like we used to in the bad old days :) Here's an article that explains how: http://msdn.microsoft.com/en-us/library/windows/desktop/ms685032%28v=vs.85%29.aspx

You looking for double buffering

Related

I am trying to send a window (application) from the main monitor (touch screen) to a secondary monitor using the same graphics card on the same host

I am trying to send a window (application) from the main monitor (touch screen) to a secondary monitor using the same graphics card on the same host
I spent a long time gathering data, trying to get a clear idea:
The MoveDown MOVEDEV_PARAMS
BOOL GetWindowRect(
HWND hWnd, // handle to window
LPRECT lpRect // window coordinates
);
AND...
And the following, but my brain can't put them together into something useful for me
BOOL EnumDisplayDevices(
LPCTSTR lpDevice, // device name
DWORD iDevNum, // display device
PDISPLAY_DEVICE lpDisplayDevice, // device information
DWORD dwFlags // reserved);
BOOL EnumDisplayMonitors(
HDC hdc, // handle to display DC
LPCRECT lprcClip, // clipping rectangle
MONITORENUMPROC lpfnEnum, // callback function
LPARAM dwData // data for callback function
);
BOOL EnumDisplaySettings(
LPCTSTR lpszDeviceName, // display device
DWORD iModeNum, // graphics mode
LPDEVMODE lpDevMode // graphics mode settings);
How to open a window on a specific display in Windows?
//VisualAPP Open
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = _T("open");
sei.lpFile = _T("C:\\Users\\Administrator\\Desktop\\xxx.lnk");
sei.nShow = SW_SHOW;
if(ShellExecuteEx(&sei)) // Execute successfully
{
MoveApp(); // Moving program window
if (sei.hProcess)
WaitForSingleObject(sei.hProcess, INFINITE);
}
else
{
CString s;
s.Format(_T("ShellExecuteEx error,errer code:%d"), GetLastError());
// s = GetLastError();
MessageBox(s);
}
and the like...
There was a lot of enthusiastic user data that was very helpful to me, now how do I do development in VC6 is also important.
There is no problem in Visual stubio 2019, but not in Visual c++ 6.0 (maybe different support standards).
It made my feature development very difficult, and we were using VC6.
We cannot upgrade vc6 to vs19(VSCode) in a short time. Do you have a better way to solve the imminent problem.
This is probably the most important thing for ‘me to achieve immortality ’

'Specific' Double buffering with this code?

For this code I am trying to implement double buffering so that it does not blink when updating the std::cout on my console window in windows 10. What is the best way to implement this into my current code? I am looking at some Microsoft documentation but I can't figure out a way to merge it so to speak?
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
homeCoords.X = 0;
homeCoords.Y = 0;
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition(hStdOut, homeCoords);
}
The basic idea is to call CreateConsoleScreenBuffer to create an off-screen buffer. Then clear/fill it as needed by passing the handle to that screen buffer when you do your calls to FillConsoleOutputCharacter, FillConsoleOutputAttribute, etc. When it's ready for the user to view, call SetConsoleActiveScreenBuffer to make it the active buffer for the console.
Note that in most cases you won't want to create a new screen buffer every time you clear the screen--rather you'll probably want to create two screen buffers when you start your program, and alternate between the two as you write and display output.

Get all position values from CScrollBar

Trying to use a CScrollBar in my MFC C++ application for Windows7.
I receive all messages just fine and have a handler that looks something like this:
void Dialog::OnHScroll(UINT nSBCode, UINT apos, CScrollBar* pScrollBar)
{
SCROLLINFO si;
si.cbSize = sizeof( si );
si.fMask = SIF_TRACKPOS;
m_slider.GetScrollInfo(&si,SIF_TRACKPOS|SIF_POS|SIF_PAGE);
int nTrackPos = si.nTrackPos; //0 except on TB_THUMBTRACK
int nPos = si.nPos; //0 except on TB_THUMBTRACK
UINT nPage = si.nPage; //seems correct always but I dont need it
The reason I try to extract the position using GetScrollInfo is because they may be bigger than what fits inside a 16bit var, and therefore I cannot use the pos being passed as the argument.
My problem however is that I only get a valid position when dragging the bar and receiving the TB_THUMBTRACK as well as the ending TB_ENDTRACK for drag operations. If I click in the scrollbar or use the arrows at each end all positions (argument pos, and everything in the SCROLLINFO struct except page) will be 0.
Does anyone know how to get the correct positions for all messages? Ie TB_LINEUP, TB_LINEDOWN etc.
Take a look at the sample code for the WM_HSCROLL event handler that's shown in MSDN:
MSDN Documentation

C++ simulate left mouse click on minimized program

I have been searching for a bit about this particular problem I am having, I want to be able to simulate a left mouse click on a program that I am currently attached to.
Right now, I create a thread that checks a database for certain values, and when those values come back (the ones I am looking for), I want to be able to then send a left mouse click in any x,y coord of the program (while minimized).
How can this be done for Windows 7? Thanks!
EDIT: Here is how I am calling the thread ...
HWND child = GetActiveWindow();
if ( child == NULL )
MessageBox(0,"Couldn't get the child hwnd!","",0);
DWORD ID;
HANDLE thread_check_game = CreateThread ( NULL , 0 , (LPTHREAD_START_ROUTINE) game_check_thread , (LPVOID)child, 0 , &ID ); CloseHandle ( game_check_thread );
and then ...
DWORD WINAPI game_check_thread(LPVOID lpParam) {
HWND Window;
Window = (HWND)lpParam;
// ... some other code ...
// ...
WORD mouseX = 398;
WORD mouseY = 398;
SendMessage(Window,WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(mouseX,mouseY));
SendMessage(Window, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(mouseX, mouseY));
Write("Sent Left Click\n");
ExitThread(0);
return 0;
}
If you want to fire a mouse event in your application, use the SendMessage function, and your message will appear in the window with handle hWnd's message pump.
SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(mousePosX, mousePosY));
You may need to notify for WM_LBUTTONUP, depending on the way you application handles it's mouse events.

Clear Screen Command in C++

I want to clear the screen after user enters some numbers in C++. I'm programming in console application mode.
so how to do it? My OS is win7 and My IDE is CodeBlocks and the Compiler is MingW...
It depends of your OS,
If you use linux:
system("clear");
If you use windows:
system("cls");
but this make your application lees portable, it's preferable to do
cout << string(50, '\n');
this line will print lines to seem like the terminal was 'cleared'.
A good article about that problem:
http://www.cplusplus.com/articles/4z18T05o/
You can use the clrscr() defined in conio.h.
Ways to clear screen the output screen.
you can try system methods E.g. system("CLS");
link the conio.h in your compiler. I forgot how to do that. if you will use clear screen repeatedly put this function.
enter code here
void clrscr()
{
system("cls");
}
That's what Microsoft has to say about clearing a console:
#include <windows.h>
void cls( HANDLE hConsole )
{
COORD coordScreen = { 0, 0 }; // home for the cursor
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
// Get the number of character cells in the current buffer.
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
// Fill the entire screen with blanks.
if( !FillConsoleOutputCharacter( hConsole, // Handle to console screen buffer
(TCHAR) ' ', // Character to write to the buffer
dwConSize, // Number of cells to write
coordScreen, // Coordinates of first cell
&cCharsWritten ))// Receive number of characters written
{
return;
}
// Get the current text attribute.
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}
// Set the buffer's attributes accordingly.
if( !FillConsoleOutputAttribute( hConsole, // Handle to console screen buffer
csbi.wAttributes, // Character attributes to use
dwConSize, // Number of cells to set attribute
coordScreen, // Coordinates of first cell
&cCharsWritten )) // Receive number of characters written
{
return;
}
// Put the cursor at its home coordinates.
SetConsoleCursorPosition( hConsole, coordScreen );
}
int main()
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
cls(hStdout);
return 0;
}
system("cls");
Brilliant. So what happens if I replace the Windows cls with my own malicious cls? You've just given me control, thanks! This is what's called a back door, and you left it wide open by using an insecure technique.
Source: http://www.daniweb.com/software-development/cpp/threads/76934/how-do-i-clear-my-screen-in-c.
One method is to output '\f' (corresponding to the ASCII form feed character, code 12, which is used by line printers to eject a page, and recognized by some common terminals and emulators as a clear screen).
That won't work on Windows.
#ifdef _WIN32
/* windows hack */
#else
std::cout << '\f' std::flush;
#endif