Prevent dialog closing...

I have a Dialog (and also DialogEx) that with some logic checking I want to stop from closing if the user presses ESC or clicks the "X" button.
How do I do this in the OnClose (or other) event?
TIA
How do I do this in the OnClose (or other) event?
TIA