A measurement of $CP$-violating observables is performed using the decays $B^pmto D K^pm$ and $B^pmto D pi^pm$, where the $D$ meson is reconstructed in one of the self-conjugate three-body final states $K_{mathrm S}pi^+pi^-$ and $K_{mathrm S}K^+K^-$ (commonly denoted $K_{mathrm S} h^+h^-$). The decays are analysed in bins of the $D$-decay phase space, leading to a measurement that is independent of the modelling of the $D$-decay amplitude. The observables are interpreted in terms of the CKM angle $gamma$. Using a data sample corresponding to an integrated luminosity of $9,text{fb}^{-1}$ collected in proton-proton collisions at centre-of-mass energies of $7$, $8$, and $13,text{TeV}$ with the LHCb experiment, $gamma$ is measured to be $left(68.7^{+5.2}_{-5.1}right)^circ$. The hadronic parameters $r_B^{DK}$, $r_B^{Dpi}$, $delta_B^{DK}$, and $delta_B^{Dpi}$, which are the ratios and strong-phase differences of the suppressed and favoured $B^pm$ decays, are also reported.