我有功能
function bgSetDisableOverlappedContent(CAA: BOOL; var ErrorCode: DWORD; ErrorText: string): Boolean;
begin
errorCode := ERROR_SUCCESS;
ErrorText := '';
if not GetOSVersion >= 60 then
Exit;
Result := SystemParametersInfo(SPI_SETDISABLEOVERLAPPEDCONTENT, 0, @CAA, 0);
if not Result then
begin
ErrorCode := GetLastError;
ErrorText := GetErrorText(ErrorCode);
end;
end;
准确地说
procedure TForm1.Button3Click(Sender: TObject);
var
CAA: BOOL;
OS: TUsableInOS;
ErrorCode: DWORD;
ErrorText: string;
begin
CAA := False;
if bgSetDisableOverlappedContent(CAA, ErrorCode, ErrorText) then
ShowMessage('Success');
end;
但是,当我用下一个代码再次检查时
function bgGetDisableOverlappedContent(var CAA: BOOL; OS: TUsableInOS; ErrorCode: DWORD; ErrorText: string): Boolean;
begin
errorCode := ERROR_SUCCESS;
ErrorText := '';
os := tosVistaUp;
if not GetOSVersion >= 60 then
Exit;
Result := SystemParametersInfo(SPI_GETDISABLEOVERLAPPEDCONTENT, 0, @CAA, 0);
if not Result then
begin
ErrorCode := GetLastError;
ErrorText := GetErrorText(ErrorCode);
end;
end;
function GetOSVersion: Integer;
var
OSVersionInfo : TOSVersionInfo;
begin
Result:= 0;
FillChar(OsVersionInfo, Sizeof(OsVersionInfo), 0);
OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
if GetVersionEx(OSVersionInfo) then
begin
if OSVersionInfo.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
if (OsVersionInfo.dwMajorVersion = 5) and ((OsVersionInfo.dwMinorVersion = 0)) then
Result:= 50; //2000
if (OsVersionInfo.dwMajorVersion = 5) and ((OsVersionInfo.dwMinorVersion = 1)) then
Result:= 51; //XP
if (OsVersionInfo.dwMajorVersion = 5) and ((OsVersionInfo.dwMinorVersion = 2)) then
Result:= 52; //2003, 2003 R2
if (OsVersionInfo.dwMajorVersion = 6) and ((OsVersionInfo.dwMinorVersion = 0)) then
Result:= 60; //Vista, Windows Server 2008
if (OsVersionInfo.dwMajorVersion = 6) and ((OsVersionInfo.dwMinorVersion = 1)) then
Result:= 61; //Server 2008 R2, 7
end;
end;
end;
对于caa,结果又是真的,即使我精确地设置了caa:=false;
我在为Win 7工作。结果:=systemparametersinfo(spi_setdisableoverlappedcontent,0,@caa,0);为true,但spi_getdisableoverlappedcontent为caa返回true,即使在步骤中将其完全设置为false之前也是如此。
procedure TForm1.Button3Click(Sender: TObject);
var
CAA: BOOL;
OS: TUsableInOS;
ErrorCode: DWORD;
ErrorText: string;
Res: Bool;
begin
CAA := False;
{ if bgSetDisableOverlappedContent(CAA, ErrorCode, ErrorText) then
ShowMessage('Success'); }
Res := SystemParametersInfo(SPI_SETDISABLEOVERLAPPEDCONTENT,
0,
@CAA,
0);
Res := SystemParametersInfo(SPI_GETDISABLEOVERLAPPEDCONTENT,
0,
@CAA,
0);
if Caa then
ShowMessage('True')
else
ShowMessage('False');
end;
CAA是真实的。
你知道吗?
提前谢谢
博然
最佳答案
主要的问题是,当传递SPI_SETDISABLEOVERLAPPEDCONTENT
时,您应该传递一个BOOL
变量,但您传递的是一个指向BOOL
的指针。documentation表示:
pvparam参数是一个bool变量。将pvparam设置为true可禁用重叠内容,或将false设置为启用重叠内容。
这意味着设置属性的代码需要如下所示:
SystemParametersInfo(SPI_SETDISABLEOVERLAPPEDCONTENT, 0, Pointer(CAA), 0)
你的
GetOSVersion
是一场灾难。对不起,听起来很刺耳!对于Windows 8及更高版本,它返回0
。而且您的代码在运算符优先级方面有问题。你写:if not GetOSVersion >= 60 then
运算符优先级意味着
if (not GetOSVersion) >= 60 then
由于
GetOSVersion
返回一个有符号的值,(not GetOSVersion) >= 60
的计算结果为False
,与windows版本无关。那是因为not GetOSVersion
总是0。你想要逻辑否定而不是按位否定。所以你应该写
if not (GetOSVersion >= 60) then
或同等
if GetOSVersion < 60 then
实际上,有一个内置的函数来实现这一点。它被称为
CheckWin32Version
。这样称呼:if not CheckWin32Version(6, 0) then
exit;
不过,您的函数的其余部分有点混乱。按值传递
ErrorText
,然后分配给它。假设您打算让调用者接收该值。除非你经过var
,否则这不会发生。我个人会这样写你的程序:
procedure bgSetDisableOverlappedContent(CAA: BOOL);
begin
if CheckWin32Version(6, 0) then
if not SystemParametersInfo(SPI_SETDISABLEOVERLAPPEDCONTENT, 0, Pointer(CAA), 0) then
RaiseLastOSError;
end;
我认为最好将
SystemParametersInfo
中的错误转换为异常,因为这是一个异常情况。我敢说你的SystemParametersInfo
调用真的失败了。在这种情况下,没有必要为一些根本不会发生的事情构建错误代码返回机制。检查错误并转换为运行时异常。这使得调用代码更加简单。按钮单击处理程序可以简单得多:
procedure TForm1.Button3Click(Sender: TObject);
begin
bgSetDisableOverlappedContent(False);
end;
而且getter函数也比必需的复杂得多。我想要这样的:
function bgGetDisableOverlappedContent: Boolean;
var
CAA: BOOL;
begin
if not CheckWin32Version(6, 0) then
begin
Result := False;//or True, I don't know, you decide
exit;
end;
if not SystemParametersInfo(SPI_GETDISABLEOVERLAPPEDCONTENT, 0, @CAA, 0) then
RaiseLastOSError;
Result := CAA;
end;
关于windows - SPI_SETDISABLEOVERLAPPEDCONTENT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14085508/