2012-12-29 44 views
1

我有功能SPI_SETDISABLEOVERLAPPEDCONTENT

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);爲真,但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是正確的。

你有什麼想法嗎?

在此先感謝 博揚

+0

@ paulsm4該代碼是假的,但也有其他問題 –

回答

7

的主要問題是,經過SPI_SETDISABLEOVERLAPPEDCONTENT當你是爲了傳遞一個BOOL變量,但你傳遞一個指針BOOLdocumentation說:

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; 

劑和吸氣功能也比需要的要複雜得多。我有這樣的:

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;