`
dato0123
  • 浏览: 917236 次
文章分类
社区版块
存档分类
最新评论

C++ Exercises(八)

 
阅读更多

1,创建进程

#include<windows.h>
#include
<stdio.h>

intmain(VOID)
{
STARTUPINFOsi;
PROCESS_INFORMATIONpi;
ZeroMemory(
&si,sizeof(si));
si.cb
=sizeof(si);
ZeroMemory(
&pi,sizeof(pi));
//Startthechildprocess.
if(!CreateProcess(NULL,//Nomodulename(usecommandline).
"C://WINDOWS//system32//mspaint.exe",//Commandline.
NULL,//Processhandlenotinheritable.
NULL,//Threadhandlenotinheritable.
FALSE,//SethandleinheritancetoFALSE.
0,//Nocreationflags.
NULL,//Useparent'senvironmentblock.
NULL,//Useparent'sstartingdirectory.
&si,//PointertoSTARTUPINFOstructure.
&pi)//PointertoPROCESS_INFORMATIONstructure.
)
{
printf(
"CreateProcessfailed(%d)./n",GetLastError());
return-1;
}

//Waituntilchildprocessexits.
WaitForSingleObject(pi.hProcess,INFINITE);
//Closeprocessandthreadhandles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

2,创建线程


#include
<stdio.h>
#include
<iostream>
#include
<windows.h>
usingnamespacestd;

DWORDSum;
/**//*dataissharedbythethread(s)*/

/**//*thethreadrunsinthisseparatefunction*/
DWORDWINAPISummation(PVOIDParam)
{
DWORDUpper
=*(DWORD*)Param;
for(DWORDi=0;i<=Upper;i++)
Sum
+=i;
return0;
}


intmain(intargc,char*argv[])
{
DWORDThreadId;
HANDLEThreadHandle;
intParam=100;
if(Param<0)
{
fprintf(stderr,
"aninteger>=0isrequired/n");
return-1;
}

//createthethread
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>
usingnamespacestd;

charszAppName[]="BMPLoad";

LRESULTCALLBACKWindowProc(HWND,UINT,WPARAM,LPARAM);

//**********
//classCRaster
//-GenericclassforBMPrasterimages.
classCRaster{
public:
intWidth,Height;//Dimensions
intBPP;//BitsPerPixel.
char*Raster;//BitsoftheImage.
RGBQUAD*Palette;//RGBPalettefortheimage.
intBytesPerRow;//RowWidth(inbytes).
BITMAPINFO*pbmi;//BITMAPINFOstructure

//Memberfunctions(definedlater):
intLoadBMP(char*szFile);
intGDIPaint(HDChdc,intx,inty);
}
;

//**********
//WindowsMainFunction.
//-Herestartsourdemoprogram
intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
LPSTRlpCmdLine,
intnCmdShow)
{
HWNDhwnd;
MSGmsg;

WNDCLASSwc;
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,"BMPLoad",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);
}


returnmsg.wParam;
}


//**********
//MainWindowProcedure.
//-ProcessesWindowMessages
LRESULTCALLBACKWindowProc
(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
staticCRasterbmp;
HDChdc;
PAINTSTRUCTps;

switch(message)
{
caseWM_CREATE:
bmp.LoadBMP(
"example.bmp");
return0;
caseWM_PAINT:
hdc
=BeginPaint(hwnd,&ps);
bmp.GDIPaint(hdc,
10,10);
EndPaint(hwnd,
&ps);
return0;
caseWM_DESTROY:
PostQuitMessage(
0);
return0;
}

returnDefWindowProc(hwnd,message,wParam,lParam);
}


//**********
//CRaster::LoadBMPFile(FileName);
//-loadsaBMPfileintoaCRasterobject
//*supportsnon-RLE-compressedfilesof1,2,4,8&24bits-per-pixel
intCRaster::LoadBMP(char*szFile)
{
BITMAPFILEHEADERbmfh;
BITMAPINFOHEADERbmih;

//Openfile.
ifstreambmpfile(szFile,ios::in|ios::binary);
if(!bmpfile.is_open())return1;//Erroropeningfile

//Loadbitmapfileheader&infoheader
bmpfile.read((char*)&bmfh,sizeof(BITMAPFILEHEADER));
bmpfile.read((
char*)&bmih,sizeof(BITMAPINFOHEADER));

//Checkfiletypesignature
if(bmfh.bfType!='MB')return2;//FileisnotBMP

//Assignsomeshortvariables:
BPP=bmih.biBitCount;
Width
=bmih.biWidth;
Height
=(bmih.biHeight>0)?bmih.biHeight:-bmih.biHeight;//absoultevalue
BytesPerRow=Width*BPP/8;
BytesPerRow
+=(4-BytesPerRow%4)%4;//intalignment

//IfBPParen't24,loadPalette:
if(BPP==24)pbmi=(BITMAPINFO*)newchar[sizeof(BITMAPINFO)];
else
{
pbmi
=(BITMAPINFO*)newchar[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];
Palette
=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));
bmpfile.read((
char*)Palette,sizeof(RGBQUAD)*(1<<BPP));
}

pbmi
->bmiHeader=bmih;

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

Raster
=newchar[BytesPerRow*Height];

//(ifheightispositivethebmpisbottom-up,readitreversed)
if(bmih.biHeight>0)
for(intn=Height-1;n>=0;n--)
bmpfile.read(Raster
+BytesPerRow*n,BytesPerRow);
else
bmpfile.read(Raster,BytesPerRow
*Height);

//so,wealwayshaveaup-bottomraster(thatisnegativeheightforwindows):
pbmi->bmiHeader.biHeight=-Height;

bmpfile.close();

return0;
}


//**********
//CRaster::GDIPaint(hdc,x,y);
//*PaintsRastertoaWindowsDC.
intCRaster::GDIPaint(HDChdc,intx=0,inty=0)
{
//Painttheimagetothedevice.
returnSetDIBitsToDevice(hdc,x,y,Width,Height,0,0,
0,Height,(LPVOID)Raster,pbmi,0);
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics