## 说明:
本案例基于clswindow开发的一个小软件,功能是方便的对目标窗口设置置顶还是取消置顶。软件启动后会在每个窗口的标题栏右端加一个置顶功能的小控制块。通常情况下点击它会使得对应的窗口置顶,如果要取消置顶可以再次点击一下。
## 测试环境:win7x64
>[info]**提示:** 在win10上可能有一些位置遮挡本身的最小化等按钮,后期会调整。
## 部分代码摘录:
**说明:** 以下是部分代码摘录,您会看到软件大量使用了SetTop等方法。完整代码可以从底部github获得,里面有直接的编译好的exe可以直接使用。
```
Private Sub addControlBox()
Dim w As New clsWindow, s$, v, i%
w.GetWindowByTitleEx ".+?", 0, s, True, , DisplayedWindow
v = Split(s, " ")
For i = 0 To UBound(v)
If v(i) <> Me.Hwnd Then
If Not isHasAddControlBox(v(i)) Then
w.Hwnd = v(i)
If InStr("|SetWindowTop|Program Manager|", "|" & w.Caption & "|") = 0 Then
lngHandleHwnd = w.Hwnd
Call createControlWindow
End If
End If
End If
Next
End Sub
.
.
.
Private Sub setTagPos()
isClickHandle = True
wTag.SetTop Not wTag.IsTopmost
Set Image1.Picture = IIf(wTag.IsTopmost, imgAfter.Picture, imgBefore.Picture)
If wTag.IsTopmost Then
wTag.SetTop
wTag.Focus
wMe.SetTop
End If
wTag.Focus
End Sub
Private Sub Timer1_Timer()
If Not wTag.CheckWindow Then '如果窗口不存在就关闭
Unload Me
End If
If wTag.IsForegroundWindow Then '如果当前是活动窗口,那么需要显示并移动控制窗口
lngLeft = (wTag.Left + wTag.Width) * 15 - Me.Width - 60 * 15
lngTop = wTag.Top * 15 + 60
If Me.Left <> lngLeft Or Me.Top <> lngTop Then '位置需要更新时再移动
Me.Move lngLeft, lngTop
End If
Me.Visible = True
wMe.SetTop
ElseIf Not wMe.IsForegroundWindow Then
Me.Visible = False
End If
If wTag.IsTopmost <> isTop Then
isTop = Not isTop
Set Image1.Picture = IIf(isTop, imgAfter.Picture, imgBefore.Picture)
End If
End Sub
```
![](https://img.kancloud.cn/9d/5e/9d5e83909ed743fa6e5a3a8b3a441ef4_324x130.png)
点击下变成置顶,并且图标发生变化:
![](https://img.kancloud.cn/ef/7b/ef7b265772c50584a3e88466d8856165_325x140.png)
如果要退出可以点击右下角托盘,选择菜单“退出”
![](https://img.kancloud.cn/4e/17/4e17b8b080beaa67a67c40037330c0e7_253x121.png)
## 软件及源码下载
**软件下载:** https://github.com/sysdzw/SetWindowTop/blob/master/SetWindowTop.exe
**源码(github):** https://github.com/sysdzw/SetWindowTop
- 关于clsWindow
- 一、获取窗口
- GetWindowByTitle 函数
- GetWindowByTitleEx 函数
- GetWindowByClassName 函数
- GetWindowByClassNameEx 函数
- GetWindowByPoint 函数
- GetWindowByAppName 函数
- GetWindowByAppNameEx 函数
- GetWindowByCursorPos 函数
- GetWindowByPID 函数
- GetWindowByHwnd 函数
- Load 函数(同GetWindowByHwnd)
- 二、操作窗口
- Focus 方法
- SetFocus 方法(同Focus)
- FadeIn 方法
- FadeOut 方法
- Transparent 函数
- SetTransparent 函数(同Transparent)
- MakeTransparent 函数(同Transparent)
- Shake 方法
- CloseWindow 函数
- ClassName 属性
- Caption 属性
- Text 属性(同Caption)
- Title 属性(同Caption)
- Value 属性(同Caption)
- ZOrder 方法
- Left 属性
- Top 属性
- Width 属性
- Height 属性
- Visible 属性
- Enabled 属性
- WindowState 属性
- AlphaBlend 属性
- TransparentColor 属性
- CheckWindow 函数
- Move 函数
- MoveToCenter 函数
- Show 函数
- Hide 函数
- Refresh 方法
- SetPosBottom 函数
- SetPosFront 函数
- SetPosNormal 函数
- SetTop 函数
- IsTopMost 函数
- IsForegroundWindow 属性
- Maximized 函数
- Minimized 函数
- Normal 函数
- Restore 函数(同Normal)
- 三、操作控件
- GetElementHwndByText 函数
- GetElementHwndByClassName 函数
- GetElementTextByText 函数
- GetElementTextByClassName 函数
- GetClassNameByHwnd 函数
- GetTextByHwnd 函数
- GetCaptionByHwnd 函数(同GetTextByHwnd)
- SetElementTextByText 函数
- SetElementTextByClassName 函数
- SelectComboBoxIndex 函数
- AppendElementTextByClassName 函数
- Click 函数
- ClickElementByText 函数
- ClickElementByClassName 函数
- SendKeys 方法
- Paste 方法
- FileToClipboard 函数
- ClipboardSetText 函数
- 四、鼠标坐标相关
- ClickCurrentPoint 方法
- ClickPoint 方法
- ClickPointBackground 方法
- SetCursor 方法
- MoveCursor 方法(同SetCursor)
- MoveCursorTo 方法(同SetCursor)
- SetPoint 方法(同SetCursor)
- GetCursorPoint 函数
- GetCursorPosCurrent 函数(同GetCursorPoint)
- MouseLeftDown 方法
- MouseLeftUp 方法
- MouseRightDown 方法
- MouseRightUp 方法
- DragTo 方法
- DragToEx 方法
- 五、应用程序相关
- AppPath 函数
- AppName 函数
- IsWin64 函数
- CommandLine 函数
- AppCommandLine 函数(同CommandLine)
- CloseApp 函数
- ProcessID 函数
- PID 函数(同ProcessID)
- 六、公有方法属性等
- hWnd 公有变量
- DebugMe 公有变量
- Wait 方法
- Version 方法
- 使用范例
- 01 控制微信发送消息
- 02 控制QQ发送消息
- 03 QQ登录器
- 04 花式调用计算器
- 05 用鼠标在画图工具上画圆
- 06 控制记事本输入并保存
- 07 随机移动鼠标防锁屏
- 08 根据ID进入腾讯课堂
- 09 限制程序重复打开
- 10 控制c2p软件将ceb转换为pdf格式
- 11 实战:微便签-窗口特效
- 12 实战:窗口图钉-窗口置顶
- 13 实战:微型按键精灵,录制和播放鼠标动作
- 一些Q&A
- 01 这个框架是免费的吗?
- 02 无句柄的控件元素如何操作?
- 03 目标窗体中有相同类名输入框如何区分填写?
- 04 为什么win10下最大化不生效?
- 05 如何确定绝对或相对坐标?
- 06 相同标题的窗口如何得到所有句柄?
- 联系我们