📄 h20000419-44.htm
字号:
<html><head><script language="JavaScript"> function reset(){ for ( var i=0; i < document.Form1.elements.length; i++ ) { document.Form1.elements[i].options[0].selected=true }}function going(url){ parent.location=url; reset();}</script><style><!--A:link {text-decoration: none; color: #000000; font-family: 宋体}A:visited {text-decoration: none; color: #000000; font-family: 宋体}A:active {text-decoration: none; font-family: 宋体}A:hover {text-decoration: underline:#ffffff; color: #ff0000}body,table {font-size: 9pt; font-family: 宋体}.H1 {font-size: 9pt ; line-height:9pt; align=center}.ourfont {font-size: 9pt ; line-height:9pt; }.ourfont1 {font-size: 9pt ; line-height:9pt; }--></style><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>磁性”窗口?</title></head><body topmargin="0" leftmargin="0"><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody> <tr> <td><p class="content4"><span class="font"><img border="0" height="3" src="../../soft/pix.gif" width="700"></span></td> </tr></tbody></table></center></div><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody> <tr> <td bgColor="#3399ff" width="658"><p class="ourfont"><span class="font"> </span><a style="text-decoration: none; color: rgb(255,255,255)" href="../os/H00705-2.htm">操作系统</a><font color="#FFFFFF"><span class="font"><span> |</span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../image/H00705-14.htm">图像处理</a><font color="#FFFFFF"><span>|<span class="font"> </span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../autoCAD/H00705-19.htm">辅助设计</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="../data/H00705-18.htm">数 据 库</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="H00706-1.htm">高级编程</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="../netuse/H00705-20.htm">网络应用</a><font color="#FFFFFF"><span><span class="font"> | </span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../webpage/H00705-21.htm">网页制作</a><font color="#FFFFFF"><span><span class="font"> |</span></span></font> <a style="color: rgb(255,255,255); text-decoration: none" href="../technic/H00706-3.htm">技术交流 </a><a style="color: rgb(255,255,255); text-decoration: none" href="http://202.100.13.77/clslyb/default.asp?clsid=jiaoc"><font color="#FFFFFF"><strong>过客留言</strong></font></a></td> <td bgColor="#3399ff" width="32"><p align="center" class="ourfont"><a href="mailto:net@163.sn.cn"><img src="../../soft/email.gif" alt="email.gif (493 字节)" border="0" WIDTH="20" HEIGHT="19"></a></td> </tr></tbody></table></center></div><div align="center"><center><table border="1" cellSpacing="1" width="700" bordercolor="#3399FF"><tbody> <tr> <td align="left" bgColor="#FFFFFF" vAlign="top" width="700" bordercolor="#0000FF" bordercolorlight="#0000FF" bordercolordark="#0000FF"><p class="ourfont"> </p> <p align="center">“磁性”窗口 </p> <p> Winamp的用户都知道,Winamp的播放列表或均衡器在被移动的时候,仿佛会受到一股磁力,每当靠近主窗口时就一下子被“吸附”过去,自动沿边对齐。我想让我的Winamp插件也具备这种奇妙特性,于是琢磨出了一种“磁化”窗口的方法。该法适用于Delphi的各个版本。为了演示这种技术,请随我来制作一个会被Winamp“吸引”的样板程序。<br> 先新建一应用程序项目,把主窗口Form1适当改小些,并将BorderStyle设为bsNone。放一个按钮元件,双击它并在OnClick事件中写“Close;”。待会儿就按它来结束程序。现在切换到代码编辑区,定义几个全局变量。<br> var<br> Form1: TForm1; //“磁性”窗口<br> LastX, LastY: Integer; //记录前一次的坐标<br> WinampRect:TRect; //保存Winamp窗口的矩形区域<br> hwnd_Winamp:HWND; //Winamp窗口的控制句柄<br> 接着编写Form1的OnMouseDown和OnMouseMove事件。<br> procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;<br> Shift: TShiftState; X, Y: Integer);<br> const<br> ClassName=‘Winamp v1.x’; //Winamp主窗口的类名<br> //如果改成ClassName=‘TAppBuilder’,你就会发现连Delphi也有引力啦!<br> begin<br> //记录当前坐标<br> LastX := X;<br> LastY := Y;<br> //查找Winamp<br> hwnd_Winamp := FindWindow(ClassName,nil);<br> if hwnd_Winamp>0 then //找到的话,记录其窗口区域<br> GetWindowRect(hwnd_Winamp, WinampRect);<br> end;<br> procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,<br> Y: Integer);<br> var<br> nLeft,nTop:integer; //记录新位置的临时变量<br> begin<br> //检查鼠标左键是否按下<br> if HiWord(GetAsyncKeyState(VK_LBUTTON)) > 0 then<br> begin<br> //计算新坐标<br> nleft := Left + X - LastX;<br> nTop := Top + Y - LastY;<br> //如果找到Winamp,就修正以上坐标,产生“磁化”效果<br> if hwnd_Winamp>0 then<br> Magnetize(nleft,ntop);<br> //重设窗口位置<br> SetBounds(nLeft,nTop,width,height);<br> end;<br> end;<br> 别急着,看Magnetize()过程,先来了解一下修正坐标的原理。根据对Winamp实现效果的观察,我斗胆给所谓“磁化”下一个简单的定义,就是“在原窗口与目标窗口接近到某种预定程度,通过修正原窗口的坐标,使两窗口处于同一平面且具有公共边的过程”。依此定义,我设计了以下的“磁化”步骤。第一步,判断目标窗口(即Winamp)和我们的Form1在水平及垂直方向上的投影线是否重叠。“某方向投影线有重叠”是“需要进行坐标修正”的必要非充分条件。判断依据是两投影线段最右与最左边界的差减去它们宽度和的值的正负。第二步,判断两窗口对应边界是否靠得足够近了。肯定的话就让它们合拢。<br> 好了,下面便是“神秘”的Magnetize过程了……<br> procedure TForm1.Magnetize(var nl,nt:integer);<br> //内嵌两个比大小的函数<br> function Min(a,b:integer):integer;<br> begin<br> if a>b then result:=b else result:=a;<br> end;<br> function Max(a,b:integer):integer;<br> begin<br> if a end;<br> var<br> H_Overlapped,V_Overlapped:boolean; //记录投影线是否重叠<br> tw,ww,wh:integer; //临时变量<br> const<br> MagneticForce:integer=50; //“磁力”的大小。<br> //准确的说,就是控制窗口边缘至多相距多少像素时需要修正坐标<br> //为了演示,这里用一个比较夸张的数字——50。<br> //一般可以用20左右,那样比较接近Winamp的效果<br> begin<br> //判断水平方向是否有重叠投影<br> ww := WinampRect.Right-WinampRect.Left;<br> tw := Max(WinampRect.Right,nl+Width)-Min(WinampRect.Left,nl);<br> H_Overlapped := tw<=(Width+ww);<br> //再判断垂直方向<br> wh := WinampRect.Bottom-WinampRect.Top;<br> tw := Max(WinampRect.Bottom,nt+Height)-Min(WinampRect.Top,nt);<br> V_Overlapped := tw<=(Height+wh);<br> //足够接近的话就调整坐标<br> if H_Overlapped then<br> begin<br> if Abs(WinampRect.Bottom-nt) <br> else if Abs(nt+Height-WinampRect.Top) <br> end;<br> if V_Overlapped then<br> begin<br> if Abs(WinampRect.Right-nl) <br> else if Abs(nl+Width-WinampRect.Left) <br> end;<br> end;<br> 怎么样?运行后效果不错吧!<br> 我设计的一个卡拉OK插件就应用了这种技术,你可以到http://www.lotof.com/lyrics去下载它作为参考。</td> </tr></tbody></table></center></div><hr align="center" SIZE="1" width="700"><p align="center" class="ourfont"><span><span><font color="#000000"> <a style="color: rgb(0,0,0); text-decoration: none" href="../../soft/network/index.htm">互联网络</font><font color="#FFFFFF"> </font></a><font color="#000000">|</span></span><a href="../../soft/system/index.htm" style="color: rgb(0,0,0); text-decoration: none">系统工具</a><span>|<span> <a href="../../soft/tools/index.htm" style="color: rgb(0,0,0); text-decoration: none">实用工具</a> | <a href="../../soft/multimedia/index.htm" style="color: rgb(0,0,0); text-decoration: none">媒体工具</a> | <a href="../../soft/image/index.htm" style="color: rgb(0,0,0); text-decoration: none">图形图像</a> | <a href="../../soft/game/index.htm" style="color: rgb(0,0,0); text-decoration: none">游戏娱乐</a> | </span><a href="../../soft/usesoft/index.htm" style="color: rgb(0,0,0); text-decoration: none">软件<span>使用</a></font><font color="#FFFFFF"> |</font> </span></span></p><hr align="center" SIZE="1" width="700"><font color="#FFE8BB"><p align="center" class="ourfont"></font><font color="#000000"><span><span>© Copyright 2000 </span></span>陕西公用计算机互联网信息中心<span><span> 版权所有 </font></p></span></span><p align="center" class="ourfont"><font color="#000000"><span><span>本站制作维护by <a style="color: rgb(0,0,0)" href="mailto:haiyuan@163.sn.cn">HAIYUAN</a></span></span></font></p><p align="center" class="ourfont"><font color="#000000"><span><span>电话:(029)8371051 8371055 8371049 联系信箱:<a href="mailto:net@163.sn.cn">net@163.sn.cn</a></span></span></font></p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -