最近公司有一要求,就是画一条曲线,效果是颜色从线中向两边淡出。找了些能实现渐变画图函数,它们基本上
上都是填充一个矩形或三角形,包括GDI+,结果都不是很满意。后来发现ExtCreatePen这个函数画大小1像素的虚线点线线头为方块等都好
,感觉可以实现,仅仅是感觉,思路是做一淡入淡出的位图,然后用这个函数将位图选入画笔。里面有要一个LOGBRUSH结构,
typedef struct tagLOGBRUSH { 
UINT     lbStyle; 
COLORREF lbColor; 
LONG     lbHatch; 
} LOGBRUSH, *PLOGBRUSH; 

If lbStyle is BS_DIBPATTERN, the lbHatch member contains a handle to a packed DIB. To obtain this handle,
an application calls the GlobalAlloc function with GMEM_MOVEABLE (or LocalAlloc with LMEM_MOVEABLE) to 
allocate a block of memory and then fills the memory with the packed DIB. A packed DIB consists of a BITMAPINFO 
structure immediately followed by the array of bytes that define the pixels of the bitmap. 
  当lbStyle选为BS_DIBPATTERN,lbHatch就是DIB句柄。关键是我对DIB不是很了解,论坛中有贴如下:
CreateFile   打开文件 
ReadFile   把指针移动到DIB数据部分(跳过文件头14个字节吧和信息头40个字节) 
再memcpy到内存中,(分配空间时用句柄HANDLE   hDIB)
  有谁能具体结合英文注释给写一个获得DIB句柄的小例子?公司要求比较急!

解决方案 »

  1.   

    // Initialize a LOGBRUSH structure.
    LOGBRUSH logBrush;
    logBrush.lbStyle = BS_HATCHED;
    logBrush.lbColor = RGB(0, 192, 192);
    logBrush.lbHatch = HS_CROSS;// Declare an uninitialized CBrush ...
    CBrush brush;
    // ... and initialize it with the LOGBRUSH.
    brush.CreateBrushIndirect(&logBrush);// Select the brush (and perhaps a pen) into
    // the device context.
    CBrush* pOldBrush = (CBrush*)pDC->SelectObject(&brush);
    CPen* pOldPen = (CPen*)pDC->SelectStockObject(BLACK_PEN);// Have fun!
    pDC->Pie(CRect(100, 100, 300, 300), CPoint(0, 0), CPoint(50, 200));// Restore the original device context objects.
    pDC->SelectObject(pOldBrush);
    pDC->SelectObject(pOldPen);
      

  2.   


    你写的这个我知道怎么用。 If lbStyle is BS_DIBPATTERN, the lbHatch member contains a handle to a packed DIB. To obtain this handle,an application calls the GlobalAlloc function with GMEM_MOVEABLE (or LocalAlloc with LMEM_MOVEABLE) to allocate a block of memory and then fills the memory with the packed DIB. A packed DIB consists of a BITMAPINFO structure immediately followed by the array of bytes that define the pixels of the bitmap. 我想把这个logBrush的lbStyle值设为BS_DIBPATTERN,然后这个初始化一个Pen,再用这个Pen画线
    HANDLE hFile = CreateFile(_T("1.bmp"),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
    NULL,NULL);
    if(hFile == INVALID_HANDLE_VALUE )
    {
    MessageBox(_T("aa"));
    return;
    } DWORD dwFileSize,dwHighSize,dwBytesRead;
    BITMAPFILEHEADER * pbmfh;
    BOOL bSuccess;
    dwFileSize = GetFileSize(hFile,&dwHighSize);
    if(dwHighSize)
    {
    CloseHandle(hFile);
    return;
    }
    pbmfh =(BITMAPFILEHEADER *) malloc(dwFileSize);
    if(!pbmfh)
    {
    CloseHandle(hFile);
    return ;
    }
    bSuccess = ReadFile(hFile,pbmfh,54,&dwBytesRead,NULL);
    HGDIOBJ hmem = GlobalAlloc(GMEM_MOVEABLE,dwFileSize - 54);
    memcpy(hmem,pbmfh,dwFileSize - 54); LOGBRUSH lb;
    lb.lbStyle = BS_DIBPATTERNPT;
    lb.lbColor =DIB_RGB_COLORS;
    lb.lbHatch =(ULONG_PTR) hmem;
    HPEN hPen1 = ExtCreatePen(PS_GEOMETRIC | PS_DASH ,20,&lb,0,NULL);
    CClientDC dc(this);
    HGDIOBJ hOldPen;
    hOldPen = dc.SelectObject(hPen1);
    dc.MoveTo(25,25);
    dc.LineTo(100,100); CloseHandle(hFile);
    free(pbmfh);结果是想用选入的位图画线
      

  3.   

    这个位图指针没定位正确,起码应该移过DIB的文件头及信息头,不知道要不要移到颜色表之后,似乎那个地方就是所谓的句柄,不知道对不对?