Let f:X→S be a proper holomorphic submersion of complex manifolds and G a complex reductive linear algebraic group with Lie algebra g. Assume also given a holomorphic principal G-bundle P over X which is endowed with a holomorphic connection ∇ relative to f that is flat (this to be thought of as a holomorphic family of compact complex manifolds endowed with a holomorphic principal G-bundle with flat connection). We show that a refinement of the Chern-Weil homomorphism yields a graded algebra homomorphism C[g]G→⨁n≥0H0(S,ΩS,cln⊗Rnf∗C), where ΩS,cln stands for the sheaf of closed holomorphic n-forms on S. If the fibers of f are compact Riemann surfaces and we take as our invariant the Killing form, then we recover Goldman's closed holomorphic 2-form on the base S.