SetWindowPos behaviour for Notepad.exe on multi-moniter setup

Lebon333 1 Reputation point
2022-04-11T06:34:38.673+00:00

Hi. I saw some strange behaviour while trying to move Notepad window using SetWindowPos. The issue is that in multi-monitor setup, when I pass x=-1 to SetWindowPos call, the window not only moves in x-direction, but also move in y-direction (sort of creeps up). It happens on other windows like browsers etc. I would like to know why is this the case? (since this doesn't happen when I drag the window with the mouse across the monitor boundary)

I have posted the question with screenshots on stackoverflow here: https://stackoverflow.com/questions/71751167/setwindowpos-behaviour-for-notepad-exe-on-multi-moniter-setup

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,069 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 7,831 Reputation points Microsoft Vendor
    2022-04-11T07:47:51.96+00:00

    Is it normal in C++ console program? because I did not reproduce your problem.
    191821-image.png

    #include <Windows.h>  
    #include <iostream>  
      
    using namespace std;  
      
    int main(void)  
    {  
    	HWND mhwnd = FindWindow(L"Notepad", NULL);  
    	RECT rc;  
    	GetWindowRect(mhwnd, &rc);  
    	cout << "1:(" << rc.left << "," << rc.top << "," << rc.right << "," << rc.bottom << ")" << endl;  
      
    	SetWindowPos(mhwnd, HWND_TOP, 0, 200, 200, 100, SWP_NOREDRAW);  
    	GetWindowRect(mhwnd, &rc);  
    	cout << "2:(" << rc.left << "," << rc.top << "," << rc.right << "," << rc.bottom << ")" << endl;  
      
    	SetWindowPos(mhwnd, HWND_TOP, -1, 200, 200, 100, SWP_NOREDRAW);  
    	GetWindowRect(mhwnd, &rc);  
    	cout << "3:(" << rc.left << "," << rc.top << "," << rc.right << "," << rc.bottom << ")" << endl;  
      
    	SetWindowPos(mhwnd, HWND_TOP, -1, 60, 200, 100, SWP_NOREDRAW);  
    	GetWindowRect(mhwnd, &rc);  
    	cout << "4:(" << rc.left << "," << rc.top << "," << rc.right << "," << rc.bottom << ")" << endl;  
      
    	SetWindowPos(mhwnd, HWND_TOP, -1, 600, 200, 100, SWP_NOREDRAW);  
    	GetWindowRect(mhwnd, &rc);  
    	cout << "5:(" << rc.left << "," << rc.top << "," << rc.right << "," << rc.bottom << ")" << endl;  
      
    	return 0;  
    }