不兼容的类型:'regular procedure and method pointer'
Incompatible types: 'regular procedure and method pointer'
我正在按照 this 示例在线程内创建一个 window,在我的特定代码上,编译器说:
Incompatible types: 'regular procedure and method pointer'
这一行:
if (MagSetImageScalingCallback(hWndMag, MagImageScalingCallback)) then;
参考我的 MagImageScalingCallback 回调。
这里是你的线程是如何定义的 methods/functions:
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
Classes,
SysUtils,
Magnification;
type
THostWNDThread = class(TThread)
private
hWndMag: HWND;
procedure SetMagnificationWND;
function MagImageScalingCallback(HWND: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
protected
public
end;
procedure THostWNDThread.SetMagnificationWND;
begin
if (MagSetImageScalingCallback(hWndMag, MagImageScalingCallback)) then;
end;
function THostWNDThread.MagImageScalingCallback(HWND: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
begin
Result := True;
end;
begin
try
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
放大单位:
unit Magnification;
{$ALIGN ON}
{$MINENUMSIZE 4}
interface
uses
Windows;
const
// Magnifier Class Name
WC_MAGNIFIERA: AnsiString = 'Magnifier';
WC_MAGNIFIERW: WideString = 'Magnifier';
WC_MAGNIFIER = 'Magnifier';
// Magnifier Window Styles
MS_SHOWMAGNIFIEDCURSOR = [=13=]01;
MS_CLIPAROUNDCURSOR = [=13=]02;
MS_INVERTCOLORS = [=13=]04;
// Filter Modes
MW_FILTERMODE_EXCLUDE = 0;
MW_FILTERMODE_INCLUDE = 1;
type
tagMAGTRANSFORM = record
v: array[1..3, 1..3] of Single;
end;
MAGTRANSFORM = tagMAGTRANSFORM;
TMagTransform = tagMAGTRANSFORM;
PMagTransform = ^TMagTransform;
tagMAGIMAGEHEADER = record
width: UINT;
height: UINT;
format: TGUID;
stride: UINT;
offset: UINT;
cbSize: UINT;
end;
MAGIMAGEHEADER = tagMAGIMAGEHEADER;
TMagImageHeader = tagMAGIMAGEHEADER;
PMagImageHeader = ^TMagImageHeader;
tagMAGCOLOREFFECT = record
transform: array[1..5, 1..5] of Single;
end;
MAGCOLOREFFECT = tagMAGCOLOREFFECT;
TMagColorEffect = tagMAGCOLOREFFECT;
PMagColorEffect = ^TMagColorEffect;
TMagImageScalingCallback = function (hwnd: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
THWNDArray = array[0..0] of HWND;
PHWNDArray = ^THWNDArray;
// Public Functions
function MagInitialize(): BOOL; stdcall;
function MagUninitialize(): BOOL; stdcall;
function MagSetWindowSource(hwnd: HWND; rect: TRect): BOOL; stdcall;
function MagGetWindowSource(hwnd: HWND; var Rect: TRect): BOOL; stdcall;
function MagSetWindowTransform(hwnd: HWND; var Transform: TMagTransform): BOOL; stdcall;
function MagGetWindowTransform(hwnd: HWND; var Transform: TMagTransform): BOOL; stdcall;
function MagSetWindowFilterList(hwnd: HWND; dwFilterMode: DWORD;
count: Integer; pHWND: PHWNDArray): BOOL; stdcall;
function MagGetWindowFilterList(hwnd: HWND; var dwFilterMode: DWORD;
count: Integer; pHWND: PHWNDArray): Integer; stdcall;
function MagSetImageScalingCallback(hwnd: HWND;
MagImageScalingCallback: TMagImageScalingCallback): BOOL; stdcall;
// MagImageScalingCallback WINAPI MagGetImageScalingCallback(HWND hwnd );
function MagSetColorEffect(hwnd: HWND; var Effect: TMagColorEffect): BOOL; stdcall;
function MagGetColorEffect(hwnd: HWND; var Effect: TMagColorEffect): BOOL; stdcall;
implementation
const
MagnificationDll = 'Magnification.dll';
function MagInitialize; external MagnificationDll name 'MagInitialize';
function MagUninitialize; external MagnificationDll name 'MagUninitialize';
function MagSetWindowSource; external MagnificationDll name 'MagSetWindowSource';
function MagGetWindowSource; external MagnificationDll name 'MagGetWindowSource';
function MagSetWindowTransform; external MagnificationDll name 'MagSetWindowTransform';
function MagGetWindowTransform; external MagnificationDll name 'MagGetWindowTransform';
function MagSetWindowFilterList; external MagnificationDll name 'MagSetWindowFilterList';
function MagGetWindowFilterList; external MagnificationDll name 'MagGetWindowFilterList';
function MagSetImageScalingCallback; external MagnificationDll name 'MagSetImageScalingCallback';
function MagSetColorEffect; external MagnificationDll name 'MagSetColorEffect';
function MagGetColorEffect; external MagnificationDll name 'MagGetColorEffect';
end.
您不能将 非静态 class 方法用作 API 回调。您需要:
使用 class
和 static
说明符声明 MagImageScalingCallback()
:
class function MagImageScalingCallback(hwnd: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall; static;
使MagImageScalingCallback()
成为一个独立的非成员函数。
如果您查看您的代码所基于的示例,它使用 class static
方法作为其 WindowProc
API 回调。
无论哪种方式,回调都不能有隐式 Self
参数,因此如果您需要在回调中访问 class 数据,您可以传递 class 的 Self
通过 Win32 API (Get|Set)WindowLong/Ptr(GWL_USERDATA)
或 (Get|Set)Prop()
函数使用 HWND
指向回调的指针。例如:
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
Classes,
SysUtils,
Magnification;
type
THostWNDThread = class(TThread)
private
class function WindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; static;
class function MagImageScalingCallback(hwnd: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall; static;
protected
procedure Execute; override;
public
end;
procedure THostWNDThread.Execute;
var
Msg: TMsg;
WndClass: WNDCLASS;
hWndMag: HWND;
begin
with WndClass do
begin
Style := 0;
lpfnWndProc := @WindowProc;
cbClsExtra := 0;
cbWndExtra := 0;
hInstance := HInstance;
hIcon := 0;
hCursor := LoadCursor(0, IDC_ARROW);
hbrBackground := COLOR_WINDOW;
lpszMenuName := nil;
lpszClassName := 'THostWNDThread';
end;
if not Windows.RegisterClass(FWndClass) then
RaiseLastOSError;
try
hWndMag := CreateWindow(WndClass.lpszClassName, nil, DesiredStyles, DesiredXPos, DesiredYPos, DesiredWidth, DesiredHeight, 0, 0, HInstance, nil);
if hWndMag = 0 then
RaiseLastOSError;
try
SetWindowLongPtr(hWndMag, GWL_USERDATA, LONG_PTR(Self));
if not MagSetImageScalingCallback(hWndMag, MagImageScalingCallback) then
raise Exception.Create('Cant set Scaling callback');
while GetMessage(Msg, hWndMag, 0, 0) > 0 do
begin
TranslateMessage(msg);
DispatchMessage(msg)
end;
finally
DestroyWindow(hWndMag);
end;
finally
Windows.UnregisterClass(WndClass.lpszClassName, HInstance);
end;
end;
class function THostWNDThread.WindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
// handle uMsg as needed...
Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
end;
class function THostWNDThread.MagImageScalingCallback(hwnd: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
var
pThread: THostWNDThread;
begin
pThread := THostWNDThread(GetWindowLongPtr(hwnd, GWL_USERDATA));
// use pThread and parameters as needed ...
Result := True;
end;
var
Thread: THostWNDThread;
begin
try
Thread := THostWNDThread.Create(False);
Thread.WaitFor;
Thread.Free;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
我正在按照 this 示例在线程内创建一个 window,在我的特定代码上,编译器说:
Incompatible types: 'regular procedure and method pointer'
这一行:
if (MagSetImageScalingCallback(hWndMag, MagImageScalingCallback)) then;
参考我的 MagImageScalingCallback 回调。
这里是你的线程是如何定义的 methods/functions:
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
Classes,
SysUtils,
Magnification;
type
THostWNDThread = class(TThread)
private
hWndMag: HWND;
procedure SetMagnificationWND;
function MagImageScalingCallback(HWND: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
protected
public
end;
procedure THostWNDThread.SetMagnificationWND;
begin
if (MagSetImageScalingCallback(hWndMag, MagImageScalingCallback)) then;
end;
function THostWNDThread.MagImageScalingCallback(HWND: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
begin
Result := True;
end;
begin
try
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
放大单位:
unit Magnification;
{$ALIGN ON}
{$MINENUMSIZE 4}
interface
uses
Windows;
const
// Magnifier Class Name
WC_MAGNIFIERA: AnsiString = 'Magnifier';
WC_MAGNIFIERW: WideString = 'Magnifier';
WC_MAGNIFIER = 'Magnifier';
// Magnifier Window Styles
MS_SHOWMAGNIFIEDCURSOR = [=13=]01;
MS_CLIPAROUNDCURSOR = [=13=]02;
MS_INVERTCOLORS = [=13=]04;
// Filter Modes
MW_FILTERMODE_EXCLUDE = 0;
MW_FILTERMODE_INCLUDE = 1;
type
tagMAGTRANSFORM = record
v: array[1..3, 1..3] of Single;
end;
MAGTRANSFORM = tagMAGTRANSFORM;
TMagTransform = tagMAGTRANSFORM;
PMagTransform = ^TMagTransform;
tagMAGIMAGEHEADER = record
width: UINT;
height: UINT;
format: TGUID;
stride: UINT;
offset: UINT;
cbSize: UINT;
end;
MAGIMAGEHEADER = tagMAGIMAGEHEADER;
TMagImageHeader = tagMAGIMAGEHEADER;
PMagImageHeader = ^TMagImageHeader;
tagMAGCOLOREFFECT = record
transform: array[1..5, 1..5] of Single;
end;
MAGCOLOREFFECT = tagMAGCOLOREFFECT;
TMagColorEffect = tagMAGCOLOREFFECT;
PMagColorEffect = ^TMagColorEffect;
TMagImageScalingCallback = function (hwnd: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
THWNDArray = array[0..0] of HWND;
PHWNDArray = ^THWNDArray;
// Public Functions
function MagInitialize(): BOOL; stdcall;
function MagUninitialize(): BOOL; stdcall;
function MagSetWindowSource(hwnd: HWND; rect: TRect): BOOL; stdcall;
function MagGetWindowSource(hwnd: HWND; var Rect: TRect): BOOL; stdcall;
function MagSetWindowTransform(hwnd: HWND; var Transform: TMagTransform): BOOL; stdcall;
function MagGetWindowTransform(hwnd: HWND; var Transform: TMagTransform): BOOL; stdcall;
function MagSetWindowFilterList(hwnd: HWND; dwFilterMode: DWORD;
count: Integer; pHWND: PHWNDArray): BOOL; stdcall;
function MagGetWindowFilterList(hwnd: HWND; var dwFilterMode: DWORD;
count: Integer; pHWND: PHWNDArray): Integer; stdcall;
function MagSetImageScalingCallback(hwnd: HWND;
MagImageScalingCallback: TMagImageScalingCallback): BOOL; stdcall;
// MagImageScalingCallback WINAPI MagGetImageScalingCallback(HWND hwnd );
function MagSetColorEffect(hwnd: HWND; var Effect: TMagColorEffect): BOOL; stdcall;
function MagGetColorEffect(hwnd: HWND; var Effect: TMagColorEffect): BOOL; stdcall;
implementation
const
MagnificationDll = 'Magnification.dll';
function MagInitialize; external MagnificationDll name 'MagInitialize';
function MagUninitialize; external MagnificationDll name 'MagUninitialize';
function MagSetWindowSource; external MagnificationDll name 'MagSetWindowSource';
function MagGetWindowSource; external MagnificationDll name 'MagGetWindowSource';
function MagSetWindowTransform; external MagnificationDll name 'MagSetWindowTransform';
function MagGetWindowTransform; external MagnificationDll name 'MagGetWindowTransform';
function MagSetWindowFilterList; external MagnificationDll name 'MagSetWindowFilterList';
function MagGetWindowFilterList; external MagnificationDll name 'MagGetWindowFilterList';
function MagSetImageScalingCallback; external MagnificationDll name 'MagSetImageScalingCallback';
function MagSetColorEffect; external MagnificationDll name 'MagSetColorEffect';
function MagGetColorEffect; external MagnificationDll name 'MagGetColorEffect';
end.
您不能将 非静态 class 方法用作 API 回调。您需要:
使用
class
和static
说明符声明MagImageScalingCallback()
:class function MagImageScalingCallback(hwnd: HWND; srcdata: Pointer; srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER; unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall; static;
使
MagImageScalingCallback()
成为一个独立的非成员函数。
如果您查看您的代码所基于的示例,它使用 class static
方法作为其 WindowProc
API 回调。
无论哪种方式,回调都不能有隐式 Self
参数,因此如果您需要在回调中访问 class 数据,您可以传递 class 的 Self
通过 Win32 API (Get|Set)WindowLong/Ptr(GWL_USERDATA)
或 (Get|Set)Prop()
函数使用 HWND
指向回调的指针。例如:
program Project2;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
Classes,
SysUtils,
Magnification;
type
THostWNDThread = class(TThread)
private
class function WindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; static;
class function MagImageScalingCallback(hwnd: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall; static;
protected
procedure Execute; override;
public
end;
procedure THostWNDThread.Execute;
var
Msg: TMsg;
WndClass: WNDCLASS;
hWndMag: HWND;
begin
with WndClass do
begin
Style := 0;
lpfnWndProc := @WindowProc;
cbClsExtra := 0;
cbWndExtra := 0;
hInstance := HInstance;
hIcon := 0;
hCursor := LoadCursor(0, IDC_ARROW);
hbrBackground := COLOR_WINDOW;
lpszMenuName := nil;
lpszClassName := 'THostWNDThread';
end;
if not Windows.RegisterClass(FWndClass) then
RaiseLastOSError;
try
hWndMag := CreateWindow(WndClass.lpszClassName, nil, DesiredStyles, DesiredXPos, DesiredYPos, DesiredWidth, DesiredHeight, 0, 0, HInstance, nil);
if hWndMag = 0 then
RaiseLastOSError;
try
SetWindowLongPtr(hWndMag, GWL_USERDATA, LONG_PTR(Self));
if not MagSetImageScalingCallback(hWndMag, MagImageScalingCallback) then
raise Exception.Create('Cant set Scaling callback');
while GetMessage(Msg, hWndMag, 0, 0) > 0 do
begin
TranslateMessage(msg);
DispatchMessage(msg)
end;
finally
DestroyWindow(hWndMag);
end;
finally
Windows.UnregisterClass(WndClass.lpszClassName, HInstance);
end;
end;
class function THostWNDThread.WindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
// handle uMsg as needed...
Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
end;
class function THostWNDThread.MagImageScalingCallback(hwnd: HWND; srcdata: Pointer;
srcheader: MAGIMAGEHEADER; destdata: Pointer; destheader: MAGIMAGEHEADER;
unclipped: TRect; clipped: TRect; dirty: HRGN): BOOL; stdcall;
var
pThread: THostWNDThread;
begin
pThread := THostWNDThread(GetWindowLongPtr(hwnd, GWL_USERDATA));
// use pThread and parameters as needed ...
Result := True;
end;
var
Thread: THostWNDThread;
begin
try
Thread := THostWNDThread.Create(False);
Thread.WaitFor;
Thread.Free;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.