Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For Windows:

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-enumdisplaymonitors

For Windows:

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-enumdisplaymonitors

#include <windows.h>
#include <iostream>
using namespace std;
 size_t monitor_count = 0;
BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
monitor_count++;
return TRUE;
}
int main(void)
{
// Method 1
cout << GetSystemMetrics(SM_CMONITORS) << endl;
// Method 2
EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, 0);
cout << monitor_count << endl;
return 0;
}

For Windows:

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    // Method 1
    cout << GetSystemMetrics(SM_CMONITORS) << endl;

    // Method 2
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, 0);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use this version instead:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    (*reinterpret_cast<size_t *>(Arg4))++;
    return TRUE;
}

int main(void)
{
    // Method 1
    cout << GetSystemMetrics(SM_CMONITORS) << endl;

    // Method 2
    size_t monitor_count = 0;
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));
    cout << monitor_count << endl;

    return 0;
}

For Windows:

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    // Method 1
    cout << GetSystemMetrics(SM_CMONITORS) << endl;

    // Method 2
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, 0);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use this version instead:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    (*reinterpret_cast<size_t *>(Arg4))++;
    return TRUE;
}

int main(void)
{
    // Method 1
    cout << GetSystemMetrics(SM_CMONITORS) << endl;

    // Method 2
    size_t monitor_count = 0;
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));
    cout << monitor_count << endl;

    return 0;
}

For Windows:

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, 0);
NULL);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use this version instead:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    (*reinterpret_cast<size_t *>(Arg4))++;
    return TRUE;
}

int main(void)
{
    size_t monitor_count = 0;
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));
    cout << monitor_count << endl;

    return 0;
}

For Windows:

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, NULL);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use this the following version instead:instead. It isn't intentionally obfuscated, it's just plain old pointer use:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    (*reinterpret_cast<size_t *>(Arg4))++;
    return TRUE;
}

int main(void)
{
    size_t monitor_count = 0;
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));
    cout << monitor_count << endl;

    return 0;
}

For Windows:The following version for Windows uses a global variable to keep track of the number of monitors (real and virtual):

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, NULL);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use the following version instead. It isn't intentionally obfuscated, it's just plain old pointer use:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    (*reinterpret_cast<size_t *>(Arg4))++;
    return TRUE;
}

int main(void)
{
    size_t monitor_count = 0;
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));
    cout << monitor_count << endl;

    return 0;
}

The following version for Windows uses a global variable to keep track of the number of monitors (real and virtual):

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, NULL);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use the following version instead. It isn't intentionally obfuscated, it's just plain old pointer use:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    // Regarding pointer use:
    // 1) Reinterpret the LPARAM as a size_t*
    // 2) Dereference the size_t* to get the variable
    // 3) Add one to the value of the variable
    (*reinterpret_cast<size_t *>(Arg4))++;
     return TRUE;
}

int main(void)
{
    size_t monitor_count = 0;

    // Regarding the fourth parameter:
    // 1) Get address of variable
    // 2) Reinterpret the size_t* as an LPARAM
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));
     cout << monitor_count << endl;

    return 0;
}

The following version for Windows uses a global variable to keep track of the number of monitors (real and virtual):

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, NULL);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use the following version instead. It isn't intentionally obfuscated, it's just plain old pointer use:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    // Regarding pointer use:
use and the fourth parameter:
    // 1) Reinterpret the LPARAM as a size_t*
    // 2) Dereference the size_t* to get the variable
    // 3) Add one to the value of the variable
    (*reinterpret_cast<size_t *>(Arg4))++;

    return TRUE;
}

int main(void)
{
    size_t monitor_count = 0;

    // Regarding the fourth parameter:
    // 1) Get address of variable
    // 2) Reinterpret the size_t* as an LPARAM
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));

    cout << monitor_count << endl;

    return 0;
}

The following version for Windows uses a global variable to keep track of the number of monitors (real and virtual):

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, NULL);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use the following version instead. It isn't intentionally obfuscated, it's just plain old pointer use. I've included comments regarding this pointer use:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    // Regarding pointer use and the fourth parameter:
    // 1) Reinterpret the LPARAM as a size_t*
    // 2) Dereference the size_t* to get the variable
    // 3) Add one to the value of the variable
    (*reinterpret_cast<size_t *>(Arg4))++;

    return TRUE;
}

int main(void)
{
    size_t monitor_count = 0;

    // Regarding the fourth parameter:
    // 1) Get address of variable
    // 2) Reinterpret the size_t* as an LPARAM
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));

    cout << monitor_count << endl;

    return 0;
}

The following version for Windows uses a global variable to keep track of the number of monitors (real and virtual):

#include <windows.h>
#include <iostream>
using namespace std;

size_t monitor_count = 0;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    monitor_count++;
    return TRUE;
}

int main(void)
{
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, NULL);
    cout << monitor_count << endl;

    return 0;
}

... or if you abhor global variables, then you can use the following version instead. It isn't intentionally obfuscated, it's just plain old pointer use. I've included comments regarding this pointer use:

#include <windows.h>
#include <iostream>
using namespace std;

BOOL monitor_enum_proc(HMONITOR Arg1, HDC Arg2, LPRECT Arg3, LPARAM Arg4)
{
    // Regarding pointer use and the fourth parameter:
    // 1) Reinterpret the LPARAM as a size_t*
    // 2) Dereference the size_t* to get the variable
    // 3) Add one to the value of the variable
    (*reinterpret_cast<size_t *>(Arg4))++;

    return TRUE;
}

int main(void)
{
    size_t monitor_count = 0;

    // Regarding pointer use and the fourth parameter:
    // 1) Get address of variable
    // 2) Reinterpret the size_t* as an LPARAM
    EnumDisplayMonitors(NULL, NULL, monitor_enum_proc, reinterpret_cast<LPARAM>(&monitor_count));

    cout << monitor_count << endl;

    return 0;
}