BSOne.SFC/Tocsg.Module/FindWindow/DFndWndMain.pas

268 lines
6.5 KiB
Plaintext
Raw Blame History

unit DFndWndMain;
interface
uses
Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, DAngle,
ThdCloseSidebar, Winapi.Windows;
type
TDlgFndWndMain = class(TForm)
toolAddWindow: TBitBtn;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure toolAddWindowMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure toolAddWindowMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure toolAddWindowMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
hPreTopHWND_: HWND;
bSeeking_: Boolean;
DlgAngle_: TDlgAngle;
ThdCSB_: TThdCloseSidebar;
function IsAbleAddWindow(hWindow: HWND): Boolean;
public
{ Public declarations }
Constructor Create(aOwner: TComponent); override;
Destructor Destroy; override;
end;
var
DlgFndWndMain: TDlgFndWndMain;
implementation
uses
Tocsg.WndUtil, Tocsg.Process, Winapi.WinSvc, Winapi.ActiveX,
System.Win.ComObj, Winapi.AccCtrl, Winapi.AclAPI;
{$R *.dfm}
Constructor TDlgFndWndMain.Create(aOwner: TComponent);
begin
Inherited Create(aOwner);
hPreTopHWND_ := 0;
bSeeking_ := false;
DlgAngle_ := TDlgAngle.Create(Self);
ThdCSB_ := nil;
end;
Destructor TDlgFndWndMain.Destroy;
begin
if ThdCSB_ <> nil then
FreeAndNil(ThdCSB_);
Inherited;
end;
procedure TDlgFndWndMain.Button1Click(Sender: TObject);
begin
if ThdCSB_ = nil then
begin
ThdCSB_ := TThdCloseSidebar.Create(DlgAngle_);
ThdCSB_.StartThread;
end;
end;
procedure TDlgFndWndMain.Button2Click(Sender: TObject);
begin
if ThdCSB_ <> nil then
FreeAndNil(ThdCSB_);
end;
function GetCurrentUserName: string;
var
UserName: array[0..MAX_PATH - 1] of Char;
Size: DWORD;
begin
Size := SizeOf(UserName);
GetUserName(UserName, Size);
Result := UserName;
end;
function DenyAllAccessToFile(const FileName: string): Boolean;
var
ea: EXPLICIT_ACCESS;
pac: PACL;
sd: PSECURITY_DESCRIPTOR;
begin
Result := False;
// EXPLICIT_ACCESS <20><><EFBFBD><EFBFBD>ü <20>ʱ<EFBFBD>ȭ
ZeroMemory(@ea, SizeOf(EXPLICIT_ACCESS));
ea.grfAccessPermissions := GENERIC_ALL;
ea.grfAccessMode := REVOKE_ACCESS;
ea.grfInheritance := NO_INHERITANCE;
ea.Trustee.TrusteeForm := TRUSTEE_IS_NAME;
ea.Trustee.TrusteeType := TRUSTEE_IS_USER;
ea.Trustee.ptstrName := PChar(GetCurrentUserName); // <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// ACL <20><><EFBFBD><EFBFBD>
if SetEntriesInAcl(1, @ea, nil, pac) <> ERROR_SUCCESS then
Exit;
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if GetNamedSecurityInfo(PChar(FileName), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nil, nil, @pac, nil, @sd) <> ERROR_SUCCESS then
begin
LocalFree(pac);
Exit;
end;
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
if SetNamedSecurityInfo(PChar(FileName), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nil, nil, pac, nil) <> ERROR_SUCCESS then
begin
LocalFree(pac);
LocalFree(sd);
Exit;
end;
LocalFree(pac);
LocalFree(sd);
Result := True;
end;
function ProtectProcess: DWORD;
const
ACL_REVISION = $00000002;
var
hProcess: THandle;
pEmptyDacl: TACL;
dwErr: DWORD;
begin
hProcess := GetCurrentProcess();
// pEmptyDacl := AllocMem(sizeof(ACL)); // Delphi's equivalent of malloc
if not InitializeAcl(pEmptyDacl, sizeof(ACL), ACL_REVISION) then
begin
dwErr := GetLastError();
end
else
begin
dwErr := SetSecurityInfo(hProcess, SE_KERNEL_OBJECT,
DACL_SECURITY_INFORMATION, nil, nil, @pEmptyDacl, nil);
end;
// FreeMem(pEmptyDacl); // Delphi's equivalent of free
Result := dwErr;
end;
procedure TDlgFndWndMain.Button3Click(Sender: TObject);
begin
ShowMessage(IntToStr(ProtectProcess));
// if DenyAllAccessToFile('C:\Users\kku\Desktop\TEST\test2.log') then
// ShowMessage('<27><><EFBFBD><EFBFBD>')
// else
// ShowMessage('<27><><EFBFBD><EFBFBD>');
end;
function TDlgFndWndMain.IsAbleAddWindow(hWindow: HWND): Boolean;
var
sProcName,
sClassName: String;
hTemp: HWND;
begin
Result := false;
sProcName := UpperCase(GetProcessNameByPid(GetProcessPIDFromWndHandle(hWindow)));
// if (WndPages_.GetWndPage(hWindow) <> nil) or
// (sProcName = sSelfProcName_) then exit;
if sProcName = 'EXPLORER.EXE' then
begin
sClassName := UpperCase(GetWndClassName(hWindow));
if sClassName <> 'CABINETWCLASS' then
exit;
hTemp := FindWindowEx(0, 0, 'Progman', 'Program Manager');
if hTemp = 0 then
exit;
// if GetProcessPIDFromWndHandle(hTemp) = GetProcessPIDFromWndHandle(hWindow) then
// exit;
end;
Result := true;
end;
procedure TDlgFndWndMain.toolAddWindowMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
bSeeking_ := true;
SetCapture(toolAddWindow.Handle);
end;
procedure TDlgFndWndMain.toolAddWindowMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
pt: TPoint;
hTemp,
hTopHWND: HWND;
WndRect: TRect;
sClassName,
sProcName: String;
begin
if bSeeking_ then
begin
pt.X := X;
pt.Y := Y;
pt := ClientToScreen(pt);
// OutputDebugString(PChar(Format('X=%d, Y=%d', [pt.X, pt.Y])));
hTopHWND := WindowFromPoint(pt); // GetTopParentHWND(WindowFromPoint(pt));
if hTopHWND = 0 then
begin
hPreTopHWND_ := 0;
exit;
end;
if (hTopHWND <> Handle) and (hTopHWND <> 0) and (hPreTopHWND_ <> hTopHWND) then
begin
hPreTopHWND_ := 0;
DlgAngle_.Hide;
if not IsAbleAddWindow(hTopHWND) then
exit;
hPreTopHWND_ := hTopHWND;
GetWindowRect(hPreTopHWND_, WndRect);
DlgAngle_.ShowAngle(WndRect);
OutputDebugString(PChar(GetWindowCaption(hTopHWND)));
end;
end;
end;
procedure TDlgFndWndMain.toolAddWindowMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
DlgAngle_.Hide;
ReleaseCapture;
bSeeking_ := false;
try
if (hPreTopHWND_ <> 0) and (hPreTopHWND_ <> Handle) then
begin
ShowMessage(Format('%s, PID=%d, HWND=%d', [GetWindowCaption(hPreTopHWND_), GetProcessPIDFromWndHandle(hPreTopHWND_), hPreTopHWND_]));
// SendMessage(hPreTopHWND_, WM_QUIT, 0, 0);
// SendMessage(hPreTopHWND_, WM_CLOSE, 0, 0);
// ShowWindow(hPreTopHWND_, SW_HIDE);
// EnableWindow(hPreTopHWND_, false);
// DestroyWindow(hPreTopHWND_);
// SetWindowPos(hPreTopHWND_, 0, 0, 0, 1, 1, SWP_NOMOVE or SWP_NOZORDER);
end;
finally
hPreTopHWND_ := 0;
end;
end;
end.