White The background of the picture control
CLine m_line;//a custom class CLine
CCLIENTDC DC (this); Device context for painting
CRect rc = GetRect (idc_static_rect);
M_line.drawbackground (&DC,RC);
void CLine::D rawbackground (CDC *PDC, CRect rect)
{
CBrush Brushctl;
Brushctl.createsolidbrush (GetBkColor ());
Pdc->rectangle (rect);
Pdc->fillrect (RECT,&BRUSHCTL);
Brushctl.deleteobject ();
}
Above is a digression.
General steps to create a palette:
1 Creating a Logpalette structure and a paletteentry array
2 Assign a value to a paletteentry array, which is to create a palette color table
3 Creating a CPalette object and initializing the palette object with the CreatePalette function
4 using the SelectPalette function to associate a device description table with a palette
5 using the RealizePalette function in CDC to make the palette effective
If a window is to display a special color, you should generally implement your own logical palette when processing WM_PAINT messages, that is, before redrawing in OnPaint or OnDraw functions, to invoke SelectPalette and RealizePalette, If the color displayed by the window is more important, specify the Bforcebackground parameter to False when calling SelectPalette.
To coordinate the use of the System palette for each window, Windows sends messages Wm_querynewpalette and wm_palettechanged to the top-level window and redraw window when necessary.
When a top-level or overlapping window is activated, a Wm_querynewpalette message is received, and the message is received at the creation of the window, which precedes the WM_PAINT message arrival window, if the active window is to use a special color, You should implement your own logical palette and redraw the window when you receive the message.
void Cteststoreobjectdlg::onbutton3 ()
{
Todo:add your control notification handler code here
Logpalette structure and uses the logical palette structure to initialize the palette object.
const int ncolors=24; BYTE R[ncolors],g[ncolors],b[ncolors];
UINT nsize=sizeof (Logpalette) + (sizeof (paletteentry) * (nColors-1)); Calculate the space required for a logical palette
Logpalette *plp= (logpalette*) new byte[nsize];
Initialize the logical palette, assuming that r,g,b is saving the palette of colors red, green, and blue arrays of three components
plp->palversion=0x300; Fixed value
plp->palnumentries=ncolors;
for (int i=0;i<ncolors;i++)
{
plp->palpalentry[i].pered=r[i];
plp->palpalentry[i].pegreen=g[i];
plp->palpalentry[i].peblue=b[i];
plp->palpalentry[i].peflags=0;
}
Create a palette
CPalette M_palette;
M_palette. CreatePalette (PLP);
CClientDC *pdc=new CClientDC (this);
CPalette *poldpalette=pdc->selectpalette (&m_palette,false);
Pdc->realizepalette ();
Image Display or paint operation
Pdc->selectpalette (Poldpalette,false);
}
CPalette is the Palette class for MFC encapsulation. The CPalette operation is as follows:
1. Create a color palette . To create a color palette, you first need to know the number of colors contained in the palette object you want to create ncolors, and then create a logical palette
Logpalette Structureand uses the logical palette structure to initialize the palette object.
UINT nsize=sizeof (Logpalette) + (sizeof (paletteentry) * (nColors-1)); Calculate the space required for a logical palette
Logpalette *plp= (logpalette*) new byte[nsize];
Initialize the logical palette, assuming that r,g,b is saving the palette of colors red, green, and blue arrays of three components
plp->palversion=0x300; Fixed value
plp->palnumentries=ncolors;
for (int i=0;i<ncolors;i++)
{
plp->palpalentry[i].pered=r[i];
plp->palpalentry[i].pegreen=g[i];
plp->palpalentry[i].peblue=b[i];
plp->palpalentry[i].peflags=0;
}
Create a palette
CPalette M_palette;
M_palette. CreatePalette (PLP);
2. Creating a debug version from a bitmap。 Suppose M_bitmap is a CBitmap object.
Dibsection ds;
M_bitmap. GetObject (sizeof (dibsection), &ds);
//Calculation of colors
int ncolors;
if (ds.dsbmih.biclrused!=0)
{
ncolors=ds.dsbmih.biclrused;
}
Else
{
ncolors=1<<ds.dsbmih.bibitcount;
}
Create a palette
if (ncolors>256)
M_palette. CreateHalftonePalette (&DC);
Else
{
Rgbquad *prgb=new Rgbquad[ncolors];
Get a list of bitmap colors
CDC MEMDC;
Memdc.createcompatibledc (&DC);
CBitmap *poldbitmap=memdc.selectobject (&M_BITMAP);
:: Getdibcolortable (HDC) MEMDC,0,NCOLORS,PRGB); Get a list of colors
Memdc.selectobject (POLDBITMAP);
Initializing the logical palette
UINT nsize=sizeof (Logpalette) + (sizeof (paletteentry) * (nColors-1));
Logpalette *plp= (logpalette*) new byte[nsize];
plp->palversion=0x300;
plp->palnumentries=ncolors;
for (int i=0;i<ncolors;i++)
{
plp->palpalentry[i].pered=prgb[i].rgbred;
plp->palpalentry[i].pegreen=prgb[i].rgbgreen;
plp->palpalentry[i].peblue=prgb[i].rgbblue;
plp->palpalentry[i].peflags=0;
}
Create a palette
M_palette. CreatePalette (PLP);
}
3. Use the palette:
To use to create a good debug version of M_palette, you need to select a palette before drawing and implement it.
CPalette *poldpalette=pdc->selectpalette (&m_palette,false);
Pdc->realizepalette ();
...//image display or paint operation
Pdc->selectpalette (Poldpalette,false);
CBitmap is a bitmap class in MFC encapsulation that can make a DDB or DIB, a device-dependent bitmap or device-independent bitmap.
The operations on CBitmap are as follows:
1. Bitmap Read . If you read a bitmap from a file and remain in the M_bitmap object, the operation is as follows:
Hbitmap Hbitmap;
Hbitmap= (HBITMAP):: Loadimag (null, lpszpathname, image_bitmap, 0,0,
Lr_loadfromfile | Lr_createdibsection);
if (!HBITMAP)
M_bitmap. Attach (HBITMAP);
If you load a bitmap from a resource, use M_bitmap directly. LoadBitmap (nIDResource);
2. Bitmap display。 First, if the bitmap has a color palette, the bitmap palette is selected by the DC and implemented.
CPalette *poldpalette=pdc->selectpalette (Ppalette,false);
Pdc->realizepalette ();
And then use BitBlt or StretchBlt to display
cdc* MEMDC;
Memdc->createcompatibledc (PDC);
CBitmap *poldbitmap=memdc->selectobject (&M_BITMAP);
Pdc->bitblt (0,0,bitmapwidth,bitmapheight, &memdc,0,0,srccopy);//Display image
The Dib is also called a device-independent bitmap, as its name implies, the color of the pixel is independent of the system palette, so it can be used to communicate between different systems or between different applications, or to permanently save images.
The DIB file begins with the Bitmapfileheader structure, which is followed by a bitmapinfoheader structure, followed by a color table of RGBQUAD structures (24-bit true-color bitmaps are not for 2, 8, 16, 256-color bitmaps), The file is finally stored with a pixel array of Dib. The bitmapfileheader structure is defined as:
typedef struct TAGBITMAPFILEHEADER
{
WORD Bftype; File type, must be "BM"
DWORD bfsize; Size of File
WORD bfReserved1; is 0
WORD BfReserved2; is 0
DWORD bfoffbits; The offset of the stored pixel array relative to the file header
} Bitmapfileheader;
In memory, a complete DIB consists of two parts: a BITMAPINFO structure and an array of storage pixel arrays. Bitmapinfo describes various properties, such as the size of the bitmap, the color mode, and the color palette, which are defined as:
typedef struct TAGBITMAPINFO
{
Bitmapinfoheader Bmiheader;
Rgbquad Bmicolors[1]; The color list, which is just a placeholder, if the bitmap has no color list, the array is empty.
} bitmapinfo;
The Bitmapinfoheader structure contains various information about the DIB, which is defined as:
typedef struct TAGBITMAPINFOHEADER
{
DWORD bisize; The size of the structure
LONG Biwidth; The width of the bitmap (in pixels)
LONG Biheight; The height of the bitmap (in pixels)
WORD biplanes; Must be 1
WORD biBitCount//number of bits per pixel (1, 4, 8, 16, 24, or 32)
DWORD bicompression; Compression mode, typically 0 or bi_rgb (uncompressed)
DWORD biSizeImage; The size of the image in bytes (only for compressed bitmaps)
LONG Bixpelspermeter; The horizontal resolution of the bitmap is illustrated by the number of pixels per metre of the target device
LONG Biypelspermeter; The vertical resolution of the bitmap is illustrated by the number of pixels per metre of the target device
DWORD biclrused; The number of colors in the color table, or 0 if the bitmap uses the maximum number of colors specified by the biBitCount
DWORD biclrimportant; Number of important colors, if the value is 0 all colors are important
} Bitmapinfoheader;
The RGBQUAD structure is used to describe the color, which is defined as:
typedef struct TAGRGBQUAD
{
BYTE Rgbblue; Blue Component
BYTE Rgbgreen; Green component
BYTE rgbred; Red component
BYTE rgbreserved; Reserved bytes, 0
} Rgbquad;
The byte array of the DIB is stored line-up from the bottom line of the image, which is equal to the image being reversed and then scanned on a progressive line. In addition, the number of bytes per scan row in the byte array must be a multiple of 4, if not enough to be 0 padded.