The documentation of the renderstates should answer most of your related questions.
For creating the stencilmask, you need the methods
SetRenderState(D3DRS_STENCILZFAIL,D3DSTENCILOP_KEEP)
SetRenderState(D3DRS_STENCILFAIL,D3DSTENCILOP_INCRSAT)
SetRenderState(D3DRS_STENCILPASS,D3DSTENCILOP_INCRSAT)
SetRenderState(D3DRS_STENCILFUNC,D3DCMP_ALWAYS)
because they increment the stencilbuffer, while rendering your circle. Then youre drawing your yellow triangle without using the stencilbuffer. After that youre drawing the red triangle with
SetRenderState(D3DRS_STENCILZFAIL,D3DSTENCILOP_KEEP)
SetRenderState(D3DRS_STENCILFAIL,D3DSTENCILOP_KEEP)
SetRenderState(D3DRS_STENCILPASS,D3DSTENCILOP_KEEP)
SetRenderState(D3DRS_STENCILFUNC,D3DCMP_LESS)
SetRenderState(D3DRS_STENCILREF,0)
so your that your stenciltest returns only true, where youre circle had been drawn before (there should be the stencilvalue greater 0). If after that there is nothing drawn properly, you should try to deactivate the Z-Test maybe, the order of your triangles isn't right.
How do we use Stencil operation in DirectX09 shaders effect file (vs_3_0 and ps_3_0) ?
Stencil operations are only used from your main program code. Shaders cannot have any effect on the stenciltests.