Let $f:mathcal{X}to S$ be a proper holomorphic submersion of complex manifolds and $G$ a complex reductive linear algebraic group with Lie algebra $mathfrak{g}$. Assume also given a holomorphic principal $G$-bundle $mathcal{P}$ over $mathcal{X}$ which is endowed with a holomorphic connection $ abla$ 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 $mathbb{C}[mathfrak{g}]^Gto bigoplus_{nge 0} H^0(S,,Omega^n_{S,cl}otimes R^nf_*mathbb{C})$, where $Omega^n_{S,cl}$ 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 Goldmans closed holomorphic $2$-form on the base $S$.