⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 040.htm

📁 Delphi书籍--Delphi网上教程
💻 HTM
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->DELPHI专题文档-程序应用-->用Delphi编写DLL实现动态改变分辨率</TITLE>
<META NAME="keywords" CONTENT=" DELPHI专题文档-程序应用 用Delphi编写DLL实现动态改变分辨率">
<META NAME="description" CONTENT=" - DELPHI专题文档-程序应用 - 用Delphi编写DLL实现动态改变分辨率">

<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋体"}
.tt2 {font: 12pt/15pt "宋体"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<a href="index6.html">返回</a>

<body text="#000000" aLink=#9900ff link=#006699 vLink=#006699 bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3">
<TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0>
<TR>

<TD class="tt2" bgcolor="#F5F8F8" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷体_GB2312">用Delphi编写DLL实现动态改变分辨率</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<p><span style="font-size: 9pt"><font color="#ffffff">----</font>
读了《在VB中调用API函数动态改变及恢复屏幕设置》一文,觉得非常实用。的确,因显示器的分辩率的不一致而影响软件界面及人机正常交互的情形太多了。通常,我们的应用可能用VB、Delphi、PB等不同语言实现的,如果在各种语言中都调用API来实现动态的变屏幕设置的话,先不管调用能否成功,光一个DEVMODE结构在不同语言的定义就需要半天。能不能自己做一个DLL,装封几个简单的动态改变屏幕分辨率的函数,以达到不同语言均可调用的目的呢?作者进行了一番探索。Delphi语言封装大部分Windows 
API函数,只要在uses子句后加上 Windows,即可直接调用许多Windows API函数。因此笔者选用Delphi语言。 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>一、 创建一个DLL</b> 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 关于在Delphi中如何创建DLL,已有不少文章介绍过了,读者也可以在Delphi5.0的帮助中查找(有详细例子),在此不再赘述。Setscn.dll中装封了三个函数: 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> setdisplaymode函数实现动态设置屏幕分辩率,参数pwidth、pheight为欲设置分辩率的屏宽和屏高,返回值为longint型,为0表示成功。 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> getscreenwidth()函数的功能是取得当前屏幕的屏宽,返回值为longint型。 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> getscreenwidth()函数的功能是取得当前屏幕的屏高,返回值为longint型。 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 程序源代码如下: 
</span></p> 
<BR> 
<pre><span style="font-size: 9pt">
library  setscn;
uses Windows;
var NewDevMode: TDEVMODE;
function setdisplaymode(pwidth,pheight:integer):
	longint;stdcall;export;
begin
With NewDevMode do
begin
dmSize := 122;
dmFields := DM_PELSWIDTH Or DM_PELSHEIGHT ;
dmPelsWidth := pwidth ;
dmPelsHeight := pheight ;
end;
result:=ChangeDisplaySettings(NewDevMode,0);
end;
Function getscreenwidth():longint;stdcall;export;
begin
result:=GetDeviceCaps(hinstance, HORZRES);
end;
function getscreenheight():longint;stdcall;export;
begin
result:=GetDeviceCaps(hinstance, VERTSIZE);
end;
exports
setdisplaymode index 1,
getscreenwidth index 2,
getscreenheight index 3;
begin
end.
</span></pre> 
<BR> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 
编译以上代码,生成一个名为setscn.dll的动态连接库,将其拷贝到windows的system目录下,便大功告成。 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>二、 为setscn创建引入程序单元</b> 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 为了在Delphi语言中成功调用上述函数,我们需要编写以下引入程序单元。 
</span></p> 
<BR> 
<pre><span style="font-size: 9pt">
{Import unit for setscn.Dll}
unit scnimport;
interface
function setdisplaymode(pwidth,pheight:integer):longint;
function getscreenwidth():longint;
function getscreenheight():longint;
implementation
function setdisplaymode(pwidth,pheight:integer):longint;
	external 'setscn' index 1;
function getscreenwidth():longint;external 'setscn' index 2;
function getscreenheight():longint;external 'setscn' index 3;
end.
</span></pre> 
<BR> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>三、 
不同语言中的声明及调用:</b> </span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 1、 VB中声明及调用: 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> VB中声明: </span></p> 
<BR> 
<pre><span style="font-size: 9pt">
Private Declare Function setdisplaymode Lib
&quot;setscn.dll&quot; (ByVal pwidth As Integer, ByVal
pheight As Integer) As Long
Private Declare Function getscreenwidth Lib
	&quot;setscn.dll&quot; () As Long
Private Declare Function getscreenheight Lib
	 &quot;setscn.dll&quot; () As Long
Dim sh, sw As Long
</span></pre> 
<BR> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 调用示例:点击Command1将屏幕分辩率设为640*480,点击Command2恢复原来设置。 
</span></p> 
<BR> 
<pre><span style="font-size: 9pt">
Private Sub Command1_Click()
sw = getscreenwidth()
sh = getscreenheight()
setdisplaymode 640, 480
End Sub
Private Sub Command2_Click()
setdisplaymode sw, sh
End Sub
</span></pre> 
<BR> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>2、PB中声明及调用:</b> 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> PB中声明: </span></p> 
<BR> 
<pre><span style="font-size: 9pt">
Public Function long setdisplaymode(long pwidth,
	 long pheight)Library &quot; setscn.dll&quot;
Public Function long getscreenwidth()Library
	&quot; setscn.dll&quot;
Public Function long getscreenheight()Library
	 &quot; setscn.dll&quot;
</span></pre> 
<BR> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 调用示例:点击cb_1将屏幕分辩率设为640*480,点击cb_2恢复原来设置。如下: 
</span></p> 
<BR> 
<pre><span style="font-size: 9pt">
//clicked for cb_1 return long
//sw、sh是long型的全程变量
sw=getscreenwidth()
sh=getscreenheight()
setdisplaymode(640,480)
//clicked for cb_2 return long
setdisplaymode(sw,sh)
</span></pre> 
<BR> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>3、Delphi中的声名及调用</b> 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 因为我们为setscn创建引入程序单元scnimport.pas,所以只要在uses子句后添加上scnimport,即可直接调用(源代码略)。 
</span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> <b>四、 结语:</b> </span> 
<p><span style="font-size: 9pt"><font color="#ffffff">----</font> 
软件编程以进入组件年代。组件技术的飞速发展不但提高了代码的重用率,而且对软件的可靠性大有益处。一个真正的Win32程序员不能只局限于某一种语言,如果能利用某一语言之长,做一些组件或动态连接库,以弥补其它语言之短,编程过程中就如虎添翼、左右逢源,编一些高水平的程序也非难事。以上代码在windows98 
环境下,分别用Delphi5.0、VB6.0、PB6.5下调试通过,效果很好。 </span></p> 
<hr color="#EE9B73" size="1" width="94%"> 
 
</TD> 
 
</TR> 
</table> 
</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -