I made myself a little autoclicker in c++, but after it's done, I want the console window to come up again, so the user can decide wether to do it again, or end the program.
How do I do that?

Piece of cake! Use SetForegroundWindow -> http://msdn.microsoft.com/en-us/library/ms633539(v=vs.85).aspx

#define _WIN32_WINNT 0x0500

#include <windows.h>

int main()
{
    HWND hwnd = GetConsoleWindow();

    int timer_1 = 0;
    int timer_2 = 0;
    int delay = 50;

    while (true)
    {
        if (timer_1 > 100)
        {
            if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

            timer_1 = 0;
        }

        if (timer_2 > 3000)
        {
            SetForegroundWindow(hwnd); Sleep(15);
            SetForegroundWindow(hwnd);

            timer_2 = 0;
        }

        timer_1 += delay;
        timer_2 += delay;

        Sleep(delay);
    }
}

Edited 5 Years Ago by m4ster_r0shi: n/a

Piece of cake! Use SetForegroundWindow -> http://msdn.microsoft.com/en-us/library/ms633539(v=vs.85).aspx

#define _WIN32_WINNT 0x0500

#include <windows.h>

int main()
{
    HWND hwnd = GetConsoleWindow();

    int timer_1 = 0;
    int timer_2 = 0;
    int delay = 50;

    while (true)
    {
        if (timer_1 > 100)
        {
            if (GetAsyncKeyState(VK_ESCAPE) >> 15) break;

            timer_1 = 0;
        }

        if (timer_2 > 3000)
        {
            SetForegroundWindow(hwnd); Sleep(15);
            SetForegroundWindow(hwnd);

            timer_2 = 0;
        }

        timer_1 += delay;
        timer_2 += delay;

        Sleep(delay);
    }
}

Why do you need the #define _WIN32_WINNT 0x0500

HWND hwnd = GetConsoleWindow();
What is the HWND hwnd
Assuming HWND is like a class, then hwnd would be an object?
You can't do class object = blabla, can you :/

I need #define _WIN32_WINNT 0x0500 because I can't use GetConsoleWindow otherwise.
See the remarks section here -> http://msdn.microsoft.com/en-us/library/ms683175(v=vs.85).aspx HWND is nothing but a typedef for HANDLE , which in turn
is a typedef for PVOID , which in turn is a typedef for void * See here -> http://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx

So, hwnd is a pointer to the console window. I need this
because I use it as an argument to SetForegroundWindow.

The program I wrote brings the console window up every three seconds.
Run it, then click somewhere so that the console window disappears, then wait.

Oh, and you can exit the program anytime by hiting esc.

Edited 5 Years Ago by m4ster_r0shi: n/a

I need #define _WIN32_WINNT 0x0500 because I can't use GetConsoleWindow otherwise.
See the remarks section here -> http://msdn.microsoft.com/en-us/library/ms683175(v=vs.85).aspx HWND is nothing but a typedef for HANDLE , which in turn
is a typedef for PVOID , which in turn is a typedef for void * See here -> http://msdn.microsoft.com/en-us/library/aa383751(v=vs.85).aspx

So, hwnd is a pointer to the console window. I need this
because I use it as an argument to SetForegroundWindow.

The program I wrote brings the console window up every three seconds.
Run it, then click somewhere so that the console window disappears, then wait.

Oh, and you can exit the program anytime by hiting esc.

I use this code

mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

Is there a better way to do it? And what does the zeroes mean?

This article has been dead for over six months. Start a new discussion instead.