Windows 7 GetRandomRegion not working on Windows7

Leo H

New Member
Joined
May 26, 2010
Messages
2
Using the GetRandomRgn function in windows does not work on Windows 7. The forms borders stay visible.

The following code on XP clears the entire window including borders and enables you to draw on the entire window area.

The code on Windows 7 clears only the client area and enables you to draw on the client area only

IntPtr hrgn, hdc;
hrgn = CreateRectRgn(0, 0, 0, 0);
hdc = GetWindowDC(hWnd);
int res = GetRandomRgn(hdc, hrgn, 4); // the value of SYSRGN is 4.
ReleaseDC(hWnd, hdc);
SetWindowRgn(hWnd, hrgn, true);
 


Solution
In Windows 7, the behavior of the GetRandomRgn function might differ from Windows XP. This could be due to changes in the operating system's handling of regions and window drawing. To replicate the behavior you observed in Windows XP where the entire window area, including the borders, could be cleared to allow drawing across the entire window, you might need to adjust your code slightly to accommodate the changes in behavior. Here's a modified version of your code that you can try on Windows 7 to potentially achieve the same effect as in Windows XP:
C#:
IntPtr hrgn, hdc; hrgn = CreateRectRgn(0, 0, 0, 0); hdc = GetDC(hWnd); int res = GetRandomRgn(hdc, hrgn, 1); // The value of SYSRGN is 1 (alternatively, you might try using 2...
In Windows 7, the behavior of the GetRandomRgn function might differ from Windows XP. This could be due to changes in the operating system's handling of regions and window drawing. To replicate the behavior you observed in Windows XP where the entire window area, including the borders, could be cleared to allow drawing across the entire window, you might need to adjust your code slightly to accommodate the changes in behavior. Here's a modified version of your code that you can try on Windows 7 to potentially achieve the same effect as in Windows XP:
C#:
IntPtr hrgn, hdc; hrgn = CreateRectRgn(0, 0, 0, 0); hdc = GetDC(hWnd); int res = GetRandomRgn(hdc, hrgn, 1); // The value of SYSRGN is 1 (alternatively, you might try using 2 instead of 1) ReleaseDC(hWnd, hdc); SetWindowRgn(hWnd, hrgn, true);
In this modified version of the code:
  • The GetRandomRgn function call uses 1 as the region type instead of 4. This value (SYSRGN = 1) might work better to encompass the entire window area, including the borders, on Windows 7.
  • You should use GetDC instead of GetWindowDC as the latter is discouraged in modern Windows programming.
  • Adjusting the region type parameter from 4 to 1 may help encompass the entire window including its borders, similar to the behavior you observed on Windows XP.
By making these adjustments, you might have a better chance of achieving the desired effect on the window in Windows 7. Please give this modified code a try and see if it helps in clearing the entire window area for drawing.
 


Solution
Back
Top