Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

SelectObject() function

Sonx_hvn7Sonx_hvn7 Member Posts: 54
Join Date: Apr 2008
Posts: 5

Help on SelectObject() function...
I'm struggling to understand the use of this function... Here's an example code

HDC hMemDC;
HBITMAP hBmp, hOldBmp;

hBmp = Earth.GetHandle ();

// Draw a line through the graphic file
hMemDC = CreateCompatibleDC(0);
hOldBmp = SelectObject (hMemDC, hBmp);

MoveTo (hMemDC, 0, 20);
LineTo (hMemDC, 30, 50);

SelectObject (hMemDC, hOldBmp);

DeleteDC (hMemDC);

// Show changes
Earth.Repaint ();

SelectObject() is used twice, but i fail to understand the use of it.. The code draws a line through an Earth object (Bitmap)...

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Join Date: Apr 2008
    : Posts: 5
    :
    : Help on SelectObject() function...
    : I'm struggling to understand the use of this function... Here's an
    : example code
    :
    : HDC hMemDC;
    : HBITMAP hBmp, hOldBmp;
    :
    : hBmp = Earth.GetHandle ();
    :
    : // Draw a line through the graphic file
    : hMemDC = CreateCompatibleDC(0);
    : hOldBmp = SelectObject (hMemDC, hBmp);
    :
    : MoveTo (hMemDC, 0, 20);
    : LineTo (hMemDC, 30, 50);
    :
    : SelectObject (hMemDC, hOldBmp);
    :
    : DeleteDC (hMemDC);
    :
    : // Show changes
    : Earth.Repaint ();
    :
    : SelectObject() is used twice, but i fail to understand the use of
    : it.. The code draws a line through an Earth object (Bitmap)...
    :
    [color=Blue]
    SelectObject() if succeeds pushes the old object back as a return code. So, you select your Earth bitmap into DC, then draw anything on that DC (it will be drawn on a surface of a bitmap, selected into DC, which is your Earth picture), then when you use SelectObject() second time it pushes Earth object back with lines drawn on on it. And you use that bitmap somewhere in Repaint() function. This is how Windows draws in memory without flicker. First you draw everything in a DC, which has a bitmap selected into it - then you dump all that to screen in one shot - instead of drawing all on screen one by one, which will flicker of course.
    [/color]
  • Sonx_hvn7Sonx_hvn7 Member Posts: 54
    : : Join Date: Apr 2008
    : : Posts: 5
    : :
    : : Help on SelectObject() function...
    : : I'm struggling to understand the use of this function... Here's an
    : : example code
    : :
    : : HDC hMemDC;
    : : HBITMAP hBmp, hOldBmp;
    : :
    : : hBmp = Earth.GetHandle ();
    : :
    : : // Draw a line through the graphic file
    : : hMemDC = CreateCompatibleDC(0);
    : : hOldBmp = SelectObject (hMemDC, hBmp);
    : :
    : : MoveTo (hMemDC, 0, 20);
    : : LineTo (hMemDC, 30, 50);
    : :
    : : SelectObject (hMemDC, hOldBmp);
    : :
    : : DeleteDC (hMemDC);
    : :
    : : // Show changes
    : : Earth.Repaint ();
    : :
    : : SelectObject() is used twice, but i fail to understand the use of
    : : it.. The code draws a line through an Earth object (Bitmap)...
    : :
    : [color=Blue]
    : SelectObject() if succeeds pushes the old object back as a return
    : code. So, you select your Earth bitmap into DC, then draw anything
    : on that DC (it will be drawn on a surface of a bitmap, selected into
    : DC, which is your Earth picture), then when you use SelectObject()
    : second time it pushes Earth object back with lines drawn on on it.
    : And you use that bitmap somewhere in Repaint() function. This is how
    : Windows draws in memory without flicker. First you draw everything
    : in a DC, which has a bitmap selected into it - then you dump all
    : that to screen in one shot - instead of drawing all on screen one by
    : one, which will flicker of course.
    : [/color]

    Thanx AsmGuru, a lot more clearer now... two questions though...

    1. You saying the second time SelectObject is used, the Earth is pushed back with lines. Which handler are we referring to in this case (hBmp or hOldBmp)? Maybe am confused with the term "push back"...

    2. I now want to draw the same line but not on the earth image, but on the frame itself... What changes do i make on the existing code? I actually have a thread which you replied to where i tried doing this with no success...

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : : : Join Date: Apr 2008
    : : : Posts: 5
    : : :
    : : : Help on SelectObject() function...
    : : : I'm struggling to understand the use of this function... Here's an
    : : : example code
    : : :
    : : : HDC hMemDC;
    : : : HBITMAP hBmp, hOldBmp;
    : : :
    : : : hBmp = Earth.GetHandle ();
    : : :
    : : : // Draw a line through the graphic file
    : : : hMemDC = CreateCompatibleDC(0);
    : : : hOldBmp = SelectObject (hMemDC, hBmp);
    : : :
    : : : MoveTo (hMemDC, 0, 20);
    : : : LineTo (hMemDC, 30, 50);
    : : :
    : : : SelectObject (hMemDC, hOldBmp);
    : : :
    : : : DeleteDC (hMemDC);
    : : :
    : : : // Show changes
    : : : Earth.Repaint ();
    : : :
    : : : SelectObject() is used twice, but i fail to understand the use of
    : : : it.. The code draws a line through an Earth object (Bitmap)...
    : : :
    : : [color=Blue]
    : : SelectObject() if succeeds pushes the old object back as a return
    : : code. So, you select your Earth bitmap into DC, then draw anything
    : : on that DC (it will be drawn on a surface of a bitmap, selected into
    : : DC, which is your Earth picture), then when you use SelectObject()
    : : second time it pushes Earth object back with lines drawn on on it.
    : : And you use that bitmap somewhere in Repaint() function. This is how
    : : Windows draws in memory without flicker. First you draw everything
    : : in a DC, which has a bitmap selected into it - then you dump all
    : : that to screen in one shot - instead of drawing all on screen one by
    : : one, which will flicker of course.
    : : [/color]
    :
    : Thanx AsmGuru, a lot more clearer now... two questions though...
    :
    : 1. You saying the second time SelectObject is used, the Earth is
    : pushed back with lines. Which handler are we referring to in this
    : case (hBmp or hOldBmp)? Maybe am confused with the term "push
    : back"...
    :
    : 2. I now want to draw the same line but not on the earth image, but
    : on the frame itself... What changes do i make on the existing code?
    : I actually have a thread which you replied to where i tried doing
    : this with no success...
    :
    :
    [color=Blue]The handle which always holds the Earth bitmap is hBmp. The DC has one slot for the current bitmap object. Push back is the same term as "return" - the function returns current bitmap and sets the bitmap you pass as a parameter in that one slot, so the passed bitmap becomes current. Read more about the DC in MSDN - you have to always (almost always) leave the DC in its original state, before releasing it back to Windows. This means you need to always do SelectObject() at least twice - once to select your object and once to deselect it and return DC in the original state.
    [/color]
Sign In or Register to comment.