I'm messing around with CombineRgn but not its not working...

closed account (3pj6b7Xj)
Ok, so i'm messing around with CombineRgn because it sounded interesting and I want to see what I can do with it and if I can put it to use with my game I am making. First I create two regions, a red one and a blue and then I combine them to make a third one but for some reason the third one won't show up. I see the red and blue one but the third one is no where, here is some code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
CreateApp Window(hInstance,WndProc); /* Create window & point to procedure. */
Window.Center(); /* Center the window on the desktop. */
HWND hWnd = Window.Present(); /* Store the handle and present the window. */

/* Create the regions to experiment with. */
HRGN Region1 = CreateRectRgn(0,0,100,100);
HRGN Region2 = CreateRectRgn(50,50,150,150);

/* Combine Region1 & Region2 & store result in CRegion. */
HRGN CRegion; CombineRgn(CRegion,Region1,Region2,RGN_OR);

/* Create a red, blue & yellow brush. */
HBRUSH hbRedBrush = CreateSolidBrush(RGB(255,0,0));
HBRUSH hbBlueBrush = CreateSolidBrush(RGB(0,0,255));
HBRUSH hbYellowBrush = CreateSolidBrush(RGB(255,255,0));

/* Display the created regions and fill them. */
FillRgn(Window.DC(),Region1,hbRedBrush);
FillRgn(Window.DC(),Region2,hbBlueBrush);

/* Display the combined region. */
FillRgn(Window.DC(),CRegion,hbYellowBrush);


I see Region1 & Region2 on my window but CRegion doesn't show up.
I've tried all the parameters for the type of combine mode and I get
nothing.

Am I doing something wrong? I really want to see how this works!
Sorry for the /* */ comments is an old habit from C programming, lol.
And oh yes, Window.DC() returns an HDC handle to my window, that
is what my handy dandy CreateApp class does, thanks for help guys.
Last edited on
For the CombineRgn function, the destination region must exist.
In your case HRGN CRegion; is not enough.
You have to create the region, then call CombineRgn.

1
2
3
/* Combine Region1 & Region2 & store result in CRegion. */
HRGN CRegion = CreateRectRgn(0,0,0,0) //create the destination region first - 
 CombineRgn(CRegion,Region1,Region2,RGN_OR);
closed account (3pj6b7Xj)
I will try again, thanks. :)
closed account (3pj6b7Xj)
Hey it worked! Here is the resulting code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <windows.h>
#include "createapp.h"

using namespace nsCtApp;

/*  Declare Windows procedure  */
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpszArgument,int nWinState)
{
    CreateApp Window(hInstance,WndProc); /* Create window & point to procedure. */
    Window.Center(); /* Center the window on the desktop. */
    HWND hWnd = Window.Present(); /* Store the handle and present the window. */

    /* Create the regions to experiment with. */
    HRGN Region1 = CreateRectRgn(0,0,100,100);
    HRGN Region2 = CreateRectRgn(50,50,150,150);
    HRGN CRegion1 = CreateRectRgn(0,0,0,0);
    HRGN CRegion2 = CreateRectRgn(0,0,0,0);

    /* Combine Region1 & Region2 & store result in CRegion. */
    CombineRgn(CRegion1,Region1,Region2,RGN_AND);
    CombineRgn(CRegion2,Region1,Region2,RGN_DIFF);

    /* Create red & blue brushes. */
    HBRUSH hbRedBrush = CreateSolidBrush(RGB(255,0,0));
    HBRUSH hbBlueBrush = CreateSolidBrush(RGB(0,0,255));
    
    /* Start the message loop. */
    while (true)
    {
        if (!Window.MessagePump(1))
        {   /* If the window must close, get confirmation. */
            LPCSTR lpszMessage = "Are you sure you want to quit?";
            int iMB = MessageBox(hWnd,lpszMessage,"Message",MB_YESNO|MB_ICONINFORMATION);
            if (iMB == IDYES) break; /* Quit the loop if yes is selected. */
        }

        /* Display the combined region. */
        FillRgn(Window.DC(),CRegion1,hbRedBrush);
        FillRgn(Window.DC(),CRegion2,hbBlueBrush);
    }

    /* Delete created objects. */
    DeleteObject(hbBlueBrush);
    DeleteObject(hbRedBrush);
    DeleteObject(CRegion2);
    DeleteObject(CRegion1);
    DeleteObject(Region2);
    DeleteObject(Region1);

    return 0;
}

LRESULT CALLBACK WndProc /* Window Procedure */
(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
     switch (Message)
     {
          case WM_SYSCOMMAND:
          /* If window must close, post a WM_QUIT message & veto the quit. */
          if ((wParam & 0xFFF0) == SC_CLOSE) { PostQuitMessage(0); return 0; }
         default:
               return DefWindowProc(hWnd,Message,wParam,lParam);
     }
	
     return 0;
}


Thanks a bunch. At first I displayed both squares inside the loop and they flickered like crazy. Using combine region allowed to display the first region and the combined region with out any flicker because they are separate now, now I should experiment more and see what else I can do with this, thanks a for the help! :)
Last edited on
Topic archived. No new replies allowed.