源代码如下:
// Dib.cpp : 定义应用程序的入口点。
//#include "stdafx.h"
#include "Dib.h"
#include "resource.h"
#define MAX_LOADSTRING 100struct SeedInfo
{
int seedHeight,seedWidth;
int bytesPerLine;
int seedPtNumbers;
};// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名void LoadSeedBmp(HINSTANCE hInst,UINT ID_BMP,BYTE* map,SeedInfo* si);
void LoadSeedBmp(HINSTANCE hInst,UINT ID_BMP,BYTE* map,SeedInfo* si)
{
ASSERT( hInst!= NULL );
ASSERT( map != NULL );
ASSERT( si != NULL );
//BYTE mask[8]={1<<7,1<<6,1<<5,1<<4,1<<3,1<<2,1<<1,1<<0};
BYTE mask[8]={1<<0,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7};
CBitmap bmp; HBITMAP hBmp =  LoadBitmap(hInst,MAKEINTRESOURCE( ID_BMP) );
ASSERT( hBmp);
bmp.Attach(hBmp); BITMAP info;
bmp.GetBitmap(&info);

si->seedHeight = info.bmHeight;
si->seedWidth = info.bmWidth;
TRACE2("\nwidth = %d height = %d",si->seedWidth,si->seedHeight); ASSERT( info.bmBitsPixel == 1 );
int seedCount =0; si->bytesPerLine = ( (  info.bmWidth * info.bmBitsPixel + 31 ) / 32*4 ) /4 * 4; TRACE2("\n计算所得每行需字节数=%d,结构显示=%d",si->bytesPerLine,info.bmWidthBytes);
//si->bytesPerLine = info.bmWidthBytes;
int dataLong = si->bytesPerLine * si->seedHeight ;
BYTE* pData = new BYTE[  dataLong * 2 ];
//GetDIBits();

long lRet = bmp.GetBitmapBits( dataLong , pData );
TRACE1("\nGetBitmapBits return = %d * 1000",lRet / 1000);


BYTE* pLine = NULL;
register int bit=0,row =0,byte=0,col=0,idx=0; BYTE aLine;
for (row = 0 ; row < si->seedHeight ; row++ )
{
pLine = pData + row * (si->bytesPerLine);
col=0;
aLine=0;
        for ( byte = 0 ;  (byte < si->bytesPerLine ) ; byte ++)
{
aLine = pLine[byte];
//TRACE1("\n%d",aLine);
for(bit=7; (col < si->seedWidth) && ( bit >= 0 );bit--,col++)
{
if ( aLine & mask[bit] )
{
idx = row * si->seedWidth + col;
map [ idx ] = 1;
//TRACE1("\n%d",idx);
seedCount++;
}
}
} }
si->seedPtNumbers=seedCount;
TRACE1("\nSeedCnt =%d",seedCount);
delete []pData;}
BYTE map[ 1024* 768  ]= {0};SeedInfo si;
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
  // TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;


// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DIB, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance); // 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow)) 
{
return FALSE;
} hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DIB); // 主消息循环:
while (GetMessage(&msg, NULL, 0, 0)) 
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return (int) msg.wParam;
}//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
//  注释: 
//
//    仅当希望在已添加到 Windows 95 的
//    “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
//    才需要此函数及其用法。调用此函数
//    十分重要,这样应用程序就可以获得关联的
//   “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX);  wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DIB);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_DIB;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);
}//
//   函数: InitInstance(HANDLE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释: 
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;   hInst = hInstance; // 将实例句柄存储在全局变量中   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);   if (!hWnd)
   {
      return FALSE;
   }   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);   return TRUE;
}//
//  函数: WndProc(HWND, unsigned, WORD, LONG)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND - 处理应用程序菜单
//  WM_PAINT - 绘制主窗口
//  WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc; switch (message) 
{
case WM_CREATE:
LoadSeedBmp(hInst,IDB_BITMAP1,map,&si);
break;
case WM_COMMAND:
wmId    = LOWORD(wParam); 
wmEvent = HIWORD(wParam); 
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
TRIVERTEX        vert[2] ;
GRADIENT_RECT    gRect;
vert [0] .x      = 0;
vert [0] .y      = 0;
vert [0] .Red    = 0xff00;
vert [0] .Green  = 0xff;
vert [0] .Blue   = 0x0000;
vert [0] .Alpha  = 0x0000; vert [1] .x      = 1024;
vert [1] .y      = 32; 
vert [1] .Red    = 0x0000;
vert [1] .Green  = 0x0000;
vert [1] .Blue   = 0xff00;
vert [1] .Alpha  = 0x0022; gRect.UpperLeft  = 0;
gRect.LowerRight = 1;
GradientFill(hdc,vert,2,&gRect,1,GRADIENT_FILL_RECT_H); for(int x =0; x<si.seedWidth; x++)
{
for ( int y=0; y< si.seedHeight ;y++)
{
SetPixel(hdc,x,y,RGB(255,255, map[ x* si.seedHeight + y] ? 255 : 0) );
}
} EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}// “关于”框的消息处理程序。
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}