Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - Applying a Region for a Window
Author:Arkon
Category:Win32API
File Size:~ 1.34 KB
Uploaded at:10-Sep-06 04:42:18 pm
Description:
  Applying a round rectangle region to a normal window (for both clipping/painting).
This is a simple case, you might want to use polygons too.
  
hWnd = CreateWindow(...
RECT rect;
// Notice we don't use GetClientRect,
//this is because we want the Window size and not the Client size.
GetWindowRect(hWnd, &rect);
HRGN hRgn = CreateRoundRectRgn(0, 0, rect.right - rect.left, rect.bottom - rect.top, 20, 20);
// Set the new region
SetWindowRgn(hWnd, hRgn, false);

// Now it's time to draw the new window.
ShowWindow(hWnd, ...)

In the WinProc, we will have to process the WM_SIZING message:

case WM_SIZING:{
static POINT lastPos;
POINT p;
GetCursorPos(&p);
// If it's the same old position, ignore this resizing.
if (memcmp(&p, &lastPos, sizeof(POINT))) {
HRGN hRgn = CreateRoundRectRgn(0, 0, ((PRECT)lParam)->right - ((PRECT)lParam)->left, ((PRECT)lParam)->bottom - ((PRECT)lParam)->top, 20, 20);
SetWindowRgn(hWnd, hRgn, true); // Redraw window.
lastPos = p;
return 1; // We processed the message.
}
lastPos = p;
}return 0; // This time, we didn't.
case WM_SIZE:{
if (wParam != SIZE_MINIMIZED && wParam != SIZE_MAXHIDE) {
RECT rect;
GetWindowRect(hWnd, &rect);
HRGN hRgn = CreateRoundRectRgn(0, 0, rect.right - rect.left, rect.bottom - rect.top, 20, 20);
SetWindowRgn(hWnd, hRgn, true); // Redraw window.
}
}return 0;

// Cleanup: none
// The system knows to delete the region object,
// don't touch it once you called SetWindowRgn.
User Contributed Comments(1)
 [1] Spooky | 2013-10-04 19:57:31

When handling WM_SIZING: rather than using `lastPos`, if you store the RECT pointed to by lParam and memcmp to compare the current and previous RECT, it's around ten times faster than GetCursorPos.
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[106236]
2D Rotated Rectangles Collision Detection[79393]
Keyboard Hook[68784]
UDP[52867]
HTTP Proxy[36098]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10613]
PS2 Mouse Driver[6098]
Wave Format Player[4984]
Reading FAT12[4870]
CodeGuru[4561]


All rights reserved to RageStorm © 2009