Home > Software engineering >  MFC double buffer drawing, brush color setting problem?
MFC double buffer drawing, brush color setting problem?

Time:10-11

Recent need in BMP bitmap display, draw the ROI image is needed in the area, try to use double buffer drawing, but the brush color setting, the code is as follows:
 
Void COpenCloseDlg: : OnBnClickedButton1 ()
{
The CDC * m_picDC=GetDlgItem (IDC_STATIC_PIC) - & gt; GetDC ();
The CDC m_dcMemory;
M_dcMemory. CreateCompatibleDC (m_picDC);
The FILE * fp.
Fp=fopen (" C: \ \ Users \ \ t \ \ Desktop \ \ Lena raw ", "rb");
Fread (ImgData, sizeof (BYTE), 65536, fp);
The fclose (fp);
HBITMAP HBMP=CreateDIBSection (NULL, m_pBmpInfoGray, DIB_RGB_COLORS, (VOID * *) & amp; DIBdata, NULL, 0);
Memcpy (ImgData DIBdata, 256 * 256);
CBitmap BmpCompatible;
BmpCompatible. Attach (HBMP);//CBitmap object that is associated with HBITMAP
CBitmap * pOldBitmap=m_dcMemory. SelectObject (& amp; BmpCompatible);//DC compatible for bitmap
CRect the rect.
M_PIC. GetClientRect (& amp; The rect);//m_PIC for static control variables associated
Int w=the rect. Width ();
Int h=the rect. Height ();
CClientDC dc (FromHandle (m_PIC m_hWnd));
CBrush * pBrush=CBrush: : FromHandle ((HBRUSH) GetStockObject (NULL_BRUSH));
CBrush. * pOldBrush=m_dcMemory SelectObject (pBrush);
CPoint m_StartPoint (10, 10);
CPoint pointControl (50, 50);
CRect rc (m_StartPoint pointControl);
Rc. NormalizeRect ();
CPen pen (PS_SOLID, 2, RGB (255255, 0));
CPen * pOldPen=m_dcMemory. SelectObject (& amp; Pen);
M_dcMemory. A Rectangle (& amp; Rc);
Dc. SetStretchBltMode (COLORONCOLOR);
Dc. StretchBlt (0, 0, the rect. Width (), the rect. Height (), & amp; M_dcMemory, 0,0,256,256, SRCCOPY);
M_dcMemory. SelectObject (pOldBitmap);
M_dcMemory. SelectObject (pOldBrush);
BmpCompatible. DeleteObject ();
}


Display as follows:
http://img.my.csdn.net/uploads/201609/11/1473597613_3467.JPG
Image upload all kinds of failure, so we have to use the hyperlinks
Rectangular box is gray
But a little change, display is normal, the code is as follows:
 
Void COpenCloseDlg: : OnBnClickedButton1 ()
{
The CDC * m_picDC=GetDlgItem (IDC_STATIC_PIC) - & gt; GetDC ();
The CDC m_dcMemory;
M_dcMemory. CreateCompatibleDC (m_picDC);
The FILE * fp.
Fp=fopen (" C: \ \ Users \ \ t \ \ Desktop \ \ Lena raw ", "rb");
Fread (ImgData, sizeof (BYTE), 65536, fp);
The fclose (fp);
HBITMAP HBMP=CreateDIBSection (NULL, m_pBmpInfoGray, DIB_RGB_COLORS, (VOID * *) & amp; DIBdata, NULL, 0);
Memcpy (ImgData DIBdata, 256 * 256);
CBitmap BmpCompatible;
BmpCompatible. Attach (HBMP);//CBitmap object that is associated with HBITMAP
CBitmap * pOldBitmap=m_dcMemory. SelectObject (& amp; BmpCompatible);//DC compatible for bitmap
CRect the rect.
M_PIC. GetClientRect (& amp; The rect);//m_PIC for static control variables associated
Int w=the rect. Width ();
Int h=the rect. Height ();
CClientDC dc (FromHandle (m_PIC m_hWnd));
CBrush * pBrush=CBrush: : FromHandle ((HBRUSH) GetStockObject (NULL_BRUSH));
CBrush * pOldBrush=dc. SelectObject (pBrush);
CPoint m_StartPoint (10, 10);
CPoint pointControl (50, 50);
CRect rc (m_StartPoint pointControl);
Rc. NormalizeRect ();
Dc. SetStretchBltMode (COLORONCOLOR);
Dc. StretchBlt (0, 0, the rect. Width (), the rect. Height (), & amp; M_dcMemory, 0,0,256,256, SRCCOPY);
CPen pen (PS_SOLID, 2, RGB (255255, 0));
CPen * pOldPen=dc. SelectObject (& amp; Pen);
Dc. A Rectangle (& amp; Rc);
Dc. SelectObject (pOldBrush);
BmpCompatible. DeleteObject ();
}

Pictures show as follows:
http://img.my.csdn.net/uploads/201609/11/1473597613_6242.JPG
Don't understand the problem because of which, ask everybody to help explain?

CodePudding user response:

1, brush into a pointer type, use CreatePen to create;
2, create a brush on Dlg create functions or initialization function, prevent resources repeatedly create and release,

CodePudding user response:

 HDC HDC=: : GetDC (hWnd); 
HPEN HPEN, hOldPen;//brush
HPen=CreatePen (PS_SOLID, 2, RGB (0, 255));//generated red color brush brush width 5 pixels
HOldPen=(HPEN) SelectObject (HDC, HPEN);//into the brush device field condition
The Arc (HDC, xx - GJZ, yy - GJZ, xx + GJZ, yy + GJZ, xx, yy, xx, yy);//mechanical center range of 250
SelectObject (HDC, hOldPen);
DeleteObject (hPen);

CodePudding user response:

M_pBmpInfoGray problems?
Reference:
Void CShow256BmpDlg: : OnButton1 ()
{
//TODO: Add your the control notification handler code here
CClientDC dc (this);
The CDC memDC;
CBitmap bitmap.
CBitmap bitmap1;
HBITMAP HBITMAP;
CBitmap * pOldBitmap;
DWORD len.
MemDC. CreateCompatibleDC (& amp; Dc);
//8 bit
Bitmap. LoadBitmap (IDB_MY_BITMAP1);//8 bit "girl8. BMP
"BITMAP bm={0};
Bitmap. GetBitmap (& amp; Bm);
Int ImageSize=bm. BmWidthBytes * bm. BmHeight;
BYTE * pImageIn=new BYTE [ImageSize];
Memset (pImageIn, 0, ImageSize);
Len=bitmap. GetBitmapBits (ImageSize, pImageIn);
//use: CreateDIBSection
BITMAPINFO bmInfo;
BmInfo. BmiHeader. BiSize=sizeof (BITMAPINFOHEADER);
BmInfo. BmiHeader. BiWidth=bm. BmWidth;
BmInfo. BmiHeader. BiHeight=- bm. BmHeight;//should '-'!!!!!!!!!!
BmInfo. BmiHeader. BiPlanes=1;
BmInfo. BmiHeader. BiBitCount=bm. BmBitsPixel;//=32!!!!!!!!!!
BmInfo. BmiHeader. BiCompression=BI_RGB;
BmInfo. BmiHeader. BiSizeImage=ImageSize;
BmInfo. BmiHeader. BiXPelsPerMeter=0;
BmInfo. BmiHeader. BiYPelsPerMeter=0;
BmInfo. BmiHeader. BiClrUsed=0;
BmInfo. BmiHeader. BiClrImportant=0;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related