Hide external window
Posted: Thu Jan 07, 2010 12:16 pm
hi...
how to hide a window from a external program like notepad for example?
how to hide a window from a external program like notepad for example?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
#PROCESS_ALL_ACCESS_VISTA_WIN7 = $1FFFFF
Prototype.i PFNCreateToolhelp32Snapshot(dwFlags.i, th32ProcessID.i) ;
Prototype.b PFNProcess32First(hSnapshot.i, *lppe.PROCESSENTRY32) ;
Prototype.b PFNProcess32Next(hSnapshot.i, *lppe.PROCESSENTRY32) ;
Procedure GetPidByName(p_name$)
Protected hDLL.i, process_name$
Protected PEntry.PROCESSENTRY32, hTool32.i
Protected pCreateToolhelp32Snapshot.PFNCreateToolhelp32Snapshot
Protected pProcess32First.PFNProcess32First
Protected pProcess32Next.PFNProcess32Next
Protected pid.i
hDLL = OpenLibrary(#PB_Any,"kernel32.dll")
If hDLL
pCreateToolhelp32Snapshot = GetFunction(hDLL,"CreateToolhelp32Snapshot")
pProcess32First = GetFunction(hDLL,"Process32First")
pProcess32Next = GetFunction(hDLL,"Process32Next")
Else
ProcedureReturn 0
EndIf
PEntry\dwSize = SizeOf(PROCESSENTRY32)
hTool32 = pCreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0)
pProcess32First(hTool32, @PEntry)
process_name$ = Space(#MAX_PATH)
CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH)
If UCase(process_name$) = UCase(p_name$)
ProcedureReturn PEntry\th32ProcessID
EndIf
While pProcess32Next(hTool32, @PEntry) > 0
process_name$ = Space(#MAX_PATH)
CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH)
If UCase(process_name$) = UCase(p_name$)
ProcedureReturn PEntry\th32ProcessID
EndIf
Wend
CloseLibrary(hDLL)
ProcedureReturn 0
EndProcedure
Procedure EnumWindowsProc(hWnd,lParam)
GetWindowThreadProcessId_(hWnd,@lpProc)
If lpProc=PeekL(lParam) ; process passed to EnumWindows is process found at this hwnd
PokeL(lParam,hWnd) ; set ptr to hwnd
ProcedureReturn 0
EndIf
ProcedureReturn 1
EndProcedure
Procedure GetProcessId_(hProcess) ; for windows Vista, Windows XP SP1, Windows 7
Protected hThread
Protected Pid
Protected GCPI
GCPI = GetProcAddress_(GetModuleHandle_("Kernel32"),"GetCurrentProcessId");
hThread = CreateRemoteThread_(hProcess,0,0,GCPI,0,0,0)
If Not hThread
ProcedureReturn 0
EndIf
WaitForSingleObject_(hThread,#INFINITE)
GetExitCodeThread_(hThread,@Pid)
CloseHandle_(hThread)
ProcedureReturn Pid
EndProcedure
Procedure GetProcHwnd(lpProc)
Protected pid = GetProcessId_(lpProc)
ptr=pid
Repeat : Until EnumWindows_(@EnumWindowsProc(),@ptr)
If ptr=pid ; if no handle is returned no matching process was found
ProcedureReturn 0
EndIf
; some form of GetWindow_() here for top-level window..
ProcedureReturn ptr
EndProcedure
#WindowWidth = 310
#WindowHeight = 200
hwnd = OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget)
Top = 10
GadgetHeight = 24
ButtonGadget(1, 223,#WindowHeight-36, 80, 24, "Hide")
ButtonGadget(2, 120,#WindowHeight-36, 80, 24, "Show")
ButtonGadget(8, 10, #WindowHeight-36, 80, 24, "Quit")
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 1
Pid.i = GetPidByName("notepad.exe")
hproc.i = OpenProcess_(#PROCESS_ALL_ACCESS,0,Pid)
hwnd = GetProcHwnd(hproc)
ShowWindow_(hwnd,#SW_HIDE)
Case 2
ShowWindow_(hwnd,#SW_SHOW)
Case 8 ; Quit...
EventID = #PB_Event_CloseWindow
EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow
End
thanks but i have compiler error "GetPidByName is not a function, array, macro or link list"RASHAD wrote:mx101 Hi
1- Run Notepad.exe first
2- Run the next prog. and play with show or hide
Thanks to SFSxOI
Code: Select all
#WindowWidth = 310 #WindowHeight = 200 hwnd = OpenWindow(0, 100, 200, #WindowWidth, #WindowHeight, "PureBasic - Gadget Demonstration", #PB_Window_MinimizeGadget) Top = 10 GadgetHeight = 24 ButtonGadget(1, 223,#WindowHeight-36, 80, 24, "Hide") ButtonGadget(2, 120,#WindowHeight-36, 80, 24, "Show") ButtonGadget(8, 10, #WindowHeight-36, 80, 24, "Quit") Repeat EventID = WaitWindowEvent() If EventID = #PB_Event_Gadget Select EventGadget() Case 1 Pid.i = GetPidByName("notepad.exe") hproc.i = OpenProcess_(#PROCESS_ALL_ACCESS,0,Pid) hwnd = GetProcHwnd(hproc) ShowWindow_(hwnd,#SW_HIDE) Case 2 ShowWindow_(hwnd,#SW_SHOW) Case 8 ; Quit... EventID = #PB_Event_CloseWindow EndSelect EndIf Until EventID = #PB_Event_CloseWindow End
nothing to change. Changing is badRASHAD wrote:If you are running 32 bit OS and application just change all .i to .l
RASHAD wrote:Sorry mx101
I posted the wrong code ,I allready updated it in the first post
This code is for 64 bit application
If you are running 32 bit OS and application just change all .i to .l
That is all
You can after successful attempt enhance the code to check for the application ( if it is 32 or 64 bit ) first
ok work on x64RASHAD wrote:Sorry Thomas
You are very fast (give me some time man well you?)
work on x64 not work on x86RASHAD wrote:Sorry Thomas
You are very fast (give me some time man well you?)
Check it now Thomas
Code: Select all
hwndNotepad = FindWindow_("Notepad",0)
Delay(100)
ShowWindow_(hwndNotepad,#SW_HIDE)