A square flattop beam is a fundamental shape that is in high demand in various applications, such as ultra-high-power lasers, uniform surface processing and medical engineering. In this experiment, a new and simple scheme of the adaptive beam shaping system to generate a square flattop shape with high uniformity and edge steepness using virtual diagonal phase grating encoded on a spatial-light modulator and a 4f system is proposed. The grating vector kg is non-parallel to the normal vectors kx and ky of the objective beam profile to be extracted; thus, the residual and extracted components hit separately on the Fourier plane of the 4f system. Consequently, using a spatial-frequency filter passing components parallel to kx and ky, the residual components are blocked by the filter without loss of the high spatial-frequency domain of the extracted component. When the width of the filter was 1.0 mm, the edge of the shaped beam increased in height within 20 μm, which is less than 20% of that obtained with conventional vertical phase grating.