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?

Recommended Answers

All 5 Replies

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);
    }
}

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.

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?

I use this code

What for? Bringing the console window to the foreground? Doesn't my code work?

what does the zeroes mean?

Everything is explained here -> http://msdn.microsoft.com/en-us/library/ms646260(v=vs.85).aspx

Is there a better way to do it?

Sure, use SendInput instead -> http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx

You can find all the information you need about windows functions on that site.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.