导航
  • 主页
  • Windows SDK/API
  • 基础类
  • ActiveX
  • 数据库及相关技术
  • 网络及通讯开发
  • VCL组件使用和开发
  • 问答

请把PASCAL翻译成C++。代码在帖子里

CPerlAsm_Lx 2003-11-10 07:21:05
var
Form1 : TForm;
HookID : THandle;
DisableRButton : boolean;

implementation

function MouseProc(nCode: integer; wParam,
lParam: longint): longint; stdcall;
function GetClassNameEnh(Handle: Hwnd): string;
var
ClassName: PChar;
Buffer: integer;
begin
Buffer := 40;
getmem(ClassName, Buffer);
windows.GetClassName(Handle, ClassName, Buffer);
Result := ClassName;
Freemem(ClassName);
end;

var
P: TPoint;
begin
if (nCode < 0) then
Result := CallNextHookEx(HookID, nCode, wParam, lParam)
else if DisableRButton and ((wParam = WM_RBUTTONDOWN) or (wParam =
WM_RBUTTONUP)) then
begin
GetCursorPos(P);
if Pos('Internet Explorer_', GetClassNameEnh(WindowFromPoint(P))) <> 0
then
Result := HC_SKIP
else
Result := CallNextHookEx(HookID, nCode, wParam, lParam);
end
else
Result := CallNextHookEx(HookID, nCode, wParam, lParam);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
DisableRButton := True;
HookID := SetWindowsHookEx(WH_MOUSE, MouseProc, 0,
GetCurrentThreadId());
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if HookID <> 0 then
UnHookWindowsHookEx(HookID);
end;


...全文
6 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
短歌如风 2003-11-13
CPerlAsm_Lx:
MouseProc是全局函数呀!你是不是写成成员函数了?
返回值改为int。因为我看原来定义是longint,所以就写了long。C++的指针类型检查比Object Pascal严格,如果不行就reinterpret_cast
回复
CPerlAsm_Lx 2003-11-13
[C++ Error] Unit1.cpp(24): E2034 Cannot convert 'long (__stdcall * (_closure )(int,long,long))(int,long,long)' to 'int (__stdcall *)()'
[C++ Error] Unit1.cpp(24): E2342 Type mismatch in parameter 'lpfn' (wanted 'int (__stdcall *)()', got 'void')
[C++ Error] Unit1.cpp(28): E2141 Declaration syntax error
回复
CPerlAsm_Lx 2003-11-13
楼上的2位。

[C++ Error] Unit1.cpp(24): E2451 Undefined symbol 'MouseProc'
[C++ Error] Unit1.cpp(28): E2141 Declaration syntax error

//根本就没定义钩子勾的函数。怎么不调试呢。不是浪费大家双被时间?
回复
yjy1001 2003-11-12
也来献丑
TForm * Form1;
HANDLE HookID;
bool DisableRButton;

AnsiString __fastcall function GetClassNameEnh( HWND Handle )
{
char * ClassName;
int Buffer;
Buffer = 40;
ClassName = new char[Buffer];
GetClassName( Handle, ClassName, Buffer );
AnsiString Result( ClassName );
delete ClassName;
return Result;
}
__stdcall long MouseProc( int nCode, long wParam, long lParam )
{
TPoint P;
if ( nCode < 0 )
return CallNextHookEx( HookID, nCode, wParam, lParam );
else if (DisableRButton && ( ( wParam == WM_RBUTTONDOWN ) || ( wParam == WM_RBUTTONUP ) ))
{
GetCursorPos( & P );
if ( AnsiString( GetClassNameEnh( WindowFromPoint( &P ) ) ).Pos( “Internet Explorer_” ) != 0 )
return HC_SKIP;
else
return CallNextHookEx( HookID, nCode, wParam, lParam );
}
else
return CallNextHookEx( HookID, nCode, wParam, lParam );
}


void __fastcall TForm1::FormCreate( TObject * Sender )
{
DisableRButton = True;
HookID = SetWindowsHookEx( WH_MOUSE, MouseProc, 0, GetCurrentThreadId() );
}

void __fastcall TForm1::FormDestroy( TObject * Sender );
{
if ( HookID != 0 )
UnHookWindowsHookEx( HookID );
}
修改了 下: plainsong(短歌) 的代码 因为 他没注意到 bcb 的if 要(),和 ‘ 与“的改写,以及漏写了 2个 “ ;” 估计没有调试过……

我写完 也没调试过。

唉 PASCAL 写 没有缩进 的程序 好难看啊??? 是 有点晕

这个程序是个鼠标钩子,在鼠标右键按下和放开的时候 判断 鼠标右击的是否为 IE 程序。
回复
有哥 2003-11-10
有点晕
回复
短歌如风 2003-11-10
贴上来才发现有错误。上面的作废,试试下面的。

TForm * Form1;
HANDLE HookID;
bool DisableRButton;

AnsiString __fastcall function GetClassNameEnh( HWND Handle )
{
char * ClassName;
int Buffer;
Buffer = 40;
ClassName = new char[Buffer];
GetClassName( Handle, ClassName, Buffer );
AnsiString Result( ClassName );
delete []ClassName;
return Result;
}

__stdcall long MouseProc( int nCode, long wParam, long lParam )
{
TPoint P;
if ( nCode < 0 )
return CallNextHookEx( HookID, nCode, wParam, lParam )
else if DisableRButton && ( ( wParam == WM_RBUTTONDOWN ) || ( wParam == WM_RBUTTONUP ) )
{
GetCursorPos( & P );
if ( AnsiString( GetClassNameEnh( WindowFromPoint( &P ) ) ).Pos( 'Internet Explorer_' ) != 0 )
return HC_SKIP
else
return CallNextHookEx( HookID, nCode, wParam, lParam );
}
else
return CallNextHookEx( HookID, nCode, wParam, lParam );
}


void __fastcall TForm1::FormCreate( TObject * Sender )
{
DisableRButton = True;
HookID = SetWindowsHookEx( WH_MOUSE, MouseProc, 0, GetCurrentThreadId() );
}

void __fastcall TForm1::FormDestroy( TObject * Sender );
{
if ( HookID != 0 )
UnHookWindowsHookEx( HookID );
}
回复
短歌如风 2003-11-10

TForm * Form1;
HANDLE HookID;
bool DisableRButton;

AnsiString __fastcall function GetClassNameEnh( HWND Handle )
{
char * ClassName;
int Buffer;
Buffer = 40;
ClassName = new char[Buffer];
GetClassName( Handle, ClassName, Buffer );
AnsiString Result( ClassName );
delete ClassName;
return Result;
}

__stdcall long MouseProc( in nCode, long wParam, long lParam )
{
POINT P;
if ( nCode < 0 )
return CallNextHookEx( HookID, nCode, wParam, lParam )
else if DisableRButton && ( ( wParam = WM_RBUTTONDOWN ) || ( wParam = WM_RBUTTONUP ) )
{
GetCursorPos( & P );
if ( AnsiString( GetClassNameEnh( WindowFromPoint( P ) ) ).Pos( 'Internet Explorer_' ) != 0 )
return HC_SKIP
else
return CallNextHookEx( HookID, nCode, wParam, lParam );
}
else
return CallNextHookEx( HookID, nCode, wParam, lParam );
}


void __fastcall TForm1::FormCreate( TObject * Sender )
{
DisableRButton = True;
HookID = SetWindowsHookEx( WH_MOUSE, MouseProc, 0, GetCurrentThreadId() );
}

void __fastcall TForm1::FormDestroy( TObject * Sender );
{
if ( HookID != 0 )
UnHookWindowsHookEx( HookID );
}
回复
发动态
发帖子
C++ Builder
创建于2007-08-02

1.3w+

社区成员

C++ Builder相关内容讨论区
申请成为版主
社区公告
暂无公告