1,创建进程
#include <windows.h>
#include <stdio.h>

int main( VOID )


{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = 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.
0, // No creation flags.
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() );
return -1;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}

2,创建线程

#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;


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++)
Sum += i;
return 0;
}

int main(int argc, char *argv[])


{
DWORD ThreadId;
HANDLE ThreadHandle;
int Param = 100;
if (Param < 0)


{
fprintf(stderr, "an integer >= 0 is required \n");
return -1;
}
// create the thread
ThreadHandle = CreateThread(NULL, 0, Summation, &Param, 0, &ThreadId);

if (ThreadHandle != NULL)
{
WaitForSingleObject(ThreadHandle, INFINITE);
CloseHandle(ThreadHandle);
printf("sum = %d\n",Sum);
}
}

3,加载BMP文件

#include <windows.h>
#include <fstream>
using namespace std;

char szAppName [] = "BMPLoad";

LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);

// **********
// class CRaster
// - Generic class for BMP raster images.

class CRaster
{
public:
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 )


{
HWND hwnd;
MSG msg;

WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;

RegisterClass (&wc);

hwnd = CreateWindow (szAppName,"BMP Load",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
0,0,hInstance,0);

ShowWindow (hwnd,nCmdShow);
UpdateWindow (hwnd);

while (GetMessage(&msg,0,0,0))

{
TranslateMessage (&msg);
DispatchMessage (&msg);
}

return msg.wParam;
}

// **********
// Main Window Procedure.
// - Processes Window Messages
LRESULT CALLBACK WindowProc
(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)


{
static CRaster bmp;
HDC hdc;
PAINTSTRUCT ps;

switch (message)

{
case WM_CREATE:
bmp.LoadBMP ("example.bmp");
return 0;
case WM_PAINT:
hdc=BeginPaint (hwnd,&ps);
bmp.GDIPaint (hdc,10,10);
EndPaint (hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
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)


{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;

// Open file.
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:
BPP=bmih.biBitCount;
Width=bmih.biWidth;
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)];
else

{
pbmi=(BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];
Palette=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));
bmpfile.read ((char*)Palette,sizeof (RGBQUAD) * (1<<BPP));
}
pbmi->bmiHeader=bmih;

// Load Raster
bmpfile.seekg (bmfh.bfOffBits,ios::beg);

Raster= new char[BytesPerRow*Height];

// (if height is positive the bmp is bottom-up, read it reversed)
if (bmih.biHeight>0)
for (int n=Height-1;n>=0;n--)
bmpfile.read (Raster+BytesPerRow*n,BytesPerRow);
else
bmpfile.read (Raster,BytesPerRow*Height);

// so, we always have a up-bottom raster (that is negative height for windows):
pbmi->bmiHeader.biHeight=-Height;

bmpfile.close();

return 0;
}

// **********
// 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);
}


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述