これは、あなたが説明したことを行う、完全にマルチモニター対応でタスクバー対応の実装です。
#include <Windows.h>
int main()
{
ClampConsoleToScreen();
return 0;
}
void ClampConsoleToScreen()
{
HWND window = GetConsoleWindow();
RECT windowRect;
GetWindowRect(window, &windowRect);
HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO mi;
memset(&mi, 0, sizeof(mi));
mi.cbSize = sizeof(mi);
GetMonitorInfo(monitor, &mi);
int adj, any;
adj = 0;
any = 0;
if (windowRect.right > mi.rcWork.right)
{
// Get negative adjustment value to move it left onto screen
adj = mi.rcWork.right - windowRect.right;
}
if (windowRect.left < mi.rcWork.left)
{
// Get positive adjustment value to move it right onto screen
adj = mi.rcWork.left - windowRect.left;
}
windowRect.left += adj;
windowRect.right += adj;
any |= adj;
adj = 0;
if (windowRect.bottom > mi.rcWork.bottom)
{
// Get negative adjustment value to move it up onto screen
adj = mi.rcWork.bottom - windowRect.bottom;
}
if (windowRect.top < mi.rcWork.top)
{
// Get positive adjustment value to move it down onto screen
adj = mi.rcWork.top - windowRect.top;
}
windowRect.top += adj;
windowRect.bottom += adj;
any |= adj;
if (any)
{
MoveWindow(window,
windowRect.left,
windowRect.top,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top, TRUE);
}
}