Only Moriarty gained sentience through a glitch, or rather a poorly-thought request to the computer. The Doctor achieved it through time, when his program far exceeded the designed running time and with some coaching from the crew. Vic Fontaine was specifically programmed with knowledge of his status as a hologram, but he doesn't seem any more sentient than your standard holocharacter beyond that.
The holocharacter of Professor Moriarty gained knowledge of the Enterprise and the physical world outside of the holosuite, and some sort of sentience, thanks to a bet that led to a hasty and poorly-thought request to the computer in Elementary, Dear Data (TNG S02E03):
PULASKI: Variations on a theme. Now, now do you see my point? All that he knows is stored in his memory banks. Inspiration, original thought, all the true strength of Holmes is not possible for our friend. I'll give you credit for your vast knowledge, but your circuits would just short out if confronted by a truly original mystery. It's elementary, dear Data.
LAFORGE: Now wait a minute, Doctor. We'll see whose circuits short out.
(They return to where they entered, under the watchful gaze of a man)
LAFORGE: Computer, arch.
PULASKI: Are you sure you want to put yourself through this, Lieutenant? Better wilted laurels than no laurels at all.
LAFORGE: Computer, override previous programme. Okay. A programme that definitely challenges Data.
PULASKI: Now it has to deal with events that he has no previous knowledge of.
LAFORGE: Computer, in the Holmesian style, create a mystery to confound Data with an opponent who has the ability to defeat him.
COMPUTER: Define parameters of programme.
PULASKI: What does that mean?
LAFORGE: Computer wants to know how far to take the game.
PULASKI: You mean it's giving you a chance to limit your risk.
LAFORGE: No, the parameters will be whatever is necessary in order to accomplish the directive. Create an adversary capable of defeating Data.
In order to defeat a sentient android of the 24th century, the computer created a sentient holocharacter with knowledge of the 24th century.
It has to be noted though, that Professor Moriarty still acted well within the parameters set for the character in the Sherlock Holmes novels, and didn't seemed to have exceeded his program in any way. So, whether he fully gained sentience is debatable.
When he is woken up four years later, in Ship in a bottle (TNG S06E12), he ends up trapping Picard and Data in a holodeck recreation of the Enterprise while threatening the real Enterprise with destruction, unless a way was found to take him out of the holodeck. He tricks Picard and Data to try various solutions in the holodeck then relays their ideas to the real crew.
The Doctor gained sentience little by little over time, thanks to coaching by Kes and a few other members of Voyager's crew and the many experiences he lived through while the ship made her way back to the Federation, coupled with the adaptability of his program.
By the second year, he had already developed friendships, hobbies and fallen in love once. However, he was also well over his planned running time, as stated by the holographic recreation of Doctor Lewis Zmmerman, creator of the program, acting as the diagnostic matrix in The Swarm (VOY S03E14):
ZIMMERMAN: Two years? Well there's your problem. This program was developed as a short term supplement to your medical staff, fifteen hundred hours tops.
Vic Fontaine was specifically created by Felix, a friend of Doctor Bashir who specializes in creating holoprogram, to be aware that he is a hologram living in a holosuite, and all that it entails, including knowing that the crew of DS9 are not part of the program.
As stated in His Way (DS9 S06E20):
BASHIR: Come on, I'll introduce you.
KIRA: To a hologram?
ODO: Doctor, is this really necessary?
BASHIR: Trust me, he's no ordinary hologram. Vic. You're terrific. Great.
VIC: Thanks, Doc.
O'BRIEN: Wonderful.
KIRA: Very nice.
VIC: I know what you're thinking. He has pretty sweet pipes for a light bulb.
DAX: Light bulb?
VIC: That's what I am, right? A collection of photons and forcefields. You know, your basic heuristic, fully interactive hologram.
O'BRIEN: He knows he's a hologram?
BASHIR: Felix designed him that way. He thought it gave him the right attitude for the era.
VIC: If you're going to work Vegas in the sixties, you'd better know the score. otherwise, you're going to look like a Clyde.
Vic being sentient is debatable, as he points himself that he is a 'basic heuristic, fully interactive hologram', the only difference with the others is that he has a knowledge of what he really is, instead of fully believing in the simulation.