By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,730 Members | 1,500 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,730 IT Pros & Developers. It's quick & easy.

C++11 Seperately controlled regions

rollerbladegirl
P: 30
In C++11, using codeblocks 17.12, using GNU GCC Compiler, using UNICODE, on Windows XP Professional 32 bit with service pack 2. How do I make the three separate regions work independently of each other as described in the following? Not Visual Studio. Not ".net". Not C#.

If I have three simple images "a.bmp", "b.bmp", "c.bmp";
and I want them to each be regions with the white part not included;
and I want to be able to move each around on the screen independent of the others;
and I want the blue one to remain in front;
and I want the red one and the green one to be in front of each other when I right-mouse-click on them, but not in front of the blue one; I am trying to do this with Dialog boxes.
then how do I do this?

I have tried a lot of examples and book instructions, and now I want better help.
2 Weeks Ago #1
Share this Question
Share on Google+
4 Replies


rollerbladegirl
P: 30
Here is where I am:

Expand|Select|Wrap|Line Numbers
  1. #ifndef IDC_STATIC
  2.     #define IDC_STATIC (-1)
  3. #endif
  4.  
  5. #ifndef _UNICODE
  6.     #define _UNICODE
  7. #endif // _UNICODE
  8.  
  9. #ifndef UNICODE
  10.     #define UNICODE
  11. #endif // UNICODE
  12.  
  13. // Not clear on if I need both _UNICODE and UNICODE .
  14.  
  15. #include "resource.h"
  16.  
  17. #include <windows.h>
  18. //#include <commctrl.h>   // Not clear on if I need this .
  19.  
  20. #define MAX_LOADSTRING 100
  21.  
  22.     // Global Variables:
  23.  
  24.         HINSTANCE InstanceHandleOfMainWindow;    // Instance handle of the main window
  25.  
  26.         TCHAR szTitle[MAX_LOADSTRING];            // The title bar text
  27.             // Why all the extra code for this? Why not just put the title directly into where the window is created?
  28.             // Why not make smaller code without this extra loadstring etc.?
  29.  
  30.         HWND Handle_MainWindow = NULL;          // Handle to main application window
  31.  
  32.         HRGN hRgn = NULL;                       // Handle to region
  33.  
  34.         HDC hdcDest;                            // Dest DC
  35.  
  36.         HDC hdcMem;                             // Memory DC
  37.  
  38.         BITMAP bmpInfo;                         // Buffer to hold bitmap data
  39.  
  40.         BOOL mouseState = 0;                    // Global variable for moving region using mouse
  41.  
  42.         POINT CursPt = {0};                     // Global variable for moving region using mouse
  43.  
  44.         wchar_t WindowClass_for_WinMain[MAX_LOADSTRING] = L"(myWindowClass)";
  45.  
  46.     // Forward declarations of functions included later in this code module:
  47.  
  48.         ATOM                RegisterTheMainWindowClass(HINSTANCE hInstance2);
  49.             // Why rip this out of the WinMain and make a big deal of it? Why not just leave it in winMain?
  50.  
  51.         BOOL                DidTheMainWindowClassRegisterCorrectly(HINSTANCE, int);
  52.  
  53.         LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  54.  
  55.         LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  56.  
  57.     // Empty function to be used later to create region based on the bitmap.
  58.  
  59.         void createRegion(HWND);
  60.  
  61.     // Empty Function to be used later to paint window regions.
  62.  
  63.         void paintRegion();
  64.  
  65.     // Empty Dialog callback procedure declaration to be used later.
  66.  
  67.         INT_PTR CALLBACK DialogProc( HWND Handle_DialogBox1, UINT uMsg, WPARAM wParam, LPARAM lParam );
  68.  
  69.  
  70. int APIENTRY WinMain(HINSTANCE hInstance1, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  71.  
  72.     {
  73.         MSG msg;
  74.  
  75.         RegisterTheMainWindowClass(hInstance1);
  76.  
  77.         if (!DidTheMainWindowClassRegisterCorrectly (hInstance1, nCmdShow))
  78.             {
  79.                 return FALSE;
  80.             }
  81.  
  82.         // Message loop
  83.         while (GetMessage(&msg, nullptr, 0, 0))
  84.             {
  85.                 DispatchMessage(&msg);
  86.             }
  87.  
  88.         return msg.wParam;
  89.  
  90.     }   //int APIENTRY WinMain(HINSTANCE hInstance1, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  91.  
  92.  
  93. ATOM RegisterTheMainWindowClass(HINSTANCE hInstance3)
  94.  
  95.     // An ATOM is (always?) created when the class is registered.
  96.     // This might be a secure reference to window class in that it is special (?) to the kernel.
  97.     // Maybe (typedef unsigned short ATOM;)
  98.     // ATOM is a 16-bit Windows handle-like primitive.
  99.         // It's value is completely opaque to user-mode. (whatever that means)
  100.         // It is not a pointer or an index.
  101.     // They are indexes into a handle table in kernel mode.
  102.         // They are deliberately opaque because leaking kernel mode
  103.         // pointers to user-mode is a security violation (whatever that means).
  104.     {
  105.         WNDCLASSEX wcex;
  106.  
  107.         wcex.cbSize = sizeof(WNDCLASSEX);
  108.  
  109.         wcex.style            = 0; // CS_HREDRAW | CS_VREDRAW;
  110.             // CS_HREDRAW is for HORIZONTAL;
  111.                 // It redraws the entire window if a movement or size adjustment changes the width of the client area.
  112.             // CS_VREDRAW is for VERTICAL;
  113.                 // It redraws the entire window if a movement or size adjustment changes the height of the client area.
  114.         wcex.lpfnWndProc    = (WNDPROC)WndProc;
  115.         wcex.cbClsExtra        = 0;
  116.         wcex.cbWndExtra        = 0;
  117.         wcex.hInstance        = hInstance3;
  118.         wcex.hIcon            = nullptr;
  119.         wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
  120.         wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
  121.         wcex.lpszMenuName    = nullptr;
  122.         wcex.lpszClassName    = WindowClass_for_WinMain;
  123.         wcex.hIconSm        = nullptr;
  124.  
  125.         // Return type is ATOM.
  126.         return RegisterClassEx(&wcex);
  127.  
  128.     }   //ATOM RegisterTheMainWindowClass(HINSTANCE hInstance3)
  129.  
  130. BOOL DidTheMainWindowClassRegisterCorrectly(HINSTANCE hInstance4, int nCmdShow)
  131.     {
  132.         InstanceHandleOfMainWindow = hInstance4; // Store the instance handle in a previously created global variable
  133.  
  134.         Handle_MainWindow = CreateWindow(   // Should I use CreateWindowEx ?
  135.             WindowClass_for_WinMain,
  136.             szTitle,
  137.             WS_OVERLAPPEDWINDOW,
  138.             CW_USEDEFAULT,
  139.             0,
  140.             100,
  141.             150,
  142.             NULL,
  143.             NULL,
  144.             hInstance4,
  145.             NULL);
  146.  
  147.         if (!Handle_MainWindow)
  148.             {
  149.                 return FALSE;
  150.             }
  151.  
  152.         // Create a dialog box
  153.         HWND Handle_DialogBox2 = CreateDialog(hInstance4, MAKEINTRESOURCE(IDD_DIALOG1), Handle_MainWindow, (DLGPROC)DialogProc);
  154.  
  155.         // Create a region
  156.         createRegion(Handle_DialogBox2);
  157.  
  158.         // Don't have to show or update the window ?
  159.         // ShowWindow(hWnd, nCmdShow);
  160.         // UpdateWindow(hWnd);
  161.  
  162.         return TRUE;
  163.  
  164.     }   //BOOL DidTheMainWindowClassRegisterCorrectly(HINSTANCE hInstance4, int nCmdShow)
  165.  
  166.  
  167. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  168.     {
  169.         switch (message)
  170.         {
  171.  
  172.             case WM_DESTROY:
  173.                 {
  174.                     DeleteObject(hRgn);
  175.  
  176.                     DeleteDC(hdcMem);       //Delete the memory DC
  177.  
  178.                     DeleteDC(hdcDest);      //delete the dest DC
  179.  
  180.                     PostQuitMessage(0);
  181.                 }
  182.                 return TRUE;
  183.                 break;
  184.  
  185.             default:
  186.                 {
  187.                 }
  188.                 return DefWindowProc(hWnd, message, wParam, lParam);
  189.        }
  190.        return 0;
  191.  
  192.     }   // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  193.  
  194.  
  195. INT_PTR CALLBACK DialogProc(HWND Handle_DialogBox01, UINT uMsg, WPARAM wParam, LPARAM lParam )
  196.             // Dialog callback procedure
  197.             //                                // INT_PTR is a signed integer memsize-type
  198.             //                                //    that can store a pointer regardless of the platform capacity.
  199.             //    HWND Handle_DialogBox01,    // Temporary (internal to this procedure) Handle to a dialog box
  200.             //    UINT uMsg,                  // Message
  201.             //    WPARAM wParam,              // First message parameter
  202.             //    LPARAM lParam               // Second message parameter
  203.     {
  204.  
  205.         switch (uMsg)
  206.         {
  207.  
  208.             case WM_PAINT:
  209.                 {
  210.                     paintRegion();
  211.  
  212.                     ValidateRect(Handle_DialogBox01,nullptr);   // ValidateRect(hwnd,FALSE);
  213.                 }
  214.                 return TRUE;
  215.                 break;
  216.  
  217.             case WM_MOVE:
  218.                 {
  219.                     paintRegion();
  220.                 }
  221.                 return TRUE;
  222.                 break;
  223.  
  224.  
  225.             case WM_LBUTTONDOWN:
  226.                 {
  227.                     mouseState = 1;
  228.                 }
  229.                 return TRUE;
  230.                 break;
  231.  
  232.             case WM_MOUSEMOVE:
  233.                 {
  234.                     if(mouseState)
  235.                         {
  236.                             GetCursorPos(&CursPt);
  237.                             PostMessage(Handle_DialogBox01, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( CursPt.x, CursPt.y));
  238.                             mouseState = 0;
  239.                         }
  240.  
  241.                 }
  242.                 return TRUE;
  243.                 break;
  244.  
  245.             case WM_RBUTTONDOWN:
  246.                 {
  247.                     // Destroy the dialog box
  248.                     EndDialog(Handle_DialogBox01,0);
  249.  
  250.                     // Send message to destroy the main application window and exit
  251.                     SendMessage(Handle_MainWindow,WM_DESTROY,0,0);
  252.                 }
  253.                 return TRUE;
  254.                 break;
  255.  
  256.             default :
  257.                 {
  258.                     break;
  259.                 }
  260.                 return TRUE;
  261.                 break;
  262.         }
  263.         return FALSE;
  264.  
  265.     }   // INT_PTR CALLBACK DialogProc( HWND Handle_DialogBox01, UINT uMsg, WPARAM wParam, LPARAM lParam )
  266.  
  267. void createRegion(HWND FunctionReferenceTo_DialogBox01)
  268.     {
  269.  
  270.         COLORREF crTransparent;
  271.  
  272.         int iX = 0;
  273.         int iY = 0;
  274.  
  275.         int iRet01 = 0;     // Integer For region 1
  276.         int iRet02 = 0;     // Integer For region 2
  277.  
  278.  
  279.         // Get the destination device context
  280.         hdcDest = GetDC(FunctionReferenceTo_DialogBox01);
  281.  
  282.         // Create a memory DC
  283.         hdcMem = CreateCompatibleDC(nullptr);
  284.  
  285.  
  286.         //----------------------------------------------------------------------
  287.         // START   Load the image files
  288.         //----------------------------------------------------------------------
  289.  
  290.             // a.bmp
  291.  
  292.                 wchar_t imageFile_A[] = L"a.bmp";
  293.  
  294.                 // Load the image
  295.                 HANDLE Handle_Bitmap_A = (HBITMAP)LoadImage(GetModuleHandle(nullptr), imageFile_A, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  296.  
  297.                 if(!Handle_Bitmap_A)
  298.                     {
  299.                         // Handle error here
  300.                         return;
  301.                     }
  302.  
  303.  
  304.             // b.bmp
  305.  
  306.                 wchar_t imageFile_B[] = L"b.bmp";
  307.  
  308.                 // Load the image
  309.                 HANDLE Handle_Bitmap_B = (HBITMAP)LoadImage(GetModuleHandle(nullptr), imageFile_B, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  310.  
  311.             if(!Handle_Bitmap_B)
  312.                 {
  313.                     // Handle error here
  314.                     return;
  315.                 }
  316.  
  317.         //----------------------------------------------------------------------
  318.         // END     Load the image files
  319.         //----------------------------------------------------------------------
  320.  
  321.  
  322.  
  323.         //----------------------------------------------------------------------
  324.         // START   Create Region from imageFile_A
  325.         //----------------------------------------------------------------------
  326.  
  327.             HRGN TemporaryRegion_for_Bitmap_A;
  328.             HRGN FinalTransparentRegionFromBitmap_A;
  329.  
  330.  
  331.             // Get information about the bitmap..
  332.             GetObject(Handle_Bitmap_A, sizeof(bmpInfo), &bmpInfo);    // Get info about the bitmap
  333.  
  334.             // Select the bitmap into the dc
  335.             //hGdiObj = SelectObject(hdcMem, Handle_Bitmap_A);
  336.             SelectObject(hdcMem, Handle_Bitmap_A);
  337.             // Create a region from a bitmap with transparency colour of white
  338.             // change the pixel values for a different transparency color
  339.             // example - RGB(0,0,0) will mean a transparency color of black.. so the areas
  340.             // of the bitmap not used to create the window will be black
  341.             // COLORREF crTransparent = RGB(255, 255, 255);
  342.             crTransparent = RGB(255, 255, 255);
  343.  
  344.  
  345.             iX = 0;
  346.             iY = 0;
  347.             iRet01 = 0;     // For region 2
  348.  
  349.             FinalTransparentRegionFromBitmap_A = CreateRectRgn(0,0,0,0);
  350.  
  351.             for (iY = 0; iY < bmpInfo.bmHeight; iY++)
  352.                 {
  353.                     do
  354.                         {
  355.                             // Skip over transparent pixels at start of lines.
  356.                             while (iX < bmpInfo.bmWidth && GetPixel(hdcMem, iX, iY) == crTransparent)
  357.                                 {
  358.                                     iX++;
  359.                                 }
  360.  
  361.                             // Remember this pixel
  362.                             int iLeftX = iX;
  363.  
  364.                             // Now find first non transparent pixel
  365.                             while (iX < bmpInfo.bmWidth && GetPixel(hdcMem, iX, iY) != crTransparent)
  366.                                 {
  367.                                     ++iX;
  368.                                 }
  369.  
  370.                             // Create a temp region on this info
  371.                             TemporaryRegion_for_Bitmap_A = CreateRectRgn(iLeftX, iY, iX, iY+1);
  372.  
  373.                             // Combine into main region.
  374.                             // From wingdi.h
  375.                             // WINGDIAPI int WINAPI CombineRgn(HRGN,HRGN,HRGN,int);
  376.                             // Combine Region1 & Region2 & store result in ResultRegion.
  377.                             // CombineRgn(ResultRegion,Region1,Region2,RGN_AND);
  378.                             iRet01 = CombineRgn(FinalTransparentRegionFromBitmap_A, FinalTransparentRegionFromBitmap_A, TemporaryRegion_for_Bitmap_A, RGN_OR);
  379.  
  380.  
  381.  
  382.                         }
  383.  
  384.                     while(iX < bmpInfo.bmWidth);
  385.                         {
  386.                             iX = 0;
  387.                         }
  388.                 }
  389.  
  390.             // Center it on current desktop
  391.             iRet01 = SetWindowRgn(FunctionReferenceTo_DialogBox01, FinalTransparentRegionFromBitmap_A, TRUE);
  392.  
  393.                 if(!iRet01)
  394.                     {
  395.                         return;
  396.                     }
  397.  
  398.                 iX = (GetSystemMetrics(SM_CXSCREEN)) / 3 - (bmpInfo.bmWidth / 3);
  399.  
  400.                 iY = (GetSystemMetrics(SM_CYSCREEN)) / 3 - (bmpInfo.bmHeight / 3);
  401.  
  402.                 iRet01 = SetWindowPos(FunctionReferenceTo_DialogBox01, HWND_TOPMOST, iX, iY, bmpInfo.bmWidth, bmpInfo.bmHeight, 0);
  403.  
  404.         //----------------------------------------------------------------------
  405.         // END     Create Region from imageFile_A
  406.         //----------------------------------------------------------------------
  407.  
  408.  
  409. //----------------------------------------------------------------------------------------------------
  410. //----------------------------------------------------------------------------------------------------
  411. //----------------------------------------------------------------------------------------------------
  412.  
  413.  
  414.         //----------------------------------------------------------------------
  415.         // START   Create Region from imageFile_B
  416.         //----------------------------------------------------------------------
  417.  
  418.             HRGN TemporaryRegion_for_Bitmap_B;
  419.             HRGN FinalTransparentRegionFromBitmap_B;
  420.  
  421.  
  422.             // Get information about the bitmap..
  423.             GetObject(Handle_Bitmap_B, sizeof(bmpInfo), &bmpInfo);    // Get info about the bitmap
  424.  
  425.             // Select the bitmap into the dc
  426.             SelectObject(hdcMem, Handle_Bitmap_B);
  427.  
  428.             // Create a region from a bitmap with transparency colour of white
  429.             // change the pixel values for a different transparency color
  430.             // example - RGB(0,0,0) will mean a transparency color of black.. so the areas
  431.             // of the bitmap not used to create the window will be black
  432.             // COLORREF crTransparent = RGB(255, 255, 255);
  433.             crTransparent = RGB(255, 255, 255);
  434.  
  435.  
  436.             iX = 0;
  437.             iY = 0;
  438.             iRet02 = 0;     // For region 2
  439.  
  440.             FinalTransparentRegionFromBitmap_B = CreateRectRgn(0,0,0,0);
  441.  
  442.             for (iY = 0; iY < bmpInfo.bmHeight; iY++)
  443.                 {
  444.                     do
  445.                         {
  446.                             // Skip over transparent pixels at start of lines.
  447.                             while (iX < bmpInfo.bmWidth && GetPixel(hdcMem, iX, iY) == crTransparent)
  448.                                 {
  449.                                     iX++;
  450.                                 }
  451.  
  452.                             // Remember this pixel
  453.                             int iLeftX = iX;
  454.  
  455.                             // Now find first non transparent pixel
  456.                             while (iX < bmpInfo.bmWidth && GetPixel(hdcMem, iX, iY) != crTransparent)
  457.                                 {
  458.                                     ++iX;
  459.                                 }
  460.  
  461.                             // Create a temp region on this info
  462.                             TemporaryRegion_for_Bitmap_B = CreateRectRgn(iLeftX, iY, iX, iY+1);
  463.  
  464.                             // Combine into main region.
  465.                             // From wingdi.h
  466.                             // WINGDIAPI int WINAPI CombineRgn(HRGN,HRGN,HRGN,int);
  467.                             // Combine Region1 & Region2 & store result in ResultRegion.
  468.                             // CombineRgn(ResultRegion,Region1,Region2,RGN_AND);
  469.                             iRet02 = CombineRgn(FinalTransparentRegionFromBitmap_B, FinalTransparentRegionFromBitmap_B, TemporaryRegion_for_Bitmap_B, RGN_OR);
  470.  
  471.                         }
  472.  
  473.                     while(iX < bmpInfo.bmWidth);
  474.                         {
  475.                             iX = 0;
  476.                         }
  477.                 }
  478.  
  479.             // Center it on current desktop
  480.             iRet02 = SetWindowRgn(FunctionReferenceTo_DialogBox01, FinalTransparentRegionFromBitmap_B, TRUE);
  481.  
  482.                 if(!iRet02)
  483.                     {
  484.                         return;
  485.                     }
  486.  
  487.                 iX = (GetSystemMetrics(SM_CXSCREEN)) / 3 - (bmpInfo.bmWidth / 3);
  488.  
  489.                 iY = (GetSystemMetrics(SM_CYSCREEN)) / 3 - (bmpInfo.bmHeight / 3);
  490.  
  491.                 iRet02 = SetWindowPos(FunctionReferenceTo_DialogBox01, HWND_TOPMOST, iX, iY, bmpInfo.bmWidth, bmpInfo.bmHeight, 0);
  492.  
  493.         //----------------------------------------------------------------------
  494.         // END     Create Region from imageFile_B
  495.         //----------------------------------------------------------------------
  496.  
  497.  
  498.         // Copy the memory dc into the screen dc
  499.         paintRegion();
  500.  
  501.         // Delete the bitmap
  502.         DeleteObject(Handle_Bitmap_A);
  503.         DeleteObject(Handle_Bitmap_B);
  504.  
  505.     }   // void createRegion(HWND FunctionReferenceTo_DialogBox01)
  506.  
  507. void paintRegion()
  508. {
  509.     // Transfer color data from the source device context to the destination device context
  510.     BitBlt(hdcDest, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, hdcMem, 0, 0, SRCCOPY);
  511. }
2 Weeks Ago #2

rollerbladegirl
P: 30
Here are some pictures showing what I am doing:
Attached Images
File Type: bmp a.bmp (29.3 KB, 16 views)
File Type: bmp b.bmp (22.3 KB, 19 views)
File Type: bmp c.bmp (351.6 KB, 16 views)
File Type: bmp working.bmp (351.6 KB, 14 views)
2 Weeks Ago #3

rollerbladegirl
P: 30
Thank you gits for fixing the code to show indentations, etc. That was nice of you. I shall try to post code correctly formatted for viewing in the future. I copy and pasted this code, but that was not sufficient. You fixed it. Thank you. I shall try to do better next time.


Also. I have been experimenting and got the code to work better in that it shows both of the images, but it greys out one of them. Should I replace the original code with the updated code in my original posting, or post it separately?
2 Weeks Ago #4

rollerbladegirl
P: 30
So far, to date 1/8/2020, this post has been viewed 224 times. Still no one told me how to do this. I need some help. C++ is new to me. Please someone tell me how to do this. I can create a region. I can create two regions, one at a time. I do not have to do this with dialog boxes if there is another or better/faster way. Help! Please.
1 Week Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.