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. SetWindowRgn For Text Or Label

SetWindowRgn For Text Or Label

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 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.
  • Y Offline
    Y Offline
    yytg
    wrote on last edited by
    #1

    There is a lot of samples for RGN's for bitmaps but I need to create a RGN for Text with a font and size. How can I do it? thanks in advance

    H C 2 Replies Last reply
    0
    • Y yytg

      There is a lot of samples for RGN's for bitmaps but I need to create a RGN for Text with a font and size. How can I do it? thanks in advance

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Can you more explain,please? ;)

      Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

      Y 1 Reply Last reply
      0
      • H Hamid Taebi

        Can you more explain,please? ;)

        Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

        Y Offline
        Y Offline
        yytg
        wrote on last edited by
        #3

        I want to display text on the screen in such a way so the user will see only the text (Not the window). The RGN is made to disable coloring outside of specific places in the window. The effect of it is so you can make windows in a shape you design as so http://www.codeproject.com/KB/GDI/coolrgn.aspx[^]. How do I do the same effect on text?

        1 Reply Last reply
        0
        • Y yytg

          There is a lot of samples for RGN's for bitmaps but I need to create a RGN for Text with a font and size. How can I do it? thanks in advance

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Do you mean you wish to create a region with the shape of text? If so, try this, am not sure if it works but it is worth a try: 1. Create your font and select into a DC (a memory DC should work) 2. Use BeginPath to begin defining a path 3. Use TextOut or DrawText or such to display the text 4. Use CreateFromPath[^] to create a region. It is possible that it only works with vector-based fonts.

          > The problem with computers is that they do what you tell them to do and not what you want them to do. <

          Y 1 Reply Last reply
          0
          • C Code o mat

            Do you mean you wish to create a region with the shape of text? If so, try this, am not sure if it works but it is worth a try: 1. Create your font and select into a DC (a memory DC should work) 2. Use BeginPath to begin defining a path 3. Use TextOut or DrawText or such to display the text 4. Use CreateFromPath[^] to create a region. It is possible that it only works with vector-based fonts.

            > The problem with computers is that they do what you tell them to do and not what you want them to do. <

            Y Offline
            Y Offline
            yytg
            wrote on last edited by
            #5

            Thanks for helping The function don't work with all fonts so I did this

            SetRgn(CString str)
            {
            CRect r;GetClientRect(&r);
            CMemoryDC dc(GetDC(),r);
            CFont font;font.CreatePointFont(120,L"Arial",0,true);
            COLORREF bgColor=dc.GetBkColor();
            dc.FillSolidRect(r,bgColor);

            dc.SelectFont(font);
            dc.TextOut(0,0,str);
            
            CRgn rgn,tRgn;rgn.CreateRectRgn(0,0,0,0);
            for (int x=r.left;x<r.right;x++)>
            {
            	for (int y=r.top;y<r.bottom;y++)>
            	{
            		if(!(!tRgn))tRgn.DeleteObject();
            		COLORREF c=dc.GetPixel(x,y);
            		if(c!=bgColor)
            		{
            			tRgn.CreateRectRgn(x,y,x+1,y+1);
            			rgn.CombineRgn(rgn,tRgn,RGN\_OR);
            		}
            		
            	}
            }
            SetWindowRgn(rgn);
            

            }

            C 1 Reply Last reply
            0
            • Y yytg

              Thanks for helping The function don't work with all fonts so I did this

              SetRgn(CString str)
              {
              CRect r;GetClientRect(&r);
              CMemoryDC dc(GetDC(),r);
              CFont font;font.CreatePointFont(120,L"Arial",0,true);
              COLORREF bgColor=dc.GetBkColor();
              dc.FillSolidRect(r,bgColor);

              dc.SelectFont(font);
              dc.TextOut(0,0,str);
              
              CRgn rgn,tRgn;rgn.CreateRectRgn(0,0,0,0);
              for (int x=r.left;x<r.right;x++)>
              {
              	for (int y=r.top;y<r.bottom;y++)>
              	{
              		if(!(!tRgn))tRgn.DeleteObject();
              		COLORREF c=dc.GetPixel(x,y);
              		if(c!=bgColor)
              		{
              			tRgn.CreateRectRgn(x,y,x+1,y+1);
              			rgn.CombineRgn(rgn,tRgn,RGN\_OR);
              		}
              		
              	}
              }
              SetWindowRgn(rgn);
              

              }

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #6

              I supose it really only works for vector-fonts since a raster-font probably has nothing to put into a path...Anyways, isn't that solution a bit slow?

              > The problem with computers is that they do what you tell them to do and not what you want them to do. <

              Y 1 Reply Last reply
              0
              • C Code o mat

                I supose it really only works for vector-fonts since a raster-font probably has nothing to put into a path...Anyways, isn't that solution a bit slow?

                > The problem with computers is that they do what you tell them to do and not what you want them to do. <

                Y Offline
                Y Offline
                yytg
                wrote on last edited by
                #7

                I did a test in debug mode with breakpoints...(not the best way to check) 1000*1000=1,000,000 loop It was only 5 sec. a normal textout will not export so much as so. Update...

                CSize sizeText;dc.GetTextExtent(str,str.GetLength(),&sizeText);
                for (int x=r.left;x<sizetext.cx;x++)>
                {
                	for (int y=r.top;y<sizetext.cy;y++)>
                

                ....

                modified on Monday, December 29, 2008 9:34 AM

                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