Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; buttons
- ButtonNew = CatchImage(0,?ButtonNew)
- ButtonDel = CatchImage(2,?ButtonDel)
- ButtonRefresh = CatchImage(3,?ButtonRefresh)
- ButtonHide = CatchImage(4,?ButtonHide)
- DataSection
- ButtonNew: IncludeBinary "buttons\new.ico"
- ButtonDel: IncludeBinary "buttons\del.ico"
- ButtonRefresh: IncludeBinary "buttons\refresh.ico"
- ButtonHide: IncludeBinary "buttons\hide.ico"
- EndDataSection
- a$=Space(999) : GetModuleFileName_(0,@a$,999) : appname$=GetFilePart(a$)
- Global HostsPath.s = GetEnvironmentVariable("WINDIR") + "\system32\drivers\etc\hosts"
- Procedure ParseHostsString(String.s)
- Global IP.s = ""
- Global Host.s = ""
- If Left(String,1) = "#" And Not Left(String,2) = "#$"
- ProcedureReturn #False
- EndIf
- If Len(String) > 8
- For i = 1 To Len(String)
- If Mid(String,i,1) = " " Or Mid(String,i,1) = Chr(9)
- Else
- For k = i To Len(String)
- If Mid(String,k,1) = " " Or Mid(String,k,1) = Chr(9)
- IP = Mid(String,i,k-i+1)
- IP = ReplaceString(IP,Chr(9),"")
- IP = ReplaceString(IP," ","")
- For j = k To Len(String)
- If Mid(String,j,1) = " " Or Mid(String,j,1) = Chr(9)
- Else
- For l = j To Len(String)
- If Mid(String,l,1) = " " Or Mid(String,l,1) = Chr(9) Or l = Len(String)
- Host = Mid(String,j,l-j+1)
- Host = ReplaceString(Host,Chr(9),"")
- Host = ReplaceString(Host," ","")
- ProcedureReturn #True
- Break
- EndIf
- Next
- Break
- EndIf
- Next
- Break
- EndIf
- Next
- Break
- EndIf
- Next
- ProcedureReturn #False
- Else
- ProcedureReturn #False
- EndIf
- EndProcedure
- Procedure LoadHosts()
- ClearGadgetItems(10)
- SetFileAttributes(HostsPath,#PB_FileSystem_Normal)
- If ReadFile(0,HostsPath)
- Global NewList Status.i()
- While Eof(0) = 0
- If ParseHostsString(ReadString(0))
- If Left(IP,2) = "#$"
- AddElement(Status())
- Status() = 0
- AddGadgetItem(10,ListIndex(Status()),Host + Chr(10) + ReplaceString(IP,"#$",""))
- Else
- AddElement(Status())
- Status() = 1
- AddGadgetItem(10,ListIndex(Status()),Host + Chr(10) + IP)
- SetGadgetItemState(10,ListIndex(Status()),#PB_ListIcon_Checked)
- EndIf
- EndIf
- Wend
- CloseFile(0)
- SetFileAttributes(HostsPath,#PB_FileSystem_ReadOnly)
- ProcedureReturn #True
- Else
- ProcedureReturn #False
- EndIf
- EndProcedure
- Procedure SaveHosts()
- SetFileAttributes(HostsPath,#PB_FileSystem_Normal)
- If CreateFile(1,HostsPath)
- ForEach Status()
- If Status() = 1
- WriteStringN(1,GetGadgetItemText(10,ListIndex(Status()),1) + Chr(9) + GetGadgetItemText(10,ListIndex(Status()),0))
- Else
- WriteStringN(1,"#$" + GetGadgetItemText(10,ListIndex(Status()),1) + Chr(9) + GetGadgetItemText(10,ListIndex(Status()),0))
- EndIf
- Next
- CloseFile(1)
- SetFileAttributes(HostsPath,#PB_FileSystem_ReadOnly)
- ProcedureReturn #True
- Else
- ProcedureReturn #False
- EndIf
- EndProcedure
- Procedure AddEditHost()
- If Len(GetGadgetText(21)) > 0 And Len(GetGadgetText(22)) > 7
- ForEach(Status())
- If GetGadgetItemText(10,ListIndex(Status()),0) = GetGadgetText(21)
- RemoveGadgetItem(10,ListIndex(Status()))
- AddGadgetItem(10,ListIndex(Status()),GetGadgetText(21) + Chr(10) + GetGadgetText(22))
- If GetGadgetState(20) = #PB_Checkbox_Checked
- SetGadgetItemState(10,ListIndex(Status()),#PB_ListIcon_Checked)
- Status() = 1
- Else
- Status() = 0
- EndIf
- ProcedureReturn #True
- EndIf
- Next
- LastElement(Status())
- AddElement(Status())
- AddGadgetItem(10,ListIndex(Status()),GetGadgetText(21) + Chr(10) + GetGadgetText(22))
- If GetGadgetState(20) = #PB_Checkbox_Checked
- SetGadgetItemState(10,ListIndex(Status()),#PB_ListIcon_Checked)
- Status() = 1
- Else
- Status() = 0
- EndIf
- ProcedureReturn #True
- EndIf
- ProcedureReturn #False
- EndProcedure
- Macro SetToolBarIconSize(ToolBar, IconSize)
- SendMessage_(ToolBarID(ToolBar), #TB_SETBITMAPSIZE, 0, (IconSize << 16) + IconSize)
- ImageList_SetIconSize_(SendMessage_(ToolBarID(ToolBar), #TB_GETIMAGELIST, 0, 0) , IconSize , IconSize)
- SendMessage_(ToolBarID(ToolBar), #TB_AUTOSIZE, 0, 0)
- EndMacro
- If OpenWindow(0,#PB_Ignore,#PB_Ignore,400,300,"QHosts",#PB_Window_Invisible|#PB_Window_ScreenCentered|#PB_Window_BorderLess)
- Icon = ExtractIcon_(WindowID(0),appname$,0)
- AddSysTrayIcon(100,WindowID(0),Icon)
- SysTrayIconToolTip(100,"QHosts")
- If CreatePopupMenu(0)
- MenuItem(1,"QHosts")
- DisableMenuItem(0,1,1)
- MenuBar()
- MenuItem(2,"Show")
- MenuItem(3,"Exit")
- EndIf
- ButtonImageGadget(1,10,10,34,34,ButtonNew)
- ButtonImageGadget(2,50,10,34,34,ButtonDel)
- ButtonImageGadget(3,90,10,34,34,ButtonRefresh)
- ButtonImageGadget(4,358,10,34,34,ButtonHide)
- CheckBoxGadget(20,16,18,15,18,"")
- StringGadget(21,33,18,181,18,"")
- IPAddressGadget(22,224,18,100,18)
- ButtonGadget(23,334,18,20,18,"+")
- ButtonGadget(24,364,18,20,18,"X")
- HideGadget(20,1) : HideGadget(21,1) : HideGadget(22,1) : HideGadget(23,1) : HideGadget(24,1)
- ListIconGadget(10,10,52,380,235,"Host",250,#PB_ListIcon_MultiSelect|#PB_ListIcon_FullRowSelect|#PB_ListIcon_CheckBoxes|#PB_ListIcon_AlwaysShowSelection)
- AddGadgetColumn(10,1,"IP",105)
- Else
- MessageRequester("Error!","Can't create window... [Code: 11]")
- EndIf
- Repeat
- ev = WaitWindowEvent()
- If (ev = #PB_Event_SysTray And EventType() = #PB_EventType_LeftDoubleClick) Or (ev = #PB_Event_Menu And EventMenu() = 2)
- HideWindow(0,1)
- regn.i$ = CreateRoundRectRgn_(0,0,400,300,20,20)
- SetWindowRgn_(WindowID(0),regn.i$,#True)
- HideWindow(0,0)
- SetActiveWindow(0)
- If LoadHosts() = #False
- MessageRequester("Error!","Can't load `hosts` file, check your permissions... [Code: 12]")
- EndIf
- EndIf
- If ev = #PB_Event_SysTray And EventType() = #PB_EventType_RightClick
- DisplayPopupMenu(0,WindowID(0))
- EndIf
- If ev = #PB_Event_Menu And EventMenu() = 3
- ev = #PB_Event_CloseWindow
- EndIf
- If ev = #WM_LBUTTONDOWN
- SendMessage_(WindowID(0),#WM_NCLBUTTONDOWN,#HTCAPTION,0)
- EndIf
- If EventGadget() = 10 And EventType() = #PB_EventType_LeftDoubleClick
- HideGadget(1,1) : HideGadget(2,1) : HideGadget(3,1) : HideGadget(4,1)
- HideGadget(20,0) : HideGadget(21,0) : HideGadget(22,0) : HideGadget(23,0) : HideGadget(24,0)
- If GetGadgetState(10) > -1
- SetGadgetText(21,GetGadgetItemText(10,GetGadgetState(10),0))
- CurIP.s = GetGadgetItemText(10,GetGadgetState(10),1)
- SetGadgetState(22,MakeIPAddress(Val(StringField(CurIP,1,".")),Val(StringField(CurIP,2,".")),Val(StringField(CurIP,3,".")),Val(StringField(CurIP,4,"."))))
- SelectElement(Status(),GetGadgetState(10))
- If Status() = 1
- SetGadgetState(20,#PB_Checkbox_Checked)
- Else
- SetGadgetState(20,#PB_Checkbox_Unchecked)
- EndIf
- EndIf
- SetActiveGadget(21)
- EndIf
- If EventGadget() = 24 And EventType() = #PB_EventType_LeftClick
- HideGadget(20,1) : HideGadget(21,1) : HideGadget(22,1) : HideGadget(23,1) : HideGadget(24,1)
- HideGadget(1,0) : HideGadget(2,0) : HideGadget(3,0) : HideGadget(4,0)
- SetActiveGadget(10)
- EndIf
- If EventGadget() = 23 And EventType() = #PB_EventType_LeftClick
- AddEditHost()
- If SaveHosts() = #False
- MessageRequester("Error!","Can't save `hosts` file, check your permissions... [Code: 13]")
- EndIf
- HideGadget(20,1) : HideGadget(21,1) : HideGadget(22,1) : HideGadget(23,1) : HideGadget(24,1)
- HideGadget(1,0) : HideGadget(2,0) : HideGadget(3,0) : HideGadget(4,0)
- SetActiveGadget(10)
- EndIf
- If EventGadget() = 1 And EventType() = #PB_EventType_LeftClick
- HideGadget(1,1) : HideGadget(2,1) : HideGadget(3,1) : HideGadget(4,1)
- HideGadget(20,0) : HideGadget(21,0) : HideGadget(22,0) : HideGadget(23,0) : HideGadget(24,0)
- If GetGadgetState(10) > -1
- SetGadgetText(21,GetGadgetItemText(10,GetGadgetState(10),0))
- CurIP.s = GetGadgetItemText(10,GetGadgetState(10),1)
- SetGadgetState(22,MakeIPAddress(Val(StringField(CurIP,1,".")),Val(StringField(CurIP,2,".")),Val(StringField(CurIP,3,".")),Val(StringField(CurIP,4,"."))))
- SelectElement(Status(),GetGadgetState(10))
- If Status() = 1
- SetGadgetState(20,#PB_Checkbox_Checked)
- Else
- SetGadgetState(20,#PB_Checkbox_Unchecked)
- EndIf
- EndIf
- SetActiveGadget(21)
- EndIf
- If EventGadget() = 2 And EventType() = #PB_EventType_LeftClick
- EndIf
- If EventGadget() = 3 And EventType() = #PB_EventType_LeftClick
- FreeList(Status())
- If LoadHosts() = #False
- MessageRequester("Error!","Can't load `hosts` file, check your permissions... [Code: 12]")
- EndIf
- EndIf
- If EventGadget() = 4 And EventType() = #PB_EventType_LeftClick
- FreeList(Status())
- HideWindow(0,1)
- EndIf
- If EventType() = #PB_EventType_LeftClick And EventGadget() = 10
- ForEach Status()
- If (GetGadgetItemState(10,ListIndex(Status())) & #PB_ListIcon_Checked And Status() = 1) Or (Not GetGadgetItemState(10,ListIndex(Status())) & #PB_ListIcon_Checked And Status() = 0)
- Else
- If Status() = 0
- Status() = 1
- Else
- Status() = 0
- EndIf
- If SaveHosts() = #False
- MessageRequester("Error!","Can't save `hosts` file, check your permissions... [Code: 13]")
- EndIf
- EndIf
- Next
- EndIf
- Until ev = #PB_Event_CloseWindow
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement