?? unit2.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormCreate(TObject *Sender)
{
Pointer Address;
int X, Y, Total;
hMapFile = CreateFileMapping((HANDLE)(0xFFFFFFFF),
NULL,
PAGE_READWRITE,
0,
10000,
"S08IMappingFile");
if( hMapFile!=NULL )
pMapFile = MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0);
else
ShowMessage( "內存映射文件句柄(hMapFile)為空" );
if( pMapFile==NULL )
ShowMessage( "獲取的內存映射文件指針(pMapFile)為空" );
else
{
randomize();
Pointer Address;
nPosition = 0;
do
{
nPosition++;
Address = PChar(pMapFile) + (nPosition-1)*4;
} while( *(PInteger(Address))!=0 );
if( nPosition<=10 )
{
Label1->Caption = "工位: " + IntToStr(nPosition);
*(PInteger(Address)) = *(PInteger(Address)) + (random(10)+1);
}
else
{
ShowMessage("已經沒有工位(最多10個)。");
exit(0);
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormDestroy(TObject *Sender)
{
Pointer Address;
// 刪除內存映射文件中當前實例
Address = PChar(pMapFile) +(nPosition-1)*4;
*(PInteger(Address)) = 0;
UnmapViewOfFile(pMapFile);
CloseHandle(hMapFile);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Timer1Timer(TObject *Sender)
{
Pointer Address;
Address = PChar(pMapFile) + (nPosition-1)*4;
*(PInteger(Address)) = *(PInteger(Address)) + (random(10)+1);
// 向監視程序發送WM_USER消息
Address = PChar(pMapFile) +100*4;
while( *(PInteger(Address))!=0 )
{
PostMessage((HANDLE)(*(PInteger(Address))), WM_USER, 0, 0);
Address = PChar(Address) + 4;
}
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -