Click here to Skip to main content
15,912,932 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow can I get a controls HWND ? Pin
26-May-01 9:34
suss26-May-01 9:34 
AnswerRe: How can I get a controls HWND ? Pin
26-May-01 10:28
suss26-May-01 10:28 
GeneralRe: How can I get a controls HWND ? Pin
Vu Nguyen15-Nov-01 5:58
Vu Nguyen15-Nov-01 5:58 
AnswerRe: How can I get a controls HWND ? Pin
26-May-01 10:47
suss26-May-01 10:47 
AnswerRe: How can I get a controls HWND ? Pin
Waleed Eissa26-May-01 11:11
Waleed Eissa26-May-01 11:11 
AnswerThanks for all ! Pin
27-May-01 4:54
suss27-May-01 4:54 
GeneralPrepare image before Bitblt Pin
Starodubtsev Sergey26-May-01 7:00
Starodubtsev Sergey26-May-01 7:00 
GeneralRe: Prepare image before Bitblt Pin
Christian Graus26-May-01 22:13
protectorChristian Graus26-May-01 22:13 
CreateCompatibleBitmap creates a bitmap compatible with a device context. You can just create a bitmap and use GetDeviceCaps(dc.m_hDC, BITSPIXEL) if you prefer - the net result is the same.

By selecting a bitmap into a DC you get sent back the old one that was in the DC, this is one reason why the call to Ellipse does nothing. The second is that until you select a HBITMAP, CBitmap or DIBSection into a DC, it has a size of 1 x 1 x 1, one bit in total. This is clearly of no value Smile | :)


Select the bitmap into the DC first, then draw to your hearts content, the memory bitmap will then be created and able to be blted across. One more caveat - if you use functions like Ellipse as opposed to FillSolidRect ( which takes a pointer to a brush ), then the drawing will be done in the default brush colour, and I'm not sure if Windows offers any guarentees as to what that would be ( but I'd guess white pen, white brush, black bitmap ). Either way, you'd do better to be sure and create your own, and catch all the returns to avoid memory leaks.

Oh - I nearly forgot - call CClientDC at the LAST possible minute to avoid flicker, which I presume is the whole point ?

So here is the final code:

CDC memDC;
memDC.CreateCompatibleDC(NULL);
CBitmap bitmap;
bitmap.CreateBitmap(200, 200, 1, GetDeviceCaps(memDC.m_hDC, BITSPIXEL), NULL);
CBitmap * pOldBitmap = (CBitmap*)memDC.SelectObject(&bitmap);
CBrush red (RGB( 255, 0, 0));
memDC.FillSolidRect(0, 0, 200, 200, &red); // Or create pens/brushes and select the same as we did the bitmap, catching the return of the same type, then you can draw whatever you like...

CClientDC dc(this);
dc.BitBlt(0, 0, 200, 200, &memDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOldBitmap); // And any other objects we selected in, otherwise we get memory leaks
memDC.DeleteDC();



Christian

#include "std_disclaimer.h"

People who love sausage and respect the law should never watch either one being made.
GeneralCEditView Pin
Starodubtsev Sergey26-May-01 6:05
Starodubtsev Sergey26-May-01 6:05 
GeneralRe: CEditView Pin
27-May-01 7:40
suss27-May-01 7:40 
GeneralReading data from CFile Pin
26-May-01 2:52
suss26-May-01 2:52 
GeneralRe: Reading data from CFile Pin
26-May-01 3:46
suss26-May-01 3:46 
GeneralRe: Reading data from CFile Pin
26-May-01 5:15
suss26-May-01 5:15 
GeneralRe: Reading data from CFile Pin
27-May-01 1:02
suss27-May-01 1:02 
GeneralRe: Reading data from CFile Pin
27-May-01 7:48
suss27-May-01 7:48 
GeneralWinNT service and system shutdown Pin
"Le Phuc Nguyen Tuan"25-May-01 19:18
"Le Phuc Nguyen Tuan"25-May-01 19:18 
GeneralRe: WinNT service and system shutdown Pin
Ghazi H. Wadi28-May-01 7:19
Ghazi H. Wadi28-May-01 7:19 
GeneralSource code ... Pin
Hadi Rezaee25-May-01 18:43
Hadi Rezaee25-May-01 18:43 
GeneralRe: Source code ... Pin
Christian Graus26-May-01 22:25
protectorChristian Graus26-May-01 22:25 
GeneralRe: Source code ... Pin
Hadi Rezaee27-May-01 1:16
Hadi Rezaee27-May-01 1:16 
GeneralRe: Source code ... Pin
Christian Graus27-May-01 2:38
protectorChristian Graus27-May-01 2:38 
GeneralRe: Source code ... Pin
Hadi Rezaee27-May-01 4:04
Hadi Rezaee27-May-01 4:04 
GeneralRe: Source code ... Pin
Christian Graus27-May-01 12:00
protectorChristian Graus27-May-01 12:00 
GeneralRe: Source code ... Pin
Hadi Rezaee27-May-01 17:47
Hadi Rezaee27-May-01 17:47 
GeneralRe: Source code ... Pin
Christian Graus27-May-01 18:34
protectorChristian Graus27-May-01 18:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.