文章目录
- 1、置顶便笺
- 2、setWindowPos
- 2.1、函数原形
- 2.2、hWndInsertAfter
- 2.3、uFlags
1、置顶便笺
日常办公中想置顶便笺,可能受制于公司电脑权限,无法下载相关软件。下面这段VBA可能会帮上忙(应该没有哪家公司的电脑不让装office),也可以用这种方法置顶其它窗口。
win10中叫便利贴
win7叫便笺
'查找窗口句柄
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'这个函数能为窗口指定一个新位置和状态
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1
Sub setWindowTop()
hwnd = FindWindow("Sticky_Notes_Note_Window", "便利贴")
SetWindowPos hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub
2、setWindowPos
2.1、函数原形
BOOL SetWindowPos(
HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
2.2、hWndInsertAfter
Value |
Meaning |
HWND_BOTTOM |
Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows. |
HWND_NOTOPMOST |
Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window. |
HWND_TOP |
Places the window at the top of the Z order. |
HWND_TOPMOST |
Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated. |
#define HWND_TOP ((HWND)0)
#define HWND_BOTTOM ((HWND)1)
#define HWND_TOPMOST ((HWND)-1)
#define HWND_NOTOPMOST ((HWND)-2)
2.3、uFlags
Value |
Meaning |
SWP_ASYNCWINDOWPOS |
If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request. |
SWP_DEFERERASE |
Prevents generation of the WM_SYNCPAINT message. |
SWP_DRAWFRAME |
Draws a frame (defined in the window’s class description) around the window. |
SWP_FRAMECHANGED |
Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the window’s size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window’s size is being changed. |
SWP_HIDEWINDOW |
Hides the window. |
SWP_NOACTIVATE |
Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter). |
SWP_NOCOPYBITS |
Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned. |
SWP_NOMOVE |
Retains the current position (ignores the X and Y parameters). |
SWP_NOOWNERZORDER |
Does not change the owner window’s position in the Z order. |
SWP_NOREDRAW |
Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing. |
SWP_NOREPOSITION |
Same as the SWP_NOOWNERZORDER flag. |
SWP_NOSENDCHANGING |
Prevents the window from receiving the WM_WINDOWPOSCHANGING message. |
SWP_NOSIZE |
Retains the current size (ignores the cx and cy parameters). |
SWP_NOZORDER |
Retains the current Z order (ignores the hWndInsertAfter parameter). |
SWP_SHOWWINDOW |
Displays the window. |
#define SWP_NOSIZE 0x0001
#define SWP_NOMOVE 0x0002
#define SWP_NOZORDER 0x0004
#define SWP_NOREDRAW 0x0008
#define SWP_NOACTIVATE 0x0010
#define SWP_FRAMECHANGED 0x0020
#define SWP_SHOWWINDOW 0x0040
#define SWP_HIDEWINDOW 0x0080
#define SWP_NOCOPYBITS 0x0100
#define SWP_NOOWNERZORDER 0x0200
#define SWP_NOSENDCHANGING 0x0400
#define SWP_DRAWFRAME SWP_FRAMECHANGED
#define SWP_NOREPOSITION SWP_NOOWNERZORDER
#if(WINVER >= 0x0400)
#define SWP_DEFERERASE 0x2000
#define SWP_ASYNCWINDOWPOS 0x4000
#endif
#define HWND_TOP ((HWND)0)
#define HWND_BOTTOM ((HWND)1)
#define HWND_TOPMOST ((HWND)-1)
#define HWND_NOTOPMOST ((HWND)-2)
#ifndef NOCTLMGR