ProMiNick
Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
|
macro singleinstance _class*,_title*,exitjmp*,noextra {;(maybe ":0" instead "*" more wise for class & title)
invoke FindWindow,_class,_title
or eax,eax
match any,noextra \{ jnz exitjmp \}
match ,noextra \{
\local .init
jz .init
push SW_RESTORE
push eax
call [IsIconic]
sub esp,4
or eax,eax
jz @F
call [ShowWindow]
jmp exitjmp ;"jmp exitjmp" is more preferrable than "sub esp,8", however any of them are valid here
@@:
call [SetForegroundWindow]
jmp exitjmp
.init: \}
}
use case:
; Template for program using standard Win32 headers
format PE GUI 4.0
entry main.start
include 'win32w.inc'
section '.text' code readable executable
main:
.start:
invoke GetModuleHandle,0
mov [hInstance],eax
singleinstance _class,_title,.exit;,noactivating
invoke LoadCursor,0,IDC_ARROW ; Try to remove theese 2 lines
mov [wc.hCursor],eax ; and move cursor slowly from outer to the window, and you understand why they should never be skipped.
invoke RegisterClass,wc
test eax,eax
jz .error
invoke CreateWindowEx,0,_class,_title,WS_VISIBLE+WS_DLGFRAME+WS_SYSMENU+WS_MINIMIZEBOX,128,128,256,192,NULL,NULL,[hInstance],NULL
test eax,eax
jz .error
.msg_loop:
invoke GetMessage,msg,NULL,0,0
cmp eax,1
jb .exit
jne .msg_loop
invoke TranslateMessage,msg
invoke DispatchMessage,msg
jmp .msg_loop
.error:
invoke MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK
.exit:
invoke ExitProcess,[msg.wParam]
proc WindowProc uses ebx esi edi, hwnd,wmsg,wparam,lparam
cmp [wmsg],WM_DESTROY
je .wmdestroy
.defwndproc:
invoke DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
jmp .finish
.wmdestroy:
invoke PostQuitMessage,0
xor eax,eax
.finish:
ret
endp
section '.data' data readable writeable
label hInstance:dword at wc.hInstance ; no needed to create data members twice for same purpose
_class TCHAR 'FASMWIN32',0
_title TCHAR 'Win32 program template',0
_error TCHAR 'Startup failed.',0
wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,_class
msg MSG
section '.idata' import data readable writeable
library kernel32,'KERNEL32.DLL',\
user32,'USER32.DLL'
include 'api\kernel32.inc'
include 'api\user32.inc' so, you can forgot about manual realization of single instancing & just use macro fast & readable
_________________ I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
|