How to make a C++ program run maximized automatically? - visual-c++

I need to know whether there is a code for a C++ program to automatically maximize the program window since I always have to maximize the window when I run the program.
I'm using Windows 7.
I am very much new to C++.
Can someone help me? Thanks.

Try this It will Work
#include "stdafx.h"
#include "conio.h"
#include "Windows.h"
#include "tchar.h"
int _tmain(int argc, _TCHAR* argv[])
{
//Write Your Code HERE//
HWND hWnd;
SetConsoleTitle(_T("test"));
hWnd = FindWindow(NULL, _T("test"));
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD NewSBSize = GetLargestConsoleWindowSize(hOut);
SMALL_RECT DisplayArea = {0, 0, 0, 0};
SetConsoleScreenBufferSize(hOut, NewSBSize);
DisplayArea.Right = NewSBSize.X - 1;
DisplayArea.Bottom = NewSBSize.Y - 1;
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
ShowWindow(hWnd, SW_MAXIMIZE);
_getch();
return 0;
}
It Will show your Output in Maximized Window.

Try ShowWindow(SW_MAXIMIZED). You would have to run a program you created, FindWindow(your target) and then invoke ShowWindow(SW_MAXIMIZED) on it. Note that this is achievable through AutoHotkey and no C++.

If you wanna maximize your program when it runs you can use this code in your Main Form
__fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner)
{
WindowState = wsMaximized;
}
Or if you want to maximize your program during codes e.g. pressing a button then you can use this code if it's in you're Main form:
ShowWindow(this->Handle, SW_SHOWMAXIMIZED);
Or this one if you're in a child one :
ShowWindow(Application->Handle, SW_SHOWMAXIMIZED);

This worked for me.
#include <windows.h>
void maximizeWindow(){
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
}

Related

How to use use XNextEvent, XPeekEvent and XPutBackEvent

I am trying to develop a little Linux application to perform measurements directly on the screen i.e. to measure distances and angles in images, documents etc.
To this end I was inspired by How to listen for mouse events in Linux?
and thought I could use XNextEvent or XPeekEvent and possibly XPutBackEvent.
This program responds nicely to mouse events but since it uses XNextEvent the bucket stops there and the event is not sent to the target window and the mouse becomes useless and if I use XPutBackEvent the same event comes back again.
To fix this I replaced XNextEvent with XPeekEvent, which is supposed to send the event up the ladder. This didn't help but made things worse sending an endless stream of the same event, filtering
out equal events didn't help either.
Thanks in advance for any tip to solve this problem.
The problem is basically that the events do not disappear and I don't know how to get rid of them in order to be able to use the mouse. The code below is copied from
How to listen for mouse events in Linux? and somewhat modified.
#include <stdio.h>
#include <X11/Xlib.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
char *key_name[] = {
"first",
"second (or middle)",
"third",
"fourth", // :D
"fivth" // :|
};
int main(int argc, char **argv)
{
Display *display;
XEvent xevent,oldevent;
Window window;
long unsigned int buttontime=0;
if( (display = XOpenDisplay(NULL)) == NULL )
return -1;
window = DefaultRootWindow(display);
XAllowEvents(display, AsyncBoth, CurrentTime);
XGrabPointer(display,
window,
1,
//PointerMotionMask |
ButtonPressMask | ButtonReleaseMask ,
GrabModeAsync,
GrabModeAsync,
None,
None,
CurrentTime);
while(1)
{
XNextEvent(display, &xevent);
//XPeekEvent(display, &xevent);
switch (xevent.type) {
/*case MotionNotify:
printf("Mouse move : [%d, %d]\n", xevent.xmotion.x_root, xevent.xmotion.y_root);
break;*/
case ButtonPress:
//if ((buttontime <= xevent.xbutton.time))
{
printf("Button pressed : %s\n", key_name[xevent.xbutton.button - 1]);
printf("timevent %lu\n",xevent.xbutton.time);
printf("timeold %lu\n",buttontime);
buttontime = xevent.xbutton.time;
buttontime++;
}
break;
case ButtonRelease:
printf("Button released : %s\n", key_name[xevent.xbutton.button - 1]);
break;
}
// XPutBackEvent(display, &xevent);
sleep(1);
}
return 0;
}

How to find out programs communicate?

I will try to explain with example;
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
HWND hwndCalc;
hwndCalc = FindWindow(NULL, "Calculator");
if(!hwndCalc)
return 1;
SendMessage(hwndCalc, WM_COMMAND, MAKEWPARAM(0x84, BN_CLICKED), 0);
return 0;
}
In calculator, when you click a button SendMessage function works. I can find it with ollydbg. But with ollydbg, I can't debug other, normal programs.(exceptions, violations, crashes...)
So i am wondering whether there is any other way to find out how programs work?

libnotify action callback not being called

I'm trying to create a simple notification in gnome that will execute some code when clicked. The code I have below compiles and runs, but clicking on the notification bubble doesn't do anything. All the code samples I've found indicate that this should work.
#include <stdlib.h>
#include <stdio.h>
#include <libnotify/notify.h>
void action(NotifyNotification *n, gchar *action, gpointer data) {
system("gnome-terminal &");
}
int main(int argc, char **argv) {
gtk_init(&argc, &argv);
notify_init("MyApp");
NotifyNotification *notification;
notification = notify_notification_new("mynotification", "Hello", NULL, NULL);
notify_notification_add_action(notification, "DoAction", "default",
(NotifyActionCallback)action, NULL, NULL);
notify_notification_show(notification, NULL);
pause();
}
To compile:
gcc main.c `pkg-config --cflags --libs libnotify`
I'm on RHEL 6.4, gnome 2.82.2. Other apps (e.g. firefox "Downloads completed") are able to create notifications that perform an action when clicked; I'm just not doing it right somehow.
Had the same issue. Apparently you will have to use the GTK loop by either calling gtk_main or one of the equivalent functions instead of using pause/sleep or other non gtk blocking functions.

Array of SlimReaderWriter (SRW) Locks (using new operator)

Maybe this sounds stupid, but I want to create a vector/array of SRWLocks dynamically during runtime. The following code compiles both ways, but the array thing doesn’t work. Probably the question should be, can I create an SRWLock object using the new operator?
If this is not how it’s done, what’s the right way to create an array of SRWLock objects?
#include "stdafx.h"
#include <ppl.h>
#include <Windows.h>
#include <iostream>
using namespace std;
#define NOT_WORKING
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef NOT_WORKING
// The following doesn't work
SRWLOCK *lock = new SRWLOCK[2];
int a = 10;
AcquireSRWLockExclusive(&lock[0]);
cout<<"Exclusive Lock Acquired";
a++;
ReleaseSRWLockExclusive(&lock[0]);
// The following doesn't work either
/*
SRWLOCK *lock_n = new SRWLOCK;
AcquireSRWLockExclusive(lock_n);
cout<<"Exclusive Lock Acquired";
a++;
ReleaseSRWLockExclusive(lock_n);
*/
#else
// This works
SRWLOCK lock;
int a = 10;
AcquireSRWLockExclusive(&lock);
cout<<"Exclusive Lock Acquired";
a++;
ReleaseSRWLockExclusive(&lock);
#endif
cout<<"Value of a:"<<a;
cin.get();
return 0;
}
User1577008's own answer:
Got it! One needs to use the InitializeSRWLock() function when lock objects are created dynamically. Somebody paste this answer, and help me close this question.

windows api: how to find active thread count in current process?

I have written a server based on boost::asio in windows platform. I want to log currently active thread count of my application at regular intervals.
I can see the thread count for my application in windows task manager's process view in Threads column. is there a windows api to get the same?
after futile googling for sometime i thought its best to seek advice from SO.
Raymond Chen has the answer, based on the Tool Help Library.
For the sake of completeness, his sample code is as follows:
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
int __cdecl main(int argc, char **argv)
{
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (h != INVALID_HANDLE_VALUE) {
THREADENTRY32 te;
te.dwSize = sizeof(te);
if (Thread32First(h, &te)) {
do {
if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
sizeof(te.th32OwnerProcessID)) {
printf("Process 0x%04x Thread 0x%04x\n",
te.th32OwnerProcessID, te.th32ThreadID);
}
te.dwSize = sizeof(te);
} while (Thread32Next(h, &te));
}
CloseHandle(h);
}
return 0;
}

Resources