我需要为应该阻止整个窗口直到输入某些值的实用程序创建一个系统模式表单。所以我正在尝试创建桌面和切换。到目前为止,创建一个桌面切换到它,然后返回对我来说很好。
但是,当我尝试从一个新线程中创建一个表单时,该表单没有显示,但应用程序保留在新创建的空白桌面中,因此在我注销之前永远阻塞屏幕。
我是根据这里找到的代码制作的:
http://developex.com/blog/system-modal-back/
// ScreenLocker.h
#pragma once
using namespace System;
using namespace System::Windows::Forms;
namespace Developex
{
public ref class ScreenLocker
{
private:
String ^_desktopName;
Form ^_form;
void DialogThread(void);
public:
static void ShowSystemModalDialog (String ^desktopName, Form ^form);
};
}
// ScreenLocker.cpp
#include "stdafx.h"
#include "ScreenLocker.h"
using namespace System::Threading;
using namespace System::Runtime::InteropServices;
namespace Developex
{
void ScreenLocker::DialogThread()
{
// Save the handle to the current desktop
HDESK hDeskOld = GetThreadDesktop(GetCurrentThreadId());
// Create a new desktop
IntPtr ptr = Marshal::StringToHGlobalUni(_desktopName);
HDESK hDesk = CreateDesktop((LPCWSTR)ptr.ToPointer(),
NULL, NULL, 0, GENERIC_ALL, NULL);
Marshal::FreeHGlobal(ptr);
// Switch to the new deskop
SwitchDesktop(hDesk);
// Assign new desktop to the current thread
SetThreadDesktop(hDesk);
// Run the dialog
Application::Run(_form);
// Switch back to the initial desktop
SwitchDesktop(hDeskOld);
CloseDesktop(hDesk);
}
void ScreenLocker::ShowSystemModalDialog(String ^desktopName, Form ^form)
{
// Create and init ScreenLocker instance
ScreenLocker ^locker = gcnew ScreenLocker();
locker->_desktopName = desktopName;
locker->_form = form;
// Create a new thread for the dialog
(gcnew Thread(gcnew ThreadStart(locker,
&Developex::ScreenLocker::DialogThread)))->Start();
}
}
好吧,现在我正在尝试将其“翻译”为 Delphi,到目前为止,这就是我所拥有的:
unit Utils;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ADODB, Grids, DBGrids, ExtCtrls, ComCtrls, SyncObjs, ShellApi,
AddTimeU;
type
TFormShowThread = class(TThread)
HDesktopglobal: HDESK;
hDeskOld: HDESK;
UHeapSize: ULong;
tempDword: DWORD;
frm : TfrmBlockScreen;
private
protected
procedure Execute; override;
public
constructor Create(form : TfrmBlockScreen);
destructor Destroy; override;
end;
implementation
constructor TFormShowThread.Create(form : TfrmBlockScreen);
begin
FreeOnTerminate := True;
inherited Create(True);
frm := form;
end;
destructor TFormShowThread.Destroy;
begin
inherited;
end;
procedure TFormShowThread.Execute;
begin
hDeskOld := GetThreadDesktop(GetCurrentThreadId());
HDesktopglobal := CreateDesktop('Z', nil, nil, 0, GENERIC_ALL, nil);
SwitchDesktop(HDesktopglobal);
SetThreadDesktop(HDesktopglobal);
// tried this
Application.CreateForm(TfrmBlockScreen, frm);
// also tried this with same result
//frm := TfrmBlockScreen.Create(nil);
//frm.Show();
SwitchDesktop(hDeskOld);
CloseDesktop(HDesktopglobal);
end;
end.
我正在使用以下代码运行它:
var
frmBlockScreen : TfrmBlockScreen;
frmShowThread : TFormShowThread;
begin
frmShowThread := TFormShowThread.Create(frmBlockScreen);
frmShowThread.Priority := tpNormal;
frmShowThread.OnTerminate := ThreadDone;
frmShowThread.Start();
我不明白为什么这不起作用,而 C++ 应该可以工作,它在同一个应用程序中创建了一个新表单。
这就是我结束它的方式:
我将要显示的表单移动到一个新项目并将其编译为 timeup.exe。我使用下面显示的过程创建了一个进程,将桌面作为参数发送,以便我可以将进程分配给该桌面。这样我什至不需要创建一个新线程......到目前为止它正在工作。
这有什么缺陷吗?
var
HDesktopglobal: HDESK;
hDeskOld: HDESK;
sDesktopName : String;
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
try
hDeskOld := GetThreadDesktop(GetCurrentThreadId());
sDesktopName := 'TimeUpDesktop';
HDesktopglobal := CreateDesktop(PWideChar(sDesktopName), nil, nil, 0, GENERIC_ALL, nil);
SwitchDesktop(HDesktopglobal);
SetThreadDesktop(HDesktopglobal);
ExecNewProcess('TimeUp.exe', sDesktopName);
SwitchDesktop(hDeskOld);
CloseDesktop(HDesktopglobal);
finally
SwitchDesktop(hDeskOld);
CloseDesktop(HDesktopglobal);
end;
Application.Run;
end.
procedure ExecNewProcess(ProgramName : String; Desktop : String);
var
StartInfo : TStartupInfo;
ProcInfo : TProcessInformation;
CreateOK : Boolean;
begin
{ fill with known state }
FillChar(StartInfo,SizeOf(TStartupInfo),#0);
FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
StartInfo.cb := SizeOf(TStartupInfo);
StartInfo.lpDesktop := PChar(Desktop);
CreateOK := CreateProcess(PChar(ProgramName),nil, nil, nil,False,
CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
nil, nil, StartInfo, ProcInfo);
{ check to see if successful }
if CreateOK then
//may or may not be needed. Usually wait for child processes
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;