I am trying to make a fun program where it display random numbers, but I need to remove the scrollbar so it looks more convincing. I managed to make the program full screen but I can't remove the vertical scrollbar. Screenshot
Code:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
There are many ways, one of them is manipulating the size of the internal buffer of the console to have the same size of the window and then using ShowScrollBar function to remove the scrolls.
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hstdout, &csbi);
csbi.dwSize.X = csbi.dwMaximumWindowSize.X;
csbi.dwSize.Y = csbi.dwMaximumWindowSize.Y;
SetConsoleScreenBufferSize(hstdout, csbi.dwSize);
HWND x = GetConsoleWindow();
ShowScrollBar(x, SB_BOTH, FALSE);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
Another way is to rely on conio.h or another C/C++ header/library which implements user interface functions.
Related
I am a new user of C++ and do not know all about types of variables.
I have this code but it doesn't work normally. For normal i mean - after starting cursor must be random moves for -25 to 25 pixel of screen.
Sorry if i provided few information. Ask me i can send what you want. And sorry for my bad English.
#include <iostream>
#include "MyForm1.h"
#include <windows.h>
#include <cstdlib>
#include <winuser.h>
#include <playsoundapi.h>
using namespace System;
using namespace System::Windows::Forms;
using namespace std;
// Cursor random moving here :3
int shakecursor() {
POINT p;
int __cdecl GetCursorPos(int &p);
cout << p.x << p.y << endl;
int x_p1;
int y_p1;
x_p1 = rand() % 0 -25;
y_p1 = rand() % 0 -25;
int x_p = p.x + x_p1;
int y_p = p.y + y_p1;
int __cdecl SetCursorPos(int x_p1, int y_p1);
Sleep(10);
return 0;
}
[STAThreadAttribute]
int main(cli::array<System::String ^> ^args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
My3yPaB::MyForm mainForm;
Application::Run(%mainForm);
bool shaking = true;
while (shaking = true) {
shakecursor();
}
}```
These
int __cdecl GetCursorPos(int &p);
int __cdecl SetCursorPos(int x_p1, int y_p1);
are not function calls. they are function declarations.
Instead it seems you mean
GetCursorPos( p );
and
SetCursorPos( x_p, y_p );
So i fixed this problem and program doesn't do anything here code:
#include <iostream>
#include "MyForm1.h"
#include <windows.h>
#include <cstdlib>
#include <winuser.h>
#include <Mmsystem.h>
#include <playsoundapi.h>
#pragma comment (lib, "User32.lib")
using namespace System;
using namespace System::Windows::Forms;
using namespace std;
int shakecursor() {
POINT p;
GetCursorPos(&p);
cout << p.x << p.y << endl;
int x_p1;
int y_p1;
x_p1 = rand() % 51 - 25;
y_p1 = rand() % 51 - 25;
int x_p = p.x + x_p1;
int y_p = p.y + y_p1;
SetCursorPos(x_p, y_p);
Sleep(10);
return 0;
}
[STAThreadAttribute]
int main(cli::array<System::String ^> ^args) {
//PlaySound(L"start.mp3", NULL, NULL);
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
My3yPaB::MyForm mainForm;
Application::Run(%mainForm);
bool shaking = true;
while (shaking = true) {
shakecursor();
}
}```
I have a c++ program that generates random files filled with gibberish, but for it to work it needs to run in the background. The method I am using generates a null window. I have made other programs using this background method, but it doesn't work in this program:
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <fstream>
#include <windows.h>
using namespace std;
string random(int len)
{
string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string r;
srand(time(NULL));
for(int i = 0; i < len; i++) r.push_back(a.at(size_t(rand() % 62)));
return r;
}
int main(){
restart:
/*This is the background code*/
HWND window;
AllocConsole();
window - FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(window, 0);
std::string file=random(1);
std::ofstream o(file.c_str());
o << random(999) << std::endl;
goto restart;
return 0;
}
I am using the dev C++ compiler
I just realized my problem, the goto statement needed to not include the null window rendering part, so the window wasn't re-rendered and de-rendered every time. Also there was a - that needed to be an =.
In Windows, winapi provides a function that reports information about a monitor:
DEVMODE dm;
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm);
int FPS = dm.dmDisplayFrequency;
What is the equivalent of this on Linux? The Linux man pages direct me to an allegro library function, but not only am I not using allegro, that function is from a very outdated version of said library and reportedly only works on Windows.
Use XRandr API (man 3 Xrandr). See here for an example:
http://www.blitzbasic.com/Community/posts.php?topic=86911
You can also look at the code for xrandr(1).
Edit1: For posterity sake:
Sample code slightly adjusted so its more of a demo:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
int main()
{
int num_sizes;
Rotation current_rotation;
Display *dpy = XOpenDisplay(NULL);
Window root = RootWindow(dpy, 0);
XRRScreenSize *xrrs = XRRSizes(dpy, 0, &num_sizes);
//
// GET CURRENT RESOLUTION AND FREQUENCY
//
XRRScreenConfiguration *conf = XRRGetScreenInfo(dpy, root);
short current_rate = XRRConfigCurrentRate(conf);
SizeID current_size_id = XRRConfigCurrentConfiguration(conf, ¤t_rotation);
int current_width = xrrs[current_size_id].width;
int current_height = xrrs[current_size_id].height;
std::cout << "current_rate = " << current_rate << std::endl;
std::cout << "current_width = " << current_width << std::endl;
std::cout << "current_height = " << current_height << std::endl;
XCloseDisplay(dpy);
}
Compile with:
g++ 17797636.cpp -o 17797636 -lX11 -lXrandr
Output:
$ ./17797636
current_rate = 50
current_width = 1920
current_height = 1080
A simple example:
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
int main(int argc, char *argv[])
{
Display *display = XOpenDisplay(NULL);
Window default_root_window = XDefaultRootWindow(display);
XRRScreenResources *screen_resources = XRRGetScreenResources(display, default_root_window);
RRMode active_mode_id = 0;
for (int i = 0; i < screen_resources->ncrtc; ++i) {
XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(display, screen_resources, screen_resources->crtcs[i]);
// If None, then is not displaying the screen contents
if (crtc_info->mode != None) {
active_mode_id = crtc_info->mode;
}
}
double active_rate = 0;
for (int i = 0; i < screen_resources->nmode; ++i) {
XRRModeInfo mode_info = screen_resources->modes[i];
if (mode_info.id == active_mode_id) {
active_rate = (double)mode_info.dotClock / ((double)mode_info.hTotal * (double)mode_info.vTotal);
}
}
printf("Active rate is: %.1f\n", active_rate);
return 0;
}
Iwan's answer did not work for me; xrandr has changed since 2013 I guess? The command-line tool xrandr can read my refresh rate correctly, but its source code is too complex for me to be willing to copy the way it's doing so. Instead I have chosen to clumsily delegate the work to the entire xrandr program. My crappy solution is pasted below.
Note that this solution is likely to be unreliable when multiple display devices are connected, and will probably someday break when xrandr changes again.
(pstream.h is provided by Jonathan Wakely's PStreams library, referenced here: https://stackoverflow.com/a/10702464/1364776
I'm only using it to turn the output of a command into a std::string; obviously there are various other ways to do that so use one of them if you prefer.)
#include <pstream.h>
#include <cctype>
#include <cstdlib>
#include <cmath>
float getRefreshRate()
{
try
{
redi::ipstream queryStream("xrandr");
std::string chunk;
while (queryStream >> chunk)
{
auto rateEnd = chunk.find("*");
if (rateEnd != std::string::npos)
{
auto rateBeginning = rateEnd;
while (std::isdigit(chunk[rateBeginning]) || std::ispunct(chunk[rateBeginning]))
--rateBeginning;
++rateBeginning;
auto numberString = chunk.substr(rateBeginning, rateEnd - rateBeginning);
float rate = std::strtof(numberString.data(), nullptr);
if (rate != 0 && rate != HUGE_VALF)
return rate;
}
}
}
catch (...)
{
}
return 60; // I am not proud of any of this :(
}
I'm currently learning c++,and the code went so far,until
i made array variables to call a function instead of string,but something went wrong,and i can't figure out what it is.Problem is,it couts correctly only first 2 letters,then it couts the rest as └└└└ symbols.
Here's the code:
#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
using namespace std;
int land(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
char H = 72;
cout<<H;
}
int player(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
char X = 88;
cout<<X;
}
/*previously,i declared a string inside worldgen,and generated 2d array inside the for loop,but when i changed variables to call a function,first letters were X and H,but then it went └└└└└└└└└└└└└└└└└└└└└└└ for all the remaining characters.What's wrong here? */
int worldgen(int dimX,int dimY,int x,int y){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
system("TITLE MyTitleText");
int H = land();
int X = player();
string world[dimX][dimY];
for(int c = 0;c<dimY;c++){
for(int count = 0;count<dimX;count++){
world[count][c] = H;
world[x][y] = X;
cout<<world[count][c];
}
cout<<endl;
}
}
int main(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
worldgen(70,15,10,10);
cin.get();
}
Neither land() nor player() return anything so neither H nor X are getting any meaningful data are are just garbage data that was uninitialized.
I'm surprised this is even compiling since you have two functions that are supposed to return ints but aren't set to return anything.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Colorize stdout output to Windows cmd.exe from console C++ app
I am using codeblocks on Windows XP and I need a way to output colored text, and maybe change the color of the background as well in a console application.
I tried the conio.h functions but they don't seem to be compatible with code blocks.
It looks like you'll want to use some Windows API features to accomplish this.
If you were using Cygwin for windows, it'd be a bit easier.
Here's an example courtesy of daniweb:
// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// a Dev-C++ tested console application by vegaseat 07nov2004
#include <iostream>
#include <windows.h> // WinApi header
using namespace std; // std::cout, std::cin
int main()
{
HANDLE hConsole;
int k;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}
cin.get(); // wait
return 0;
}
This piece of code might help:
WinConsole.h
#pragma once
typedef void* HANDLE;
class WinConsole
{
public:
WinConsole(void);
~WinConsole(void);
void SetColour(WORD colour);
WORD GetDefaultColour() const;
void Reset();
private:
HANDLE fConsoleHandle;
CONSOLE_SCREEN_BUFFER_INFO fDefaultScreenBufferInfo;
};
WinConsole.cpp
#include "WinConsole.h"
#define WIN32_LEAN_AND_MEAN
#define <Windows.h>
WinConsole::WinConsole(void)
{
fConsoleHandle = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (INVALID_HANDLE_VALUE != fConsoleHandle)
{
::GetConsoleScreenBufferInfo(fConsoleHandle, &fDefaultScreenBufferInfo);
}
}
WinConsole::~WinConsole(void)
{
}
void WinConsole::SetColour( WORD colour )
{
if (INVALID_HANDLE_VALUE != fConsoleHandle)
{
::CONSOLE_SCREEN_BUFFER_INFO info = { sizeof(CONSOLE_SCREEN_BUFFER_INFO), 0 };
if(::GetConsoleScreenBufferInfo(fConsoleHandle, &info))
{
::SetConsoleTextAttribute(fConsoleHandle, (info.wAttributes & 0xff00)|colour);
}
}
}
void WinConsole::Reset()
{
if (INVALID_HANDLE_VALUE != fConsoleHandle)
{
::SetConsoleTextAttribute(fConsoleHandle, fDefaultScreenBufferInfo.wAttributes);
}
}
WORD WinConsole::GetDefaultColour() const
{
if (INVALID_HANDLE_VALUE != fConsoleHandle)
{
return (WORD)(fDefaultScreenBufferInfo.wAttributes & 0x00ff);
}
return e_FGRed | e_FGGreen | e_FGBlue;
}
Usage:
WinConsole console;
console.SetColour(FOREGROUND_RED|BACKGROUND_BLUE); // Superman style ;)