The first study is presented of CP violation with an amplitude analysis of the Dalitz plot of $B^0 to D K^+ pi^-$ decays, with $D to K^+ pi^-$, $K^+ K^-$ and $pi^+ pi^-$. The analysis is based on a data sample corresponding to $3.0,{rm fb}^{-1}$ of $pp$ collisions collected with the LHCb detector. No significant CP violation effect is seen, and constraints are placed on the angle $gamma$ of the unitarity triangle formed from elements of the Cabibbo-Kobayashi-Maskawa quark mixing matrix. Hadronic parameters associated with the $B^0 to D K^*(892)^0$ decay are determined for the first time. These measurements can be used to improve the sensitivity to $gamma$ of existing and future studies of the $B^0 to D K^*(892)^0$ decay.