Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can I use a CRgn to draw on a CBitmap?

How can I use a CRgn to draw on a CBitmap?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsperformancehelp
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Craig Miller
    wrote on last edited by
    #1

    Hi, I was wondering if anyone can tell me if the following is possible and how I would go about doing it: I have a CBitmap in memory that is created at start up that is 1750 x 2000 and is just an empty bitmap. I have a list of corrodinates that I want to create CRgn's for, let's say I have the following 0, 0, 10, 10 25, 50, 35, 60 I then load the Bitmap into a DC, the question is now, how do I create the CRgn's on this bitmap? I've decided to try and use regions instead of a color bitmap mask to do my image hotspots. Does anyone see anything that I'm missing as to why this wouldn't work? Thanks for any help you can provide, Craig

    B 1 Reply Last reply
    0
    • C Craig Miller

      Hi, I was wondering if anyone can tell me if the following is possible and how I would go about doing it: I have a CBitmap in memory that is created at start up that is 1750 x 2000 and is just an empty bitmap. I have a list of corrodinates that I want to create CRgn's for, let's say I have the following 0, 0, 10, 10 25, 50, 35, 60 I then load the Bitmap into a DC, the question is now, how do I create the CRgn's on this bitmap? I've decided to try and use regions instead of a color bitmap mask to do my image hotspots. Does anyone see anything that I'm missing as to why this wouldn't work? Thanks for any help you can provide, Craig

      B Offline
      B Offline
      Baafie
      wrote on last edited by
      #2

      This looks kind of familiar.. You must be in a hurry, codeguru.com??

      C 1 Reply Last reply
      0
      • B Baafie

        This looks kind of familiar.. You must be in a hurry, codeguru.com??

        C Offline
        C Offline
        Craig Miller
        wrote on last edited by
        #3

        Baafie, Thanks for your reply, yes I have been stressing out on how I'm going to basically create a bmp/jpg that has hotspots to display tool tips and handle click messages. One thing though, I'm usin the CRgn so I can find out where the user is pointing, I just noticed though that the PtInRegion is specific to one particular region. If I have 50 different regions does that mean I have to keep an array of CRgn objects and iterate through the array so that I can call PtInRegion on each object? This seems like it would be really slow, do you know of a better way to deal with this problem? Thanks again, Craig

        C 1 Reply Last reply
        0
        • C Craig Miller

          Baafie, Thanks for your reply, yes I have been stressing out on how I'm going to basically create a bmp/jpg that has hotspots to display tool tips and handle click messages. One thing though, I'm usin the CRgn so I can find out where the user is pointing, I just noticed though that the PtInRegion is specific to one particular region. If I have 50 different regions does that mean I have to keep an array of CRgn objects and iterate through the array so that I can call PtInRegion on each object? This seems like it would be really slow, do you know of a better way to deal with this problem? Thanks again, Craig

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          There is a much better way to deal with this. Create a mask bitmap that is the same dimensions and marks out your regions in different colours. Use GetPixel to find out the colour of this second bitmap at your mouse position and viola - you have a match. You could create constant COLORREFs like this const COLORREF MASK_HELP = RGB(255,0,0); const COLORREF MASK_ABOUT = RGB(0,255,0); Then you can grab your colour and do a switch statement that uses meaningful values instead of assorted colours. Christian #include "std_disclaimer.h" The careful application of terror is also a form of communication. Eagles may soar, but weasels don't get sucked into jet engines.

          C 1 Reply Last reply
          0
          • C Christian Graus

            There is a much better way to deal with this. Create a mask bitmap that is the same dimensions and marks out your regions in different colours. Use GetPixel to find out the colour of this second bitmap at your mouse position and viola - you have a match. You could create constant COLORREFs like this const COLORREF MASK_HELP = RGB(255,0,0); const COLORREF MASK_ABOUT = RGB(0,255,0); Then you can grab your colour and do a switch statement that uses meaningful values instead of assorted colours. Christian #include "std_disclaimer.h" The careful application of terror is also a form of communication. Eagles may soar, but weasels don't get sucked into jet engines.

            C Offline
            C Offline
            Craig Miller
            wrote on last edited by
            #5

            Christian, That is actually an excellent suggestion, thank you. The problem I ran into with trying to do that method is when the graphic is zoomed and then I read in the masked graphic the points are all messed up because the masked image is being read into the dc at 100% and I couldn't figure out how to change the size of the image to match the one that is being displayed. I have been trying to figure this out for about two weeks, it's a very frustrating problem for me, that is why I tried using the CRgn instead but it doesn't seem like that is a very good solution either :-(. Thanks again for your help, any other ideas and I would be very receptive to listening, Craig

            C 1 Reply Last reply
            0
            • C Craig Miller

              Christian, That is actually an excellent suggestion, thank you. The problem I ran into with trying to do that method is when the graphic is zoomed and then I read in the masked graphic the points are all messed up because the masked image is being read into the dc at 100% and I couldn't figure out how to change the size of the image to match the one that is being displayed. I have been trying to figure this out for about two weeks, it's a very frustrating problem for me, that is why I tried using the CRgn instead but it doesn't seem like that is a very good solution either :-(. Thanks again for your help, any other ideas and I would be very receptive to listening, Craig

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I am just looking at our zooming code as it happens ;0) The way I handle things in my paint program is that I do one thing before anything else every time the mouse moves: I call a function that takes into account the scroll position and zoom factor and stores the mouse position converted to bitmap co-ordinates. Then I always have a member variabl I can use to operate on the bitmap and I know exactly what part of the bitmap the mouse is pointing at. I'd suggest you do the same - if the user zooms, scrolls or moves the mouse, take these things into account and recalculate so that you know where on your 1:1 bitmap they are pointing. Christian Secrets of a happy marriage #27: Never go to bed if you are mad at each other. It's more fun to stay up and fight.

              C 1 Reply Last reply
              0
              • C Christian Graus

                I am just looking at our zooming code as it happens ;0) The way I handle things in my paint program is that I do one thing before anything else every time the mouse moves: I call a function that takes into account the scroll position and zoom factor and stores the mouse position converted to bitmap co-ordinates. Then I always have a member variabl I can use to operate on the bitmap and I know exactly what part of the bitmap the mouse is pointing at. I'd suggest you do the same - if the user zooms, scrolls or moves the mouse, take these things into account and recalculate so that you know where on your 1:1 bitmap they are pointing. Christian Secrets of a happy marriage #27: Never go to bed if you are mad at each other. It's more fun to stay up and fight.

                C Offline
                C Offline
                Craig Miller
                wrote on last edited by
                #7

                Christian, Thanks so much for such information. I think the biggest problem I'm having is trying to figure out exactly where they are pointing. Is there some kind of formula that I can use to determine this information? I can get the zoom level from the Dundas COXSrollWnd class that I'm using. How is the conversion to bitmap coordinates done? Is there a CBitmap method that will do this for me? Thanks again for all of your help, this has been an enlightening discussion for me to say the least, Craig

                C 1 Reply Last reply
                0
                • C Craig Miller

                  Christian, Thanks so much for such information. I think the biggest problem I'm having is trying to figure out exactly where they are pointing. Is there some kind of formula that I can use to determine this information? I can get the zoom level from the Dundas COXSrollWnd class that I'm using. How is the conversion to bitmap coordinates done? Is there a CBitmap method that will do this for me? Thanks again for all of your help, this has been an enlightening discussion for me to say the least, Craig

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  First of all you need to get the point that comes in, then you need to add the scroll positions. If it is zoomed you need to divide by the factor zoomed, in other words if you're zoomed in by a factor of 2, you need to divide by 2, because 10, 10 in mouse co-ords will point to 5,5 on the bitmap. Here is my function:

                  CPoint p = pt;
                  

                  // Figure out our mouse position, which is the cursor position, offset to allow for zooming and scrolling
                  pt.x = pt.x + GetScrollPosition().x - m_XOffset;
                  pt.y = pt.y + GetScrollPosition().y - m_YOffset;

                  // Zoom > 0 means zoomed up, < 0 means zoomed down.
                  if (m_iZoom>0)
                  {
                  UINT divide = 1 << m_iZoom ;
                  pt.x = (long)(((float) pt.x/(float) divide));
                  pt.y = (long) (((float) pt.y/(float) divide));

                  }
                  else if (m\_iZoom<0)
                  {
                  	int multiply = 1 << (m\_iZoom\*-1);
                  	pt.x \*= multiply;
                  	pt.y \*= multiply;				
                  }
                  

                  In my case the zoom factor is not 1,2,3, but doubles every time in either direction, hence the shift operators. m_XOffset and m_YOffset are where the window is maximised and the bitmap is centered. There are lots of casts because I regard warnings as errors. Christian Secrets of a happy marriage #27: Never go to bed if you are mad at each other. It's more fun to stay up and fight.

                  C 1 Reply Last reply
                  0
                  • C Christian Graus

                    First of all you need to get the point that comes in, then you need to add the scroll positions. If it is zoomed you need to divide by the factor zoomed, in other words if you're zoomed in by a factor of 2, you need to divide by 2, because 10, 10 in mouse co-ords will point to 5,5 on the bitmap. Here is my function:

                    CPoint p = pt;
                    

                    // Figure out our mouse position, which is the cursor position, offset to allow for zooming and scrolling
                    pt.x = pt.x + GetScrollPosition().x - m_XOffset;
                    pt.y = pt.y + GetScrollPosition().y - m_YOffset;

                    // Zoom > 0 means zoomed up, < 0 means zoomed down.
                    if (m_iZoom>0)
                    {
                    UINT divide = 1 << m_iZoom ;
                    pt.x = (long)(((float) pt.x/(float) divide));
                    pt.y = (long) (((float) pt.y/(float) divide));

                    }
                    else if (m\_iZoom<0)
                    {
                    	int multiply = 1 << (m\_iZoom\*-1);
                    	pt.x \*= multiply;
                    	pt.y \*= multiply;				
                    }
                    

                    In my case the zoom factor is not 1,2,3, but doubles every time in either direction, hence the shift operators. m_XOffset and m_YOffset are where the window is maximised and the bitmap is centered. There are lots of casts because I regard warnings as errors. Christian Secrets of a happy marriage #27: Never go to bed if you are mad at each other. It's more fun to stay up and fight.

                    C Offline
                    C Offline
                    Craig Miller
                    wrote on last edited by
                    #9

                    That is excellent, thanks so much for your help in this matter, you make it seem so easy after I've been trying so hard for so long. I will certainly give this a try, I'm sure it will work fine for me. Thanks again, Craig

                    C 1 Reply Last reply
                    0
                    • C Craig Miller

                      That is excellent, thanks so much for your help in this matter, you make it seem so easy after I've been trying so hard for so long. I will certainly give this a try, I'm sure it will work fine for me. Thanks again, Craig

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      No worries - always glad to help. :) Christian Secrets of a happy marriage #27: Never go to bed if you are mad at each other. It's more fun to stay up and fight.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups