Get the current window position under Windows - c++

I want to get the position of the current window (its a console window). By current I mean the window I'm programming in, for example if the console is in the top left of the screen I should get X = 0, Y = 0. (By the position of the window I mean the top left of the window respective of the monitor)
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
int X, Y;
GetCurrentWindowPos(&X, &Y); /* How do I do this? */
return 0;
}

void GetWindowPos( int *x, int *y ) {
RECT rect = { NULL };
if( GetWindowRect( GetConsoleWindow(), &rect ) ) {
*x = rect.left;
*y = rect.top;
}
}
Cheers

Related

my ncurses window isn't appearing on my terminal

I'm new to Ncurses and I tried to make a program in C++ that makes a window then displays a box and text on both the box and standard screen.
Here is my code
#include <iostream>
#include <ncurses.h>
using namespace std;
int main(){
initscr();
int h,w,y,x;
h = 10;
w = 25;
y = 15;
x = 20;
WINDOW * win = newwin(h ,w, y, x);
box(win,0,0);
wrefresh(win);
printw("Hello");
wprintw(win,"hi");
wrefresh(win);
getch();
refresh();
endwin();
return 0;
}
Any help is greatly appreciated.
It's possible that the problem is with the window's position (y and x coordinates). Set y=0 and x=0 to position the window in the upper-left corner of the screen and see if it appears.
You can also check if ncurses is properly initialised by adding if (!initscr()) endwin(); return 1;} at the beginning of the main function to ensure that ncurses has been successfully initialised.
The updated code is as follows:
#include <iostream>
#include <ncurses.h>
int main() {
if (!initscr()) {
endwin();
return 1;
}
int h = 10, w = 25, y = 0, x = 0;
WINDOW *win = newwin(h, w, y, x);
box(win, 0, 0);
wrefresh(win);
printw("Hello");
wprintw(win, "hi");
wrefresh(win);
getch();
refresh();
endwin();
return 0;
}

No appropriate default constructor available

I'm getting an error that says:
SZ_GameItem - No appropriate default constructor available.
Here's parts of the codes:
main.cpp:
#include <iostream>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_mixer.h"
using namespace std;
#include "SZ_timer.h"
SZ_Timer aTimer;
const int DELTA_TIME = 10;
bool done = false;
#include "SZ_Player.h"
SZ_Player examplePlayer;
#include "SZ_GameItem.h"
SZ_GameItem exampleItem;
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0);
// Creates the game window
SDL_Window* game_window = SDL_CreateWindow("Rise", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
// Creates the game render to draw on the game window.
SDL_Renderer* game_renderer = SDL_CreateRenderer(game_window, 0, 0);
// Game Loop
examplePlayer.Init();
while (!done)
{
aTimer.resetTicksTimer();
examplePlayer.Update();
examplePlayer.Input();
exampleItem.Update();
SDL_SetRenderDrawColor(game_renderer, 0, 0, 20, SDL_ALPHA_OPAQUE);
SDL_RenderClear(game_renderer);
examplePlayer.Render(game_renderer);
exampleItem.Render(game_renderer);
SDL_RenderPresent(game_renderer);
// If less time has passed than allocated block, wait difference
if (aTimer.getTicks() < DELTA_TIME)
{
SDL_Delay(DELTA_TIME - aTimer.getTicks());
}
}
SDL_Quit();
// Exits program
return 0;
}
SZ_GameItem.cpp:
#include "SZ_GameItem.h"
SZ_GameItem::SZ_GameItem(int eX, int eY, int eW, int eH)
{
x = eX;
y = eY;
height = eH;
width = eW;
rect.x = x;
rect.y = y;
rect.h = height;
rect.w = width;
velocity.x = 1;
velocity.y = 0;
}
SZ_GameItem::~SZ_GameItem()
{
}
void SZ_GameItem::Input()
{
}
void SZ_GameItem::Update()
{
rect.x = x;
rect.y = y;
rect.h = height;
rect.w = width;
x = x + velocity.x;
y = y + velocity.y;
}
void SZ_GameItem::Render(SDL_Renderer* pRenderer)
{
SDL_SetRenderDrawColor(pRenderer, 255, 255, 255, 255);
SDL_RenderDrawRect(pRenderer, &rect);
}
SZ_GameItem.h:
#ifndef aGameItem
#define aGameItem
#include <iostream>
#include "SDL.h"
#include "SZ_Vector2D.h"
class SZ_GameItem
{
public:
SZ_GameItem(int x, int y, int w, int h);
~SZ_GameItem();
void Update();
void Input();
void Render(SDL_Renderer* aRenderer);
SZ_Vector2D velocity;
int x, y, height, width;
private:
SDL_Rect rect;
};
#endif
When you write this line:
SZ_GameItem exampleItem;
you actually declare and initialize a variable of type SZ_GameItem. And in this case, it implicitly initializes the variable using the default constructor (that is, which takes no argument), but you haven't provided one. A default constructor can be implicitly defined in some cases, but as you provided a user-defined constructor, with signature SZ_GameItem(int x, int y, int w, int h), the default constructor is not implicitly defined.
What needs be done is to either initialize this variable yourself using the constructor your provided, or provide a default constructor.

How to print sentence in the position of the mouse cursor in VC++ Win32 application?

I want to print something in the position where the mouse cursor is, so I use POINT cursorPos; GetCursorPos(&cursorPos);
to get the position of mouse cursor.
Then I set the console cursor to the position, and print the mouse coordinates. However the result is not correct.
Here's the code:
#include<iostream>
#include<Windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
void gotoxy(int column, int line){
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE),
coord
);
}
int main(){
while (1){
POINT cursorPos;
GetCursorPos(&cursorPos);
system("pause");
gotoxy(cursorPos.x, cursorPos.y);
cout << cursorPos.x << " " << cursorPos.y;
}
}
Thank U~
Use GetConsoleScreenBufferInfo to find the cursor position in console window. See this example
Tracking mouse position in a console program may not be useful. If you really need the position of mouse pointer, you have to convert from desktop coordinates to to console window coordinates.
Get the console window's handle GetConsoleWindow()
Use ScreenToClient to convert mouse pointer position from screen to client. Map the coordinates to CONSOLE_SCREEN_BUFFER_INFO::srWindow
COORD getxy()
{
POINT pt;
GetCursorPos(&pt);
HWND hwnd = GetConsoleWindow();
RECT rc;
GetClientRect(hwnd, &rc);
ScreenToClient(hwnd, &pt);
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO inf;
GetConsoleScreenBufferInfo(hout, &inf);
COORD coord = { 0, 0 };
coord.X = MulDiv(pt.x, inf.srWindow.Right, rc.right);
coord.Y = MulDiv(pt.y, inf.srWindow.Bottom, rc.bottom);
return coord;
}
int main()
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
while (1)
{
system("pause");
COORD coord = getxy();
SetConsoleCursorPosition(hout, coord);
cout << "(" << coord.X << "," << coord.Y << ")";
}
}

Linux terminal not displaying anything with ncurses

I'm trying to make a library to simplify the ncurses use to display colors. I'm doing it object-oriented so it'll easy to handle changes in the future. But the problem is that I can't get to work this code.
#include <ncurses.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <iostream>
using namespace std;
class ColorWindow {
private:
bool canColor;
WINDOW* container;
int height, width, startx, starty;
public:
ColorWindow() {
if(has_colors()) {
canColor=true;
}
this->height = 20;
this->width = 84;
this->starty = (LINES - height) / 2; /* Calculating for a center placement */
this->startx = (COLS - width) / 2;
}
bool writeStringWithColor(int x, int y, const char* message) {
if(!canColor) {
writeString(3, 5, "Sorry, your term can't show colors.");
return false;
}
init_pair(1, COLOR_RED, COLOR_BLACK);
writeString(0, 10, "aaaaaaaaa");
wattron(getContainer(), COLOR_PAIR(1));
writeString(x, y, message);
wattroff(getContainer(), COLOR_PAIR(1));
}
void writeString(int x, int y, const char* message) {
mvwprintw(getContainer(), x, y, message);
}
WINDOW* createNewContainer() {
this->container = newwin(height, width, starty, startx);
wrefresh(this->container); /* Show that box */
return getContainer();
}
WINDOW* getContainer() {
return this->container;
}
void refreshContainer() {
refresh();
wrefresh(this->container); /* Show that box */
}
};
int main() {
ColorWindow cw = ColorWindow();
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everything to me */
keypad(stdscr, TRUE);
start_color();
cw.createNewContainer();
bool success = cw.writeStringWithColor(0, 10, "Hello everyone in color!!");
if(!success)
cw.writeString(0, 10, "Write with color failed :(");
cw.refreshContainer();
sleep(2);
endwin();
return 0;
}
Thanks in Advance.
There are a couple of bugs in your code:
You don't initialize canColor and container, so copying the fields into cw in main has undefined behavior. Fixed by:
ColorWindow() : canColor(false), container(nullptr) {
writeStringWithColor is missing a return statement at the end, also leading to undefined behavior in main. Fixed by:
return true;
at the end of writeStringWithColor.
Your x and y arguments are swapped in the call to mvwprintw in writeString. Fixed by:
mvwprintw(getContainer(), y, x, message);
LINES and COLS are only valid after ncurses is initialized, so your starty and startx values are garbage. Fixed by moving the initialization of cw after the ncurses init code in main:
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everything to me */
keypad(stdscr, TRUE);
start_color();
ColorWindow cw = ColorWindow();
Full program:
#include <ncurses.h>
#include <string.h>
#include <string>
#include <unistd.h>
#include <iostream>
using namespace std;
class ColorWindow {
private:
bool canColor;
WINDOW* container;
int height, width, startx, starty;
public:
ColorWindow() : canColor(false), container(nullptr) {
if(has_colors()) {
canColor=true;
}
this->height = 20;
this->width = 84;
this->starty = (LINES - height) / 2; /* Calculating for a center placement */
this->startx = (COLS - width) / 2;
}
bool writeStringWithColor(int x, int y, const char* message) {
if(!canColor) {
writeString(3, 5, "Sorry, your term can't show colors.");
return false;
}
init_pair(1, COLOR_RED, COLOR_BLACK);
writeString(0, 10, "aaaaaaaaa");
wattron(getContainer(), COLOR_PAIR(1));
writeString(x, y, message);
wattroff(getContainer(), COLOR_PAIR(1));
return true;
}
void writeString(int x, int y, const char* message) {
mvwprintw(getContainer(), y, x, message);
}
WINDOW* createNewContainer() {
this->container = newwin(height, width, starty, startx);
wrefresh(this->container); /* Show that box */
return getContainer();
}
WINDOW* getContainer() {
return this->container;
}
void refreshContainer() {
refresh();
wrefresh(this->container); /* Show that box */
}
};
int main() {
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everything to me */
keypad(stdscr, TRUE);
start_color();
ColorWindow cw = ColorWindow();
cw.createNewContainer();
bool success = cw.writeStringWithColor(0, 10, "Hello everyone in color!!");
if(!success)
cw.writeString(0, 10, "Write with color failed :(");
cw.refreshContainer();
sleep(2);
endwin();
return 0;
}

How do I draw lines on a image using mouse clicks in opencv?

So, after following the advise from the stackexchange users about mouse event, I was able to understand and implement some simple task using mouse clicks. So, the next goal was to draw a simple line using the mouse left click and mouse right click. Unfortunately, I can't see any line after I implemented my program.
int x,y;
Point p(0,0);
Point q(0,0);
Mat xal;
void drawimage()
{
a = q.x - p.x; //i am just checking out the values of a and b to see if the drawimagefunction is being called in the rightmouse click event
b = q.y - p.y;
cout<<" a is :"<<a<<endl;
cout<<"b is:"<<b<<endl;
line(xal,Point(p.x,p.y),Point(q.x,q.y),Scalar(0,0,255),2,8);
}
void onMouse( int event, int x, int y, int f, void* )
{
switch (event)
{
case EVENT_LBUTTONDOWN:
cout<<"Left button was pressed"<<x<<" "<<y<<" "<<endl;
{
p.x = x;
p.y = y;
cout<<"p is:"<<p.x<<p.y<<endl;
}
break;
case EVENT_RBUTTONDOWN:
cout<<"Right button was pressed at :"<<x <<" "<<y<<endl;
{
q.x = x;
q.y = y;
drawimage();//no line is being drawn though i can see that i get the values of a and b in the drawimage function.
}
break;
default:
break;
}
}
int main()
{
xal = imread("pic.JPG);
namedWindow("Picture",1);
setMouseCallback("Picture",onMouse,NULL);
imshow("Picture",xal);
cvwaitkey(0);
}
Add the following after your "line(..)" call in your drawLine() function:
imshow("Picture", xal);
The problem is that you are writing the line to the xal matrix, but you have not updated the image on the screen, which is what the imshow(..) call will do.
Try this one code. It is useful for you.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void drawimage()
{
line(xal,Point(p->x,p->y),Point(q->x,q->y),Scalar(0,0,255),2,8);
}
void CallBackFunc(int event, int x, int y, int flags, void *ptr )
{
if ( event == EVENT_LBUTTONDOWN )
{
Point*p = (Point*)ptr;
p->x = x;
p->y = y;
drawimage();
}
else if ( event == EVENT_RBUTTONDOWN )
{
Point*q = (Point*)ptr;
q->x = x;
q->y = y;
drawimage();
}
}
int main(int argc, char** argv)
{
// Read image from file
Point p;
Point q;
Mat xal = imread("MyPic.JPG");
//if fail to read the image
if ( xal.empty() )
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc,&p);
setMouseCallback("My Window", CallBackFunc,&q);
//show the image
imshow("My Window", xal);
// Wait until user press some key
waitKey(0);
return 0;
}