Centering a window on windows

If a window reaches a certain dimension on MS Windows, it looks unaesthetic if it is centered without respect to the taskbar. E.g., if the taskbar is at the top, the window should slightly move down, if the taskbar is on the left, it should slightly move to the right and so on. The other examples I found here on the wiki and ::tk::placeWindow (at least up to a certain version) ignore the taskbar. So here is my try:


 proc center_window {w} {
      set tw ${w}_temp_
      catch {destroy $tw}
      toplevel $tw
      wm attributes $tw -alpha 0.0 ; # vermeidet flickern
      wm state $tw zoomed
      update idletasks
      foreach {areaW areaH offsX offsY} [split [winfo geometry $tw] x+] {break}
      destroy $tw
      # foreach {winW winH d d} [split [winfo geometry $w] x+] {break}; # turned out to be problematic
      set winW [winfo reqwidth $w]; set winH [winfo reqheight $w]
      set x [expr { (($areaW - $winW) / 2) + $offsX }]
      set y [expr { (($areaH - $winH) / 2) + $offsY }]
      wm geom $w +$x+$y
      wm deiconify $w
      update idletasks
 }

# A simple test

 wm withdraw .
 wm geometry . 1200x900
 # tk_messageBox -message "[winfo width .]x[winfo height .]\n[winfo reqwidth .]x[winfo reqheight .]" -title debug
 # update
 # tk_messageBox -message "[winfo width .]x[winfo height .]\n[winfo reqwidth .]x[winfo reqheight .]" -title debug
 # tk_messageBox -message "[winfo geometry .]"
 wm title . "Testtitel"
 center_window .

See: