为何动态更改屏幕分辨率没有反应!(100分)

  • 主题发起人 主题发起人 英雄无用武之地
  • 开始时间 开始时间

英雄无用武之地

Unregistered / Unconfirmed
GUEST, unregistred user!
更改屏幕分辨率的方法在本论坛上有很多了,我就是采用本论坛说的方法来做的,结果发现没有反应,屏幕分辨率没有更改,我使用C++ Builder 6源代码如下:
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
if(GetSystemMetrics(SM_CXSCREEN)!=1024&&GetSystemMetrics(SM_CYSCREEN)!=768)
{
if(Application->MessageBox("建议您将屏幕分辨率更改为1024*768,确实要更改吗?","分辨率更改",MB_YESNO+MB_ICONQUESTION)==IDYES)
{
DEVMODE *lpDevMode;
if(EnumDisplaySettings(NULL,0,lpDevMode))
{
lpDevMode->dmFields=DM_PELSWIDTH||DM_PELSHEIGHT;
lpDevMode->dmPelsWidth=1024;
lpDevMode->dmPelsHeight=768;
if(!(ChangeDisplaySettings(lpDevMode,CDS_UPDATEREGISTRY)==DISP_CHANGE_SUCCESSFUL))
{
Application->MessageBox("分辨率更改没有成功,请手动更改!","分辨率更改错误",MB_OK+MB_ICONERROR);
return;
}
}
}
}
}
请指教!
 
调试的时候发现问题首先出现在EnumDisplaySettings(NULL,0,lpDevMode)函数上!
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
OldWidth,OldHeight:Integer;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function DynamicChange(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
DynamicChange(800, 600);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
DynamicChange(1024, 768);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
OldWidth:=Screen.Width;
OldHeight:=Screen.Height;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DynamicChange(OldWidth, OldHeight);
end;

end.
 
楼上已答。顶
 
我的代码好像和znj_326的没有什么区别啊,为什么我的就不行呢?
 
这个不会改变刷新频率
function EnumDisplaySettingsM(lpszDeviceName: PChar; iModeNum: Integer;
var lpDevMode: TDeviceMode): Integer; stdcall; external 'User32.DLL' name 'EnumDisplaySettingsA';

procedure ResizeScreen; //更改
var
CurrentScreenMode: TDeviceMode;
bNoFound,bEnd : boolean;
iNum : Integer;
begin
//保存当前屏幕分辨率
OldScreenMode.dmFields:= DM_PELSWIDTH + DM_PELSHEIGHT
+ DM_DISPLAYFREQUENCY;
OldScreenMode.dmSize := SizeOf(OldScreenMode);
EnumDisplaySettingsM(nil,-1,OldScreenMode);

//修改当前屏幕分辨率为800*600
bNoFound := True;
bEnd := False;
iNum := 0;
CurrentScreenMode.dmFields:=DM_PELSWIDTH + DM_PELSHEIGHT;
OldScreenMode.dmSize := SizeOf(CurrentScreenMode);
while bNoFound And (Not bEnd) do
begin
bEnd := EnumDisplaySettingsM(nil,iNum,CurrentScreenMode)=0;
iNum := iNum + 1;
if (CurrentScreenMode.dmPelsWidth = 800) AND
(CurrentScreenMode.dmPelsHeight = 600) then
bNoFound := False;
end;
CurrentScreenMode.dmDisplayFrequency := OldScreenMode.dmDisplayFrequency;

if (Not bNoFound) then
begin
CurrentScreenMode.dmFields:=DM_PELSWIDTH + DM_PELSHEIGHT +
+ DM_DISPLAYFREQUENCY;
ChangeDisplaySettings(CurrentScreenMode,CDS_RESET);
end
else
begin
ShowMessage('Not support');
end;
end;

procedure RestoreScreen; //恢复
begin
ChangeDisplaySettings(OldScreenMode,CDS_RESET);
end;



procedure TForm1.Button3Click(Sender: TObject);
begin
ResizeScreen;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
RestoreScreen;
end;
 
可是我的代码有什么问题,能帮忙指出来吗?我没看出来!
 
后退
顶部