有关消息队列(100分)

  • 主题发起人 主题发起人 星云群
  • 开始时间 开始时间

星云群

Unregistered / Unconfirmed
GUEST, unregistred user!
我编过一个变换分辨路liu的程序, 但是必须重起才能实现,怎样通过有关消息的函数直接通知系统而不用重起
 
  Windows 95提供一个API函数EnumDisplaySettings()来获取所有显示模式的有关参数,包括颜色数、分辨率等等,下面的示例代码首先用该函数正确地填充TDevMode类型变量的各字段,然后指定新的数据,再调用ChangeDisplaySettings()函数来改变显示模式:

procedure NewScreenSize(W,H: Integer);
var
DevMode: TDevMode;
begin
if EnumDisplaySettings(nil,0,DevMode) then begin
with DevMode do begin
dmPelsWidth := W;
dmPelsHeight := H;
dmFields := DM_PELSWIDTH or DM_PELSHEIGHT;

end;
ChangeDisplaySettings(DevMode, 0);
end;
end;
///////////////////////////
Windows提供给我们两个API函数,可以动态调整显示器的分辨率,他们是EnumDisplaySettings() 和ChangeDisplaySettings(),下面这个例子就是了

function DynamicResolution(X, Y: word): BOOL;
var
lpDevMode: TDeviceMode;
begin
Result := EnumDisplaySettings(nil, 0, lpDevMode);
if Result then
begin
lpDevMode.dmFields := DM_PELSWIDTH Or DM_PELSHEIGHT;
lpDevMode.dmPelsWidth := X;
lpDevMode.dmPelsHeight := Y;
Result := ChangeDisplaySettings(lpDevMode, 0) = DISP_CHANGE_SUCCESSFUL;
end
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if DynamicResolution(640, 480) then

ShowMessage('Now is 640*480');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if DynamicResolution(800, 600) then
ShowMessage('Now is 800*600');
end;
/////////////////////////////////////
{Delphi 引入的定义是错误的,必须手工引入}
function ChangeDisplaySettings(lpDevMode: PDeviceMode;
dwFlags: DWORD): Longint; stdcall;

var
Form1: TForm1;
DevModeArray: TList; // 显示模式信息结构列表

implementation

uses Math;
{$R *.DFM}

{引入函数}
function ChangeDisplaySettings; external user32 name 'ChangeDisplaySettingsA';

procedure TForm1.FormCreate(Sender: TObject);
var
DevModeCount: Integer; // tracks the number of display modes
DevModeInfo: ^TDevMode; // a pointer to display mode information
begin
DevModeArray := TList.Create;

DevModeCount := 0;

GetMem(DevModeInfo, SizeOf(TDevMode));

{枚举显示模式}
while EnumDisplaySettings(NIL, DevModeCount, DevModeInfo^) do
begin
DevModeArray.Add(DevModeInfo);

Inc(DevModeCount);

ListBox1.Items.Add(IntToStr(DevModeInfo^.dmPelsWidth)+'x'+
IntToStr(DevModeInfo^.dmPelsHeight)+', '+
IntToStr(Trunc(IntPower(2, DevModeInfo^.dmBitsPerPel)))+
' colors');

GetMem(DevModeInfo, SizeOf(TDevMode));
end;

FreeMem(DevModeInfo, SizeOf(TDevMode));

ListBox1.ItemIndex := 0;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
iCount: Integer;
begin
for iCount := 0 to DevModeArray.Count-1 do
FreeMem(DevModeArray.Items[iCount], SizeOf(TDevMode));
DevModeArray.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ModeChange: Longint; //表明是否需要重起
begin
{change the display mode}
ModeChange:=ChangeDisplaySettings(DevModeArray[ListBox1.ItemIndex],
CDS_UPDATEREGISTRY);

{可否动态改变分辨率}
if ModeChange=DISP_CHANGE_SUCCESSFUL then
ShowMessage('动态改变成功');
if ModeChange=DISP_CHANGE_RESTART then
ShowMessage('需要重起');
end;
 
接受答案了.
 
后退
顶部