全局热键隐藏当前窗口(delphi 改写为汇编)

我用的是MASMPlus的标准WIN EXE模板
在本网站的http://www.kumouse.com/article.asp?id=41可以下载到

代码如下:(delphi)

program open;

uses
Windows;

{$R *.res}

const
WM_HOTKEY = $0312;

var
WinH : Longint=0;
Msg : tMsg;

begin
MessageBox(
GetActiveWindow(),
PChar('热键:ALT+Z 功能:隐藏当前窗口 作者:美丽人生'),
PChar('说明'),
MB_OK);

RegisterHotKey(0,0,MOD_ALT,ord('Z')); //注册Alt+Z
while GetMessage(Msg, 0, 0, 0) do
if Msg.message=WM_HOTKEY then
if WinH=0 then
begin
WinH:=GetForegroundWindow;
ShowWindow(WinH,SW_HIDE);
end
else
begin
ShowWindow(WinH,SW_SHOW);
WinH:=0;
end;
end.

代码如下:(汇编)

.386
.Model Flat, StdCall
Option Casemap :None

Include windows.inc
Include user32.inc
Include kernel32.inc
Include gdi32.inc

includelib gdi32.lib
IncludeLib user32.lib
IncludeLib kernel32.lib
;include macro.asm

WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD
WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

idMyHotKey equ 2008
idMyHotKeyq equ 2009

.const
szFmt_d db '%d', 0

.DATA
szClassName db "hide windows",0
szCaption db "http://www.kumouse.com",0
szt db "热键:ALT+Z 功能:隐藏当前窗口 CTRT+Q 关闭热键 作者:美丽人生",0
szc db "说明",0

.DATA?
hInstance dd ?
hFind HINSTANCE ?

.CODE
START:

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke WinMain,hInstance,NULL,NULL,SW_SHOWDEFAULT
invoke ExitProcess,0

WinMain proc hInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD
LOCAL wc :WNDCLASSEX
LOCAL msg :MSG
local hWnd :HWND

mov wc.cbSize,sizeof WNDCLASSEX
mov wc.style,CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
mov wc.lpfnWndProc,offset WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_BTNFACE+1
mov wc.lpszMenuName,NULL
mov wc.lpszClassName,offset szClassName
;invoke LoadIcon,hInst,100
mov wc.hIcon,NULL
;invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,NULL
mov wc.hIconSm,0
invoke RegisterClassEx, ADDR wc
;invoke CreateWindowEx,NULL,ADDR szClassName,CTXT("http://www.kumouse.com"),WS_OVERLAPPEDWINDOW,200,200,400,200,NULL,NULL,hInst,NULL
invoke CreateWindowEx,NULL,ADDR szClassName,addr szCaption,WS_OVERLAPPEDWINDOW,200,200,400,200,NULL,NULL,hInst,NULL
mov hWnd,eax
;invoke ShowWindow,hWnd,SW_SHOWNORMAL
invoke UpdateWindow,hWnd

StartLoop:
invoke GetMessage,ADDR msg,NULL,0,0
cmp eax, 0
je ExitLoop
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
jmp StartLoop
ExitLoop:

mov eax,msg.wParam
ret
WinMain endp

WndProc proc hWin:DWORD,uMsg:DWORD,wParam :DWORD,lParam :DWORD
local cBuf[32]:BYTE

.if uMsg==WM_CREATE

invoke RegisterHotKey,hWin,idMyHotKey,MOD_ALT,VK_Z ;定义热键
invoke RegisterHotKey,hWin,idMyHotKeyq,MOD_CONTROL,VK_Q
invoke MessageBox,NULL,addr szt,addr szc,1

.elseif uMsg == WM_HOTKEY ;处理热键消息     
    ;invoke wsprintf, ADDR cBuf, ADDR szFmt_d,lParam
    ;invoke MessageBox,NULL,addr cBuf,addr cBuf,1
.if lParam == 5a0001h
  .if hFind == 0
      invoke GetForegroundWindow
      mov hFind,eax
      invoke ShowWindow,hFind,SW_HIDE
    .elseif hFind != 0
      invoke ShowWindow,hFind,SW_SHOW
      mov hFind,0
  pop eax
    .endif
.elseif lParam == 510002h
  invoke SendMessage,hWin,WM_DESTROY,NULL,NULL
.endif

.elseif uMsg == WM_DESTROY

invoke UnregisterHotKey,hWin,idMyHotKey ;取消定义的热键
invoke UnregisterHotKey,hWin,idMyHotKeyq

invoke PostQuitMessage,NULL
.else
invoke DefWindowProc,hWin,uMsg,wParam,lParam
.endif
ret
WndProc endp
END START

原码下载:
点击下载

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注