ZeroMemory( &si, sizeof(si) );
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
"C:\\WINDOWS\\system32\\mspaint.exe", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
printf( "CreateProcess failed (%d).\n", GetLastError() );
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
DWORD Sum; /* data is shared by the thread(s) */
/* the thread runs in this separate function */
DWORD WINAPI Summation(PVOID Param)
DWORD Upper = *(DWORD *)Param;
for (DWORD i = 0; i <= Upper; i++)
int main(int argc, char *argv[])
fprintf(stderr, "an integer >= 0 is required \n");
ThreadHandle = CreateThread(NULL, 0, Summation, &Param, 0, &ThreadId);
if (ThreadHandle != NULL) {
WaitForSingleObject(ThreadHandle, INFINITE);
CloseHandle(ThreadHandle);
printf("sum = %d\n",Sum);
char szAppName [] = "BMPLoad";
LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);
// - Generic class for BMP raster images.
int Width,Height; // Dimensions
int BPP; // Bits Per Pixel.
char * Raster; // Bits of the Image.
RGBQUAD * Palette; // RGB Palette for the image.
int BytesPerRow; // Row Width (in bytes).
BITMAPINFO * pbmi; // BITMAPINFO structure
// Member functions (defined later):
int LoadBMP (char * szFile);
int GDIPaint (HDC hdc,int x,int y);
// Windows Main Function.
// - Here starts our demo program
int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
wc.lpszClassName = szAppName;
hwnd = CreateWindow (szAppName,"BMP Load",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
ShowWindow (hwnd,nCmdShow);
while (GetMessage(&msg,0,0,0))
// Main Window Procedure.
// - Processes Window Messages
LRESULT CALLBACK WindowProc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
bmp.LoadBMP ("example.bmp");
hdc=BeginPaint (hwnd,&ps);
bmp.GDIPaint (hdc,10,10);
return DefWindowProc (hwnd,message,wParam,lParam);
// CRaster::LoadBMPFile (FileName);
// - loads a BMP file into a CRaster object
// * supports non-RLE-compressed files of 1, 2, 4, 8 & 24 bits-per-pixel
int CRaster::LoadBMP (char * szFile)
ifstream bmpfile (szFile , ios::in | ios::binary);
if (! bmpfile.is_open()) return 1; // Error opening file
// Load bitmap fileheader & infoheader
bmpfile.read ((char*)&bmfh,sizeof (BITMAPFILEHEADER));
bmpfile.read ((char*)&bmih,sizeof (BITMAPINFOHEADER));
// Check filetype signature
if (bmfh.bfType!='MB') return 2; // File is not BMP
// Assign some short variables:
Height= (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight; // absoulte value
BytesPerRow = Width * BPP / 8;
BytesPerRow += (4-BytesPerRow%4) % 4; // int alignment
// If BPP aren't 24, load Palette:
if (BPP==24) pbmi=(BITMAPINFO*)new char [sizeof(BITMAPINFO)];
pbmi=(BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];
Palette=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));
bmpfile.read ((char*)Palette,sizeof (RGBQUAD) * (1<<BPP));
bmpfile.seekg (bmfh.bfOffBits,ios::beg);
Raster= new char[BytesPerRow*Height];
// (if height is positive the bmp is bottom-up, read it reversed)
for (int n=Height-1;n>=0;n--)
bmpfile.read (Raster+BytesPerRow*n,BytesPerRow);
bmpfile.read (Raster,BytesPerRow*Height);
// so, we always have a up-bottom raster (that is negative height for windows):
pbmi->bmiHeader.biHeight=-Height;
// CRaster::GDIPaint (hdc,x,y);
// * Paints Raster to a Windows DC.
int CRaster::GDIPaint (HDC hdc,int x=0,int y=0)
// Paint the image to the device.
return SetDIBitsToDevice (hdc,x,y,Width,Height,0,0,
0,Height,(LPVOID)Raster,pbmi,0);
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/01/11/1035848.html,如需转载请自行联系原作者