| |

VerySource

 Forgot password?
 Register
Search
View: 593|Reply: 5

Ask a question about CDC :: SelectObject ()

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-13 16:30:01
| Show all posts |Read mode
The following is a piece of code in MSDN. I have put them into VC and can run, but why are there several places to use it like this?

void CBrushView :: OnDraw (CDC * pDC)
{
   CBrushDoc * pDoc = GetDocument ();
   ASSERT_VALID (pDoc);

   // CBrush :: CBrush.
   CBrush brush1; // Must initialize!
   brush1.CreateSolidBrush (RGB (0,0,255)); // Blue brush.

   CBrush * pTempBrush = NULL;
   CBrush OrigBrush;

   CRect rc;
   GetClientRect (&rc);
   ScreenToClient (&rc); // It seems unnecessary here!

   pTempBrush = (CBrush *) pDC-> SelectObject (brush1);
   
   // SelectObject () parameter should be a pointer! Why is this possible?
   
   // Save original brush.
   OrigBrush.FromHandle ((HBRUSH) pTempBrush);

   // Paint upper left corner with blue brush.
   pDC-> Rectangle (0, 0, rc.Width () / 2, rc.Height () / 2);

    // Reselect original brush into device context.
   pDC-> SelectObject (&OrigBrush);
}

After modifying it like this, that is, replacing the OrigBrush with a pointer,

Compilation works, but error reports pop up at runtime! !!

   CBrush * OrigBrush;
   ...
   OrigBrush = (CBrush *) pDC-> SelectObject (brush1);

   // Paint upper left corner with blue brush.
   pDC-> Rectangle (0, 0, rc.Width () / 2, rc.Height () / 2);

   // Reselect original brush into device context.
   pDC-> SelectObject (OrigBrush);

Everyone enlighten me !!!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-4-12 09:30:01
| Show all posts
ScreenToClient (&rc);
This sentence is to convert the screen coordinates into the coordinates of the client area, of course, useful.
   OrigBrush.FromHandle ((HBRUSH) pTempBrush);
This sentence is unnecessary, the OrigBrush variable is also redundant, as long as
   pDC-> SelectObject (&OrigBrush);
This sentence is enough, the purpose is to release the brush resources added to the pDC.
  pTempBrush = (CBrush *) pDC-> SelectObject (brush1);
This sentence should be written as:
  pTempBrush = (CBrush *) pDC-> SelectObject (&brush1);
As for the landlord's compilation, I can pass it. I haven't tried it. It's unclear, but it's definitely wrong.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-5-2 11:30:01
| Show all posts
to卐瞎子卐:
   The previous sentence of ScreenToClient (&rc) GetClientRect (&rc) has obtained the coordinates of the client area, after the screenToClient (&rc), the program can also run normally;
   The definition of OrigBrush is to save the original brush, because the return value of pDC-> SelectObject (brush1) is a pointer;
   The pointer variable should be passed in SelectObject (), but pDC-> SelectObject (brush1) is the usage in msdn! Why can pass the object? There is no such function definition in msdn! !
Reply

Use magic Report

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-6 20:00:01
| Show all posts
The base class CGdiObject of CBrush, CPen, etc. has the operator CGdiObject :: operator HGDIOBJ () const
And CDC has CDC :: SelectObject (HGDIOBJ hObject)
So CBrush is automatically converted to HGDIOBJ
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-6-1 13:15:02
| Show all posts
tohuanwu:

   Found CBrush :: operator HBRUSH () const in msdn

   And CDC :: SelectObject (CBrush *);
 
   But did not find the two function prototypes you found, nor CDC :: SelectObject (HBRUSH),
   
   Can handles also be used as pointers? The handle is the pointer?
Reply

Use magic Report

1

Threads

11

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-6-24 09:45:01
| Show all posts
CDC::SelectObject(HGDIOBJ hObject) is an inline function not written
Press F11 to debug and you can see
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list