Delphi - 我的代码之窗体移动

技术交流,DH讲解.

这个工具是好早好早以前写的,我这个喜欢在家边看电影边写写代码或者看电子书,所以经常会将网页移到屏幕的左上角或者右上角,而且要置顶.所以就写了这样一个工具,这个工具主要是对句柄的操作,还有就是窗体样式以及几个API的例子,比较基础.

整个文件在 here(Can't Input Chinese:()

下面把代码贴出来,希望有什么不懂的,结合MSDN,OK?

Var
  Form2: TForm2;
  H: Cardinal = 0;

Implementation

{$R *.dfm}

//------------------------------------------------------------------------------
// 取得鼠标所在处窗体的句柄
//------------------------------------------------------------------------------

Procedure TForm2.BtnGetHandleClick(Sender: TObject);
Var
  Pt: TPoint;
Begin
  If GetCursorPos(Pt) Then
    H := WindowFromPoint(Pt)
  Else
    H := 0;
End;

//------------------------------------------------------------------------------
// 将窗体置顶
//------------------------------------------------------------------------------

Procedure TForm2.BtnTopMostClick(Sender: TObject);
Var
  Rc: TRect;
Begin
  If H = 0 Then
    Exit;
  If GetWindowRect(H, Rc) Then
    SetWindowPos(H, HWND_TOPMOST, Rc.Left, Rc.Top, Rc.Right - Rc.Left,
      Rc.Bottom - Rc.Top, SWP_SHOWWINDOW)
End;

//------------------------------------------------------------------------------
// 去掉窗体的边框
//------------------------------------------------------------------------------

Procedure TForm2.BtnNoBorderClick(Sender: TObject);
Var
  WsStyle: Cardinal;
Begin
  If H = 0 Then
    Exit;
  WsStyle := GetWindowLong(H, GWL_STYLE);
  WsStyle := WsStyle And (Not WS_BORDER) And (Not WS_CAPTION);
  SetWindowLong(H, GWL_STYLE, WsStyle);
End;

//------------------------------------------------------------------------------
// 移动窗体
//------------------------------------------------------------------------------

Procedure TForm2.BtnMoveClick(Sender: TObject);
Var
  Dx, Dy: Integer;
  Rc: TRect;
Begin
  If H = 0 Then
    Exit;
  Dx := StrToIntDef(TxtDx.Text, 0);
  Dy := StrToIntDef(TxtDy.Text, 0);
  If GetWindowRect(H, Rc) Then
    MoveWindow(H, Rc.Left + Dx, Rc.Top + Dy, Rc.Right - Rc.Left,
      Rc.Bottom - Rc.Top, True)
End;

代码很简单,have fun.

我是DH,今天就说这么多.

posted @ 2009-12-19 19:34  HuangJacky  阅读(1151)  评论(3编辑  收藏  举报
AdminLogin