带天气功能的时钟

带天气功能的时钟

/*
=== Weather gadget  
● top left circle - toggle always on top
● double click on the title to go to the saved position  
● F4 - show GUI , or click on tray icon
● more options in the right click context menu: background color; save position for next launch,
● Accuweather forecast in popup window
● Depending on a country, data is received in Fahrenheit or Celsius degrees. 
So choose units below: unit:="C" or unit:="F". 
Convert Fahrenheit to Celsius with option: F_to_Celsius:=1

● To start not on top of other windows: enable this line #227 ;Gosub, onTop
*/

;==========	
	;weather_url:="https://www.accuweather.com/en/it/rome/213490/current-weather/213490"
	;weather_url:="https://www.accuweather.com/en/pl/krakow/274455/current-weather/274455"
	weather_url:="https://www.accuweather.com/en/us/new-york-ny/10007/current-weather/349727#"
	
;==========

	;unit:="C" ; if data received in Celsius degrees (in a particular country)
	unit:="F" ; if data received in Fahrenheit (in a particular country)
	F_to_Celsius:=0 ; convert fahrenheit to Celsius	if data received in Fahrenheit
	
;==========
	write_ini:=0 ; stores data for 1 month
			
			
#SingleInstance force
#NoEnv
;#NoTrayIcon

Menu, Tray, Add , Weather , weather
Menu, Tray, Add ,
;Menu, Tray, Icon, shell32.dll,14 ; 47 ; ; 95 ;22 
Menu, Tray, Icon, netshell.dll, 86
Menu, Tray, Add , Edit Scite, Edit_Scite
Menu, Tray, Add , Edit Notepad, Edit_Notepad
Menu, Tray, Add, Exit , Exit ; double click tray icon to exit
Menu, Tray, Default, Exit 

Menu, ContextMenu, Add, On Top, OnTop
;Menu, ContextMenu, Icon, On Top, Shell32.dll, 147
Menu, ContextMenu, Icon, On Top, Shell32.dll, 248 ;wmploc.dll, 17
Menu, ContextMenu, Add, Save current position , save_position
Menu, ContextMenu, Icon, Save current position , Shell32.dll,124 ; 268 ;101 124
Menu, ContextMenu, Add,
Menu, Submenu1, Add, Black , set_bgrd_black
Menu, Submenu1, Add, Steel , set_bgrd_steel
Menu, Submenu1, Add, Blue , set_bgrd_blue  
Menu, Submenu1, Add, Green , set_bgrd_green
;Menu, Submenu1, Add, 
;Menu, Submenu1, Add, Funny style: dots, set_bgrd_style_dots
;Menu, Submenu1, Add, Funny style: dots 2, set_bgrd_style_dots2
;Menu, Submenu1, Add, Funny style: bricks, set_bgrd_style_bricks 
;Menu, Submenu1, Add, 
;Menu, Submenu1, Add, Reset background, reset_bgrd
Menu, ContextMenu, Add, Background color, :Submenu1 

Menu, ContextMenu, Add,
Menu, ContextMenu, Add , Open settings file , Open_ini
Menu, ContextMenu, Icon , Open settings file , Shell32.dll, 70
;Menu, ContextMenu, Add , Open log , Open_log
Menu, ContextMenu, Add,
Menu, ContextMenu, Add , Go to website , Go_to_web
;Menu, ContextMenu, Icon , Go to website , C:\Program Files\Internet Explorer\iexplore.exe
Menu, ContextMenu, Icon , Go to website , shell32.dll, 221
Menu, ContextMenu, Add,

Menu, ContextMenu, Add , Forecast  , Show_in_popup
;Menu, ContextMenu, Icon , Forecast , C:\Program Files\Internet Explorer\iexplore.exe
Menu, ContextMenu, Icon , Forecast , shell32.dll, 143 ;243
Menu, ContextMenu, Add,
Menu, ContextMenu, Add , Weather , weather
Menu, ContextMenu, Add ,
Menu, ContextMenu, Add, Restart, Reload
Menu, ContextMenu, Add, Exit, Exit


FileEncoding, UTF-8
SetWorkingDir %A_ScriptDir%


global settings_ini := "Drozd Weather.ini"

/* global log_file := "Temp\Temperatures_log_AHK.txt"
global reg_log_temp := "Temp\reg_Temp_log_AHK.txt"
IfNotExist Temp 
	FileCreateDir, Temp
FileAppend, Temperature log`n  , %reg_log_temp%
 */
 
	bgrd_grad_black:="0xff222222|0xff0F0F0F|33"
	bgrd_grad_steel:="0xff1A2333|0xff2C3B54|33" 
	bgrd_grad_blue:="0xff06213D|0xff133557|33" 
	bgrd_grad_green:="0xff00230A|0xff044016|33"  
	
	bgrd_grad:=bgrd_grad_green ;bgrd_grad_black

	bgrd_ramki_black:="0xff353535|0xff1C1C1C"
	bgrd_ramki_steel:="0xff2D3F5D|0xff1A2333"
	bgrd_ramki_blue:="0xff16416B|0xff0D263E" 
	bgrd_ramki_green:="0xff044016|0xff00230A" 
	
	bgrd_ramki:=bgrd_ramki_green ;bgrd_ramki_black
	

If !pToken := Gdip_Startup(){
	MsgBox, 48, No Gdiplus 
	ExitApp
}

	OnExit, Exit

	SysGet, MonitorWorkArea, MonitorWorkArea, 1
	pos_x:=A_ScreenWidth - 138
	pos_y:= MonitorWorkAreaBottom -688
	

	IniRead, read_, %settings_ini%, Weather, weather url	

	if(read_="ERROR" || read_=""){			
			IniWrite, %weather_url%	, %settings_ini%, Weather, weather url
			
	}else{
		weather_url:=read_
	}
	

	IniRead, pos_x_saved, %settings_ini%, window position, x	
	IniRead, pos_y_saved, %settings_ini%, window position, y	
	if(!pos_x_saved || !pos_y_saved || pos_x_saved=="ERROR" || pos_y_saved=="ERROR"){
			IniWrite, %pos_x%	, %settings_ini%, window position, x
			IniWrite, %pos_y%	, %settings_ini%, window position, y
	}else{
		if(pos_x_saved<A_ScreenWidth-120 && pos_y_saved<A_ScreenHeight-130){
			pos_x:=pos_x_saved
			pos_y:=pos_y_saved
		}
	}	


	IniRead, GUI_bgrd, %settings_ini%, Window, GUI_background	
	IniRead, GUI_ramka, %settings_ini%, Window, GUI_ramka	
	
	if(GUI_bgrd!="ERROR" && GUI_bgrd!=""  && GUI_ramka!="ERROR" && GUI_ramka!=""){
				bgrd_grad:=GUI_bgrd
				bgrd_ramki:=GUI_ramka
				
	}else{
		IniWrite, %bgrd_grad%	, %settings_ini%, Window, GUI_background
		IniWrite, %bgrd_ramki%	, %settings_ini%, Window, GUI_ramka
	}


	
Gui,1: +ToolWindow -caption +AlwaysOnTop	+HwndGuiHwnd ;caption
Gui,1:Color, 120F00
Gui,1: -DPIScale
WonTop:=1
; bgrd:
Gui, 1: Add, Picture, x0 y0 w135 h66 vbgrd BackgroundTrans 0xE, 
Gdip_Set_bgrd(bgrd,bgrd_grad)


Gui, 1: Add, Picture, x0 y0 w134 h13 vramkaT 0xE,
Gdip_Set_bgrd(ramkaT,bgrd_ramki_black "|6")

Gui, Add, Picture, x3 y19 w53 h31 vramka 0xE,
Gui, Add, Picture, x60 y19 w72 h15 vramka1 0xE, 
Gui, Add, Picture, x60 y35 w72 h15 vramka2 0xE,
GoSub, ramka
; ramka:

Gui,1: Font, S7 w700 cD0D0D0 , Segoe UI ;
Gui,1: Add, Text , x122 y0 w10 h10 cD0D0D0 gexit BackgroundTrans Center ,  X 

Gui,1: Font, S6 w700 c9C9C9C , Segoe UI
Gui,1: Add, Text , x4 y0  c676767 vonTop_off gonTop BackgroundTrans,  % Chr(9675) ;  ○
Gui,1: Font, S9
Gui,1: Add, Text , x4 y+-13  c676767 vonTop_on gonTop BackgroundTrans, % Chr(9679) ;  ● 
GuiControl, Hide, onTop_off

Gui,1: Font, S7 w700 cE1E1E1 , Segoe UI ; Tahoma 
Gui,1: Add, Text , x18 y0 w100 vtitle gDoubleClick  BackgroundTrans  Center, Accuweather ;Drozd weather ; gGoToSavedPos



Gui,1: Font, S15 w700 cE1E1E1 , Segoe UI
Gui,1: Add, Text , x3 y19  w50  vTem gweather BackgroundTrans Center, -- 
Gui,1: Font, S8 w700 cE1E1E1 ,

Gui,1: Font, S7  w400
Gui,1: Add, Text , x0 y51 w76  vCon  gDoubleClick +HwndConID BackgroundTrans Center, 

Gui,1: Font, S7 w700 cD2D2D2, Arial
Gui,1: Add, Text, x78 y52 w56 vlast_check gDoubleClick +Hwndlast_checkID BackgroundTrans Center,
;GuiControl, Hide, last_check	


Gui,1: Font, S7  w700
Gui,1: Add, Text , x60 y21 w27 cE9C45D vHum Center BackgroundTrans , -- 
Gui,1: Add, Text , x60 y37 w27  cE9C45D vDew Center BackgroundTrans , -- 

Gui,1: Font, S7  w400
Gui,1: Add, Text , x88 y21  cD8C591 w42  BackgroundTrans, Humidity
Gui,1: Add, Text , x88 y37  cD8C591 w42  BackgroundTrans, Dew point   



;Gui,1: Add, Picture, x115 y51 w16 h16   BackgroundTrans Icon249 AltSubmit, shell32.dll ;
;Gui,1: Add, Picture, x112 y128 w16 h16    Icon249 AltSubmit, shell32.dll ;



OnMessage(0x201, "WM_LBUTTONDOWN")   
OnMessage(0x404, "AHK_NOTIFYICON") 
OnMessage(0x200, "WM_MOUSEMOVE")

Gui, 1: Show, Hide x%pos_x% y%pos_y% w134 h66  , Drozd_weather
;DllCall( "AnimateWindow", "Int", GuiHwnd, "Int", 300, "Int", 0x00000010 )
DllCall( "AnimateWindow", "Int", GuiHwnd, "Int", 300, "Int", 0x00000004 )
;Winset, Transparent,200, Drozd_weather  
WinSet, Style, -0xC00000, Drozd_weather ; COMPLETELY remove window border

	;Gosub, onTop

	GoSub, start_weather

	SetTimer,clear_memory, % 6*60*60*1000
return


AHK_NOTIFYICON(wParam, lParam){ ;click tray icon to show
    if (lParam = 0x202) {       ; WM_LBUTTONUP
				Gui,1:Show  				
    }else if (lParam = 0x203){   ; WM_LBUTTONDBLCLK
		}
}


WM_LBUTTONDOWN(){
	if(A_Gui=1){
		PostMessage, 0xA1, 2    ; movable borderless window
		
		ToolTip_("", 0.5)
	}	
}

;===================

WM_MOUSEMOVE(){
		global GuiHwnd, tray_tip, tipOn, ConID, last_checkID
		MouseGetPos,, ,winID,control , 3 ; hwnd
		if(winID==GuiHwnd){
			GuiControl, Show, last_check	
			SetTimer, hide_time, -6000
			
			if(control==ConID && tipOn!=1){	
				tipOn:=1
				;ToolTipFont("s11 w700", "Segoe UI")
				ToolTipFont("s10", "Segoe UI Semibold")
				ToolTipColor("F2EEDC", "900000")
				ToolTip_(tray_tip, 3)
			}else if(control==last_checkID && tipOn!=2){
				tipOn:=2
				ToolTipFont("s9", "Segoe UI")
				ToolTipColor("FFFFFF","333333")
				ToolTip_("` last checked ", 2)
			}
		}
}

;======================================


start_weather:
	GoSub, weather
	SetTimer, weather, % 30*60*1000  ; 0.5h		
			del_last_month()
return

weather:
	Gosub, weather_get
	Gosub, write_
return



weather_get:
	if !InternetConnection(){
		SetTimer, check_again, -60000
		return
	}

	;GuiControlGet,t1,1:, last_check 
	GuiControl,, last_check, 
	GuiControl,, last_check, connecting
	Contents:=DownloadToString(weather_url)
	;MsgBox,,, % Contents
  RegExMatch(Contents,"im)<div id=""detail-now""[\s\S]*<div id=""feature-history"">", today )
	;MsgBox,,, % today
  RegExMatch(today,"im)<li>Dew Point: <strong>(\d+)&#176;&nbsp;F",dew )
	RegExMatch(today,"im)<th scope=""row"">Now</th>\s*<td>(\d+)&deg;</td>",temper )
  RegExMatch(today,"im)<div id=""detail-now"" class=""detail-tab-panel day""[\s\S]*<span class=""cond"">(.*?)</span>",cond )
	RegExMatch(today,"im)<li>Humidity: <strong>(\d+%)</strong>",Humid )
	RegExMatch(today,"im)<div id=""detail-now""[\s\S]*<span class=""cond"">(.*?)</span>",cond )
	RegExMatch(today,"im)<th scope=""row"">Yesterday</th>\s*<td>(\d+)&deg;</td>",yesterday )

	
	tem:=temper1
	if tem is not integer 
	{
		MsgBox,,Drozd Weather, % "Temperature read error.`nWill check again in 1 min." , 3
		SetTimer, check_again, -60000
		return
	}
	dew:=dew1
	yester:=yesterday1
	
	
	if(F_to_Celsius){
		tem:=f_C(tem) Chr(176) "C" ;"°C" 
		dew:=f_C(dew) Chr(176) "C" ;"°C" 
		yester:=f_C(yester) Chr(176) "C" ;"°C" 
	}else{
		tem:=tem Chr(176) unit  ;"°"
		dew:=dew Chr(176) unit   ;"°"
		yester:=yester Chr(176) unit  ;"°"
	}
	
	hum:=Humid1
	last_temp:=tem

	strT:="  Temperature:   " tem "  "
	strD:="  Dew point:       " dew "  "
	strH:="  Humidity:         " Humid1 "  "
	strY:="  Yesterday:        " yester

	tray_tip:="  " cond1 "`n" strT "`n"  strD "`n" strH "`n" strY 
	
	strT_:="  Temperature:   " tem " (" temper1 "F)" "  "
	strD_:="  Dew point:       " dew " (" dew1 "F)" "  "
	strH_:="  Humidity:         " Humid1 "  "
	strY_:="  Yesterday:        " yester " ("	yesterday1 "F)"
	tray_tipC_F:="  " cond1 "`n" strT_ "`n"  strD_ "`n" strH_ "`n" strY_ 
	
	tray_tip:=(F_to_Celsius==1) ? tray_tipC_F : tray_tip
	
	GuiControl,, Tem, %tem%  
	GuiControl,, Hum, %hum%
	GuiControl,, Dew, %dew%  
	GuiControl,, Con, %cond1%

	FormatTime, date, %A_Now%, HH:mm MMMd 
	GuiControl,, last_check, %date%
	SetTimer, hide_time, -3000
return


write_:
	if(write_ini!=1)
		return
	IniRead, today_1, %settings_ini%, %  A_MM " " A_YYYY , % A_MMM A_DD
	IniRead, reg_log_, %settings_ini%, %  A_MM " " A_YYYY , % A_MMM A_DD


	reg_log_:=(reg_log_=="ERROR") ? "" : reg_log_ " | " 
	reg_log:= A_Hour "h " tem
	reg_log:=reg_log_ reg_log
	
	
	if !InStr(reg_log_,A_Hour "h")
	IniWrite, %reg_log%	, %settings_ini%, %  A_MM " " A_YYYY, % A_MMM A_DD
return


hide_time:
	SetTimer, hide_time, Off
	GuiControl, Hide, last_check	
return


check_again:
	Gosub, weather	
return



f_C(t,d:=0){ ;(°F  -  32)  x  5/9 = °C ;fahrenheit_to_celsius
	t:= (t-32)*(5/9)
	t:=Round(t,d)
	return t
}




;======================

del_last_month(){
  global reg_log_temp
	prev_m:= (A_MM!=01) ? dig(A_MM - 1) " " A_YYYY : 12 " " A_YYYY-1 
	IniRead, prev_month , %settings_ini%, % prev_m
  if(prev_month!=""){
		if FileExist(reg_log_temp){
			FileAppend, `n%prev_m%`n  , %reg_log_temp%
			FileAppend, %prev_month%`n  , %reg_log_temp%
		}
    IniDelete,  %settings_ini%, %prev_m% 
  }
	; leftovers 
	Loop % A_MM-1 {
		prev_m:=dig(A_MM-A_Index) " " A_YYYY
		IniRead, prev_month , %settings_ini%, % prev_m
		if(prev_month!=""){
			IniDelete,  %settings_ini%, %prev_m%
		}
	}
	
}


dig(num){ ;to_two_digits
   num:= num<=9 ? "0" . num : num
   return num
}


;============================================


Show_in_popup:
	HTML_page:=DownloadToString(weather_url)
	 
	if(F_to_Celsius){
		Gosub, change_FtoC

		if(RegExMatch(HTML_page,"i)<li>Ceiling: <strong>(\d+) ft<",match)){
			match:=RegExReplace(match,match1 " ft",feet_to_m(match1))
			HTML_page:=RegExReplace(HTML_page,"im)<li>Ceiling: <strong>(\d+) ft<", match)		
		}

		if(RegExMatch(HTML_page,"i)<li>Visibility: <strong>(\d+) mi", match)){
			match:=RegExReplace(match,match1 " mi", mile_to_km(match1))
			HTML_page:=RegExReplace(HTML_page,"im)<li>Visibility: <strong>(\d+) mi", match)
		} 
		
		if(RegExMatch(HTML_page,"i)<li class=""wind""><strong>(\d+) mph<",match)){		 
			match:=RegExReplace(match,match1 " mph",mile_to_km(match1) "/h")
			HTML_page:=RegExReplace(HTML_page,"im)<li class=""wind""><strong>(\d+) mph<", match)
		} 
	}

	
	RegExMatch(HTML_page,"im)<link rel=""stylesheet"" href=""(.+)?"" type=""text/css""",css)
	urlCSS:=css1

;RegExMatch(HTML_page,"im)<!-- /.feed-controls -->\s+(<div id=""feed-tabs"" class=""panel-list"">[\s\S]*Next 5 Days[\s\S]*)<div class=""rise-modules"">",Next_5_Days)

RegExMatch(HTML_page,"ims)<!-- /.feed-controls -->\s+(<div id=""feed-tabs"" class=""panel-list"">.*Next 5 Days.*?)(?=<div class=""panel-body-content"">)",Next_5_Days) 	

HTML_page1 = 
( comments
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  <html>
  <head><meta http-equiv="X-UA-Compatible" content="IE=Edge"></head>
  <style></style>
  <title>Forecast</title>
  </head>
  <body><center> ;<h1> TEST </h1>
)

HTML_page:= HTML_page1 Next_5_Days1  "<a href='" weather_url "'>" weather_url "</a><br>" "</center></body></html>"

;urlCSS:="https://vortex.accuweather.com/adc2010/stylesheets/slate.min-20180522-1001.css"
addCSS:= "<link rel=""stylesheet"" type=""text/css"" href=""" urlCSS """ />"
HTML_page:=RegExReplace(HTML_page,"im)<\/head>", addCSS "</head>")
 
w:=700 , h:=710
;w:=560 , h:=700
Gui,4: Add, ActiveX, vWB w%w% h%h% +Resize +VScroll +HScroll, about:<!DOCTYPE html><meta http-equiv="X-UA-Compatible" content="IE=edge">
  WB.silent := true 
  document := WB.Document
  document.open()
  document.write(HTML_page)
  document.close()
	;WB.Document.Body.Style.Zoom := "80%"
Gui,4: +HWNDWin_id 
;Gui,4: +Owner1  +AlwaysOnTop
Gui,4: Show,% "w" w+20 " h" h+10, IE ActiveX  
match:= document:= HTML_page:=""
return

4GuiClose:
	Gui,4: Destroy
return


change_FtoC:
	HTML_page:=RegExReplace(HTML_page,"im)<\/head>", addCSS "</head>")	 
	HTML_page2:=HTML_page
	RegExMatch(HTML_page2,"im)(\d\d)&deg;(?C1:Replacer)")
	HTML_page:=RegExReplace(HTML_page,"im)&deg;(?:F(?!\)))?","")
	HTML_page2:=""
	
	if(RegExMatch(HTML_page,"i)<li>Dew Point: <strong>(\d+)&#176;&nbsp;F<",match)){
		match:=RegExReplace(match,match1 ,f_C(match1))
		match:=RegExReplace(match,"F<","C<")
		HTML_page:=RegExReplace(HTML_page,"im)<li>Dew Point: <strong>(\d+)&#176;&nbsp;F<", match)
	}
	HTML_page:=RegExReplace(HTML_page,"im)<li>Dew Point: <strong>(\d+)&#176;&nbsp;F<", match)
return


Replacer(match,1,pos){
	global HTML_page
	repl:=f_C(match1) "°" 
	HTML_page:=RegExReplace(HTML_page,"im)" match1,repl,,1,pos)
	return 1
}


mile_to_km(mi){
	return Round(mi*1.60934) " km"
}

feet_to_m(leg){
	return Round(leg* 0.3048) " m"
}

;============================================

;======================
InternetConnection(flag:=0x40) { 
	return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

;======================


DownloadToString(url, encoding="utf-8"){ ;https://autohotkey.com/board/topic/89198-simple-download-bin-tostring-und-tofile/ Bentschi
  static a := "AutoHotkey/" A_AhkVersion
  if (!DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
    return 0
  c := s := 0, o := ""
  if (f := DllCall("wininet\InternetOpenUrl", "ptr", h, "str", url, "ptr", 0, "uint", 0, "uint", 0x80003000, "ptr", 0, "ptr"))
  {
    while (DllCall("wininet\InternetQueryDataAvailable", "ptr", f, "uint*", s, "uint", 0, "ptr", 0) && s>0)
    {
      VarSetCapacity(b, s, 0)
      DllCall("wininet\InternetReadFile", "ptr", f, "ptr", &b, "uint", s, "uint*", r)
      o .= StrGet(&b, r>>(encoding="utf-16"||encoding="cp1200"), encoding)
    }
    DllCall("wininet\InternetCloseHandle", "ptr", f)
  }
  DllCall("wininet\InternetCloseHandle", "ptr", h)
  return o
}

DownloadToFile(url, filename){
  static a := "AutoHotkey/" A_AhkVersion
  if (!(o := FileOpen(filename, "w")) || !DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
    return 0
  c := s := 0
  if (f := DllCall("wininet\InternetOpenUrl", "ptr", h, "str", url, "ptr", 0, "uint", 0, "uint", 0x80003000, "ptr", 0, "ptr"))
  {
    while (DllCall("wininet\InternetQueryDataAvailable", "ptr", f, "uint*", s, "uint", 0, "ptr", 0) && s>0)
    {
      VarSetCapacity(b, s, 0)
      DllCall("wininet\InternetReadFile", "ptr", f, "ptr", &b, "uint", s, "uint*", r)
      c += r
      o.rawWrite(b, r)
    }
    DllCall("wininet\InternetCloseHandle", "ptr", f)
  }
  DllCall("wininet\InternetCloseHandle", "ptr", h)
  o.close()
  return c
}

;=============================
ramka: ;bgrd:
	Gdip_Set_bgrd(ramka,bgrd_ramki "|15",,0)	
	Gdip_Set_bgrd(ramka2,bgrd_ramki "|7",,0)
	Gdip_Set_bgrd(ramka1,bgrd_ramki "|7",,0)
	
return

Gdip_Set_bgrd(ByRef Variable, Background=0x00000000,Hatch=0,border=0){
	GuiControlGet, Pos, Pos, Variable
	GuiControlGet, hwnd, hwnd, Variable  
	
	pBitmap := Gdip_CreateBitmap(Posw, Posh), G := Gdip_GraphicsFromImage(pBitmap), Gdip_SetSmoothingMode(G, 4)	
	
	Background_:=StrSplit(Background,"|")
	if(Background_.Length() >1){
		;=== with gradient =====
		;pBrushFront := Gdip_CreateLineBrushFromRect(0, 0, 1, 10, grad_color1, grad_color2 ,1) 
		grad_color_rim:=Background_[1]
		grad_color_mid:=Background_[2]
		size:=Background_[3]

		if(Hatch=0){
			pBrushFront := Gdip_CreateLineBrushFromRect(0, 0, 1, size, grad_color_rim, grad_color_mid ,1) 
		}else{
			pBrushFront :=Gdip_BrushCreateHatch(grad_color_rim, grad_color_mid, Hatch) ; kropki
		}
		Gdip_FillRectangle(G, pBrushFront,-1, -1,  PosW+1, Posh+1)
	;========
	}else{
		pBrushBack := Gdip_BrushCreateSolid(Background)
		Gdip_FillRectangle(G, pBrushBack, 0, 0, PosW, Posh)
	}

	if(border==1){
		pPen:=Gdip_CreatePen(0xff111111, 1) 
		Gdip_DrawRectangle(G, pPen,0, 0, PosW-1, Posh-1)
		;Gdip_DrawRoundedRectangle(G, pPen,0, 0, PosW-1, Posh-1,4)
	}
	
	hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
	SetImage(hwnd, hBitmap)
	
	Gdip_DeleteBrush(pBrushFront), Gdip_DeleteBrush(pBrushBack)
	Gdip_DeleteGraphics(G), Gdip_DisposeImage(pBitmap), DeleteObject(hBitmap)
	Return, 0
}



;=========================================


set_bgrd_black:
	IniWrite, %bgrd_grad_black%	, %settings_ini%, Window, GUI_background
	IniWrite, %bgrd_ramki_black%	, %settings_ini%, Window, GUI_ramka
return

set_bgrd_steel:
	IniWrite, %bgrd_grad_steel%	, %settings_ini%, Window, GUI_background
	IniWrite, %bgrd_ramki_steel%	, %settings_ini%, Window, GUI_ramka
return

set_bgrd_blue:
	IniWrite, %bgrd_grad_blue%	, %settings_ini%, Window, GUI_background
	IniWrite, %bgrd_ramki_blue%	, %settings_ini%, Window, GUI_ramka
return

set_bgrd_green:
	IniWrite, %bgrd_grad_green%	, %settings_ini%, Window, GUI_background
	IniWrite, %bgrd_ramki_green%	, %settings_ini%, Window, GUI_ramka
return





;=========================================

save_position:
	WinGetPos, x1,y1,,, ahk_id %GuiHwnd%
	IniWrite, %x1%	, %settings_ini%, window position, x
	IniWrite, %y1%	, %settings_ini%, window position, y
return


Open_ini:
Run, %settings_ini%
return

Open_log:
;Run, %log_file%
return

Go_to_web: 
Run, %weather_url%
return

Open_log2:
	if(A_GuiEvent == "DoubleClick")
	Run, %log_file%
return


Open_ini2:
	if(A_GuiEvent == "DoubleClick")
	Run, %settings_ini%
return

;=========================================

onTop:
		WonTop:=!WonTop             
		if WonTop {
			WinSet, AlwaysOnTop, on, Drozd_weather
			GuiControl, Show, onTop_on
			GuiControl, Hide, onTop_off
		}else{
			WinSet, AlwaysOnTop, off, Drozd_weather
			GuiControl, Show, onTop_off
			GuiControl, Hide, onTop_on
		}	
return

GoToSavedPos: ; DoubleClick
	IniRead, pos_x_saved, %settings_ini%, window position, x	
	IniRead, pos_y_saved, %settings_ini%, window position, y	
	if(pos_x_saved<A_ScreenWidth-120 && pos_y_saved<A_ScreenHeight-140)
		WinMove,  Drozd_weather, ,pos_x_saved,pos_y_saved
return


DoubleClick:

return

DisableWindowsFeature(){ ; prevent copy to  clipboard when double clicked ;autohotkey.com/boards/viewtopic.php?t=3569
   Static Dummy1 := OnMessage(0x00A3, "DisableWindowsFeature") ; WM_NCLBUTTONDBLCLK
   Static Dummy2 := OnMessage(0x0203, "DisableWindowsFeature") ; WM_LBUTTONDBLCLK
   If (A_GuiControl) {
      GuiControlGet, HCTRL, Hwnd, %A_GuiControl%
      WinGetClass, Class, ahk_id %HCTRL%
      If (Class = "Static")
				if (A_GuiControl= "Con"){					
					Gosub, Open_ini
				}else if (A_GuiControl= "last_check"){					
					Gosub, Open_log
				}else if (A_GuiControl= "title"){					
					Gosub, GoToSavedPos
				}			 
         Return 0
   }
}



;============================================



ToolTip_(tekst,t:=2,screen:=0){
	if(screen){
		CoordMode, ToolTip,Screen		
		if(screen==1){
			ToolTip, %tekst% ,600, 350
		}else{
			ToolTip, %tekst% ;,600, 350
		}	
	}else{
		CoordMode , ToolTip, Relative

		GuiControlGet, Pos, Pos, edit_1
		tipX:= PosX+ 4, tipY:=PosY +2
		ToolTip, %tekst% ,%tipX%, %tipY%
	}
	t:=t*1000
	Settimer, ToolTip_close , -%t%
}

ToolTip_close:
	Settimer, ToolTip_close , Off
	tipOn:=0
	ToolTip
return


;=========================================

clear_memory:
	EmptyMem(PID)
return


EmptyMem(PID:=0){ ;by heresy https://autohotkey.com/board/topic/30042-run-ahk-scripts-with-less-half-or-even-less-memory-usage/
    PID:=(PID) ? PID : DllCall("GetCurrentProcessId") 
    h:=DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", pid)
    DllCall("SetProcessWorkingSetSize", "UInt", h, "Int", -1, "Int", -1)
    DllCall("CloseHandle", "Int", h)
}

;======================================


;~^g:: Gosub, test 



test:
return


GuiContextMenu:
Menu, ContextMenu, Show, %A_GuiX%, %A_GuiY%
Return


~$F4::
	Gui 1: Show
return

WindowSpy:
Run, "C:\Program Files\AutoHotkey\AU3_Spy.exe" ;"C:\Program Files\AutoHotkey\WindowSpy.ahk"
WinWait, ahk_exe AU3_Spy.exe , , 3
WinMove, Active Window Info, , 1100
return

Reload:
Reload
return

Edit_Notepad:
Run, "C:\Program Files\Misc\Notepad2\Notepad2.exe" "%A_ScriptFullPath%"
return

Edit_Scite:
Run, "C:\Program Files\AutoHotkey\SciTE\SciTE.exe"  "%A_ScriptFullPath%"
return


Close:
;Esc:: 
GuiClose:
Exit:
Gdip_Shutdown(pToken)
DllCall( "AnimateWindow", "Int", GuiHwnd, "Int", 300, "Int", 0x00050008 )
ExitApp

;#Include C:\Program Files\Misc\AutoHotkey Scripts\functions_Gdip.ahk

Gdip_Startup()
{
	Ptr := A_PtrSize ? "UPtr" : "UInt"	
	if !DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
		DllCall("LoadLibrary", "str", "gdiplus")
	VarSetCapacity(si, A_PtrSize = 8 ? 24 : 16, 0), si := Chr(1)
	DllCall("gdiplus\GdiplusStartup", A_PtrSize ? "UPtr*" : "uint*", pToken, Ptr, &si, Ptr, 0)
	return pToken
}

Gdip_Shutdown(pToken)
{
	Ptr := A_PtrSize ? "UPtr" : "UInt"	
	DllCall("gdiplus\GdiplusShutdown", Ptr, pToken)
	if hModule := DllCall("GetModuleHandle", "str", "gdiplus", Ptr)
		DllCall("FreeLibrary", Ptr, hModule)
	return 0
}



SetImage(hwnd, hBitmap)
{
	SendMessage, 0x172, 0x0, hBitmap,, ahk_id %hwnd%
	E := ErrorLevel
	DeleteObject(E)
	return E
}

Gdip_BitmapFromHWND(hwnd)
{
	WinGetPos,,, Width, Height, ahk_id %hwnd%
	hbm := CreateDIBSection(Width, Height), hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
	PrintWindow(hwnd, hdc)
	pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
	SelectObject(hdc, obm), DeleteObject(hbm), DeleteDC(hdc)
	return pBitmap
}

Gdip_CreateHBITMAPFromBitmap(pBitmap, Background=0xffffffff)
{
	DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "uint", pBitmap, "uint*", hbm, "int", Background)
	return hbm
}

CreateCompatibleDC(hdc=0)
{
   return DllCall("CreateCompatibleDC", "uint", hdc)
}

SelectObject(hdc, hgdiobj)
{
   return DllCall("SelectObject", "uint", hdc, "uint", hgdiobj)
}

CreateDIBSection(w, h, hdc="", bpp=32, ByRef ppvBits=0)
{
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	
	hdc2 := hdc ? hdc : GetDC()
	VarSetCapacity(bi, 40, 0)
	
	NumPut(w, bi, 4, "uint")
	, NumPut(h, bi, 8, "uint")
	, NumPut(40, bi, 0, "uint")
	, NumPut(1, bi, 12, "ushort")
	, NumPut(0, bi, 16, "uInt")
	, NumPut(bpp, bi, 14, "ushort")
	
	hbm := DllCall("CreateDIBSection"
					, Ptr, hdc2
					, Ptr, &bi
					, "uint", 0
					, A_PtrSize ? "UPtr*" : "uint*", ppvBits
					, Ptr, 0
					, "uint", 0, Ptr)

	if !hdc
		ReleaseDC(hdc2)
	return hbm
}

Gdip_SetSmoothingMode(pGraphics, SmoothingMode)
{
   return DllCall("gdiplus\GdipSetSmoothingMode", "uint", pGraphics, "int", SmoothingMode)
}





Gdip_CreatePen(ARGB, w)
{
   DllCall("gdiplus\GdipCreatePen1", "int", ARGB, "float", w, "int", 2, "uint*", pPen)
   return pPen
}

Gdip_CloneBrush(pBrush)
{
	DllCall("gdiplus\GdipCloneBrush", "uint", pBrush, "uint*", pBrushClone)
	return pBrushClone
}



Gdip_BrushCreateSolid(ARGB=0xff000000)
{
	DllCall("gdiplus\GdipCreateSolidFill", "int", ARGB, "uint*", pBrush)
	return pBrush
}


Gdip_CreateLineBrushFromRect(x, y, w, h, ARGB1, ARGB2, LinearGradientMode=1, WrapMode=1)
{
	CreateRectF(RectF, x, y, w, h)
	DllCall("gdiplus\GdipCreateLineBrushFromRect", "uint", &RectF, "int", ARGB1, "int", ARGB2, "int", LinearGradientMode, "int", WrapMode, "uint*", LGpBrush)
	return LGpBrush
}




Gdip_DeletePath(Path)
{
	return DllCall("gdiplus\GdipDeletePath", "uint", Path)
}


PrintWindow(hwnd, hdc, Flags=0)
{
	return DllCall("PrintWindow", "uint", hwnd, "uint", hdc, "uint", Flags)
}


Gdip_CreateBitmapFromHBITMAP(hBitmap, Palette=0)
{
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	
	DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", Ptr, hBitmap, Ptr, Palette, A_PtrSize ? "UPtr*" : "uint*", pBitmap)
	return pBitmap
}

Gdip_GetDC(pGraphics)
{
	DllCall("gdiplus\GdipGetDC", "uint", pGraphics, "uint*", hdc)
	return hdc
}

GetDC(hwnd=0)
{
	return DllCall("GetDC", "uint", hwnd)
}

DeleteObject(hObject)
{
   return DllCall("DeleteObject", "uint", hObject)
}

DeleteDC(hdc)
{
   return DllCall("DeleteDC", "uint", hdc)
}

ReleaseDC(hdc, hwnd=0)
{
   return DllCall("ReleaseDC", "uint", hwnd, "uint", hdc)
}



CreateRectF(ByRef RectF, x, y, w, h)
{
   VarSetCapacity(RectF, 16)
   NumPut(x, RectF, 0, "float"), NumPut(y, RectF, 4, "float"), NumPut(w, RectF, 8, "float"), NumPut(h, RectF, 12, "float")
}


Gdip_DrawRectangle(pGraphics, pPen, x, y, w, h)
{
   return DllCall("gdiplus\GdipDrawRectangle", "uint", pGraphics, "uint", pPen, "float", x, "float", y, "float", w, "float", h)
}



Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h)
{
   return DllCall("gdiplus\GdipFillRectangle", "uint", pGraphics, "int", pBrush
   , "float", x, "float", y, "float", w, "float", h)
}

Gdip_GraphicsFromImage(pBitmap)
{
    DllCall("gdiplus\GdipGetImageGraphicsContext", "uint", pBitmap, "uint*", pGraphics)
    return pGraphics
}

Gdip_CreateBitmap(Width, Height, Format=0x26200A)
{
    DllCall("gdiplus\GdipCreateBitmapFromScan0", "int", Width, "int", Height, "int", 0, "int", Format, "uint", 0, "uint*", pBitmap)
    Return pBitmap
}



Gdip_DeletePen(pPen)
{
   return DllCall("gdiplus\GdipDeletePen", "uint", pPen)
}


Gdip_DeleteBrush(pBrush)
{
   return DllCall("gdiplus\GdipDeleteBrush", "uint", pBrush)
}


Gdip_DisposeImage(pBitmap)
{
   return DllCall("gdiplus\GdipDisposeImage", "uint", pBitmap)
}

Gdip_DeleteGraphics(pGraphics)
{
   return DllCall("gdiplus\GdipDeleteGraphics", "uint", pGraphics)
}


Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality=75)
{
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	
	SplitPath, sOutput,,, Extension
	if Extension not in BMP,DIB,RLE,JPG,JPEG,JPE,JFIF,GIF,TIF,TIFF,PNG
		return -1
	Extension := "." Extension

	DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
	VarSetCapacity(ci, nSize)
	DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
	if !(nCount && nSize)
		return -2
	
	If (A_IsUnicode){
		StrGet_Name := "StrGet"
		Loop, %nCount%
		{
			sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
			if !InStr(sString, "*" Extension)
				continue
			
			pCodec := &ci+idx
			break
		}
	} else {
		Loop, %nCount%
		{
			Location := NumGet(ci, 76*(A_Index-1)+44)
			nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int",  0, "uint", 0, "uint", 0)
			VarSetCapacity(sString, nSize)
			DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
			if !InStr(sString, "*" Extension)
				continue
			
			pCodec := &ci+76*(A_Index-1)
			break
		}
	}
	
	if !pCodec
		return -3

	if (Quality != 75)
	{
		Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
		if Extension in .JPG,.JPEG,.JPE,.JFIF
		{
			DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
			VarSetCapacity(EncoderParameters, nSize, 0)
			DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
			Loop, % NumGet(EncoderParameters, "UInt")      ;%
			{
				elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
				if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6)
				{
					p := elem+&EncoderParameters-pad-4
					NumPut(Quality, NumGet(NumPut(4, NumPut(1, p+0)+20, "UInt")), "UInt")
					break
				}
			}      
		}
	}

	if (!A_IsUnicode)
	{
		nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, 0, "int", 0)
		VarSetCapacity(wOutput, nSize*2)
		DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, &wOutput, "int", nSize)
		VarSetCapacity(wOutput, -1)
		if !VarSetCapacity(wOutput)
			return -4
		E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &wOutput, Ptr, pCodec, "uint", p ? p : 0)
	}
	else
		E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &sOutput, Ptr, pCodec, "uint", p ? p : 0)
	return E ? -5 : 0
}


Gdip_BrushCreateHatch(ARGBfront, ARGBback, HatchStyle=0)
{
	DllCall("gdiplus\GdipCreateHatchBrush", "int", HatchStyle, "UInt", ARGBfront, "UInt", ARGBback, A_PtrSize ? "UPtr*" : "UInt*", pBrush)
	return pBrush
} 



;================================
 
ToolTipFont(Options := "", Name := "", hwnd := ""){ ;lexikos https://autohotkey.com/boards/viewtopic.php?t=4777
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := ""){
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" && tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook(){
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0
    if !htext {
        Gui _TTG: Add, Text, +hwndhtext
        Gui _TTG: +hwndhgui +0x40000000
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage 0x31, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage 0x138, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

;==================================


 

给TA捐赠
共{{data.count}}人
人已捐赠
其他

复古风的时钟

2018-9-6 9:03:47

其他

输入法切换

2018-9-8 9:00:49

2 条回复 A文章作者 M管理员
  1. MrLiu

    看起来不错,试试看

  2. ahkjoo

    读取天气失败,这个不能用呢? 论坛上搜索了另外一个天气的json代码也不全,求获取天气的脚本

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索