Well you said what do you do in a situation where something like this happens? And I say, why aren't you learning to deal with it? If you come across this situation a lot, you know wha'ts going on and how you normally react. With that, it enables you the chance to take a look at why you do that or what you could do differently.
You don't have to react that way, but you choose to when you realize that you no longer have a shot at getting this girl. She has a bf which communicates to you that you should back off and leave her alone. This communicates to her that, all you wanted from her was either you were trying to get laid or were putting too much importance on the outcome of the situation.
BUT that's where you have to decide what you want out of these situations. Do you want to be able to hold your composure in case she wasn't being serious, but was trying to get you to react EXACTLY how you did SO that you'd give up and leave her alone? This is important, because one communicates that she does have a bf and you're going to bust on her for assuming that's what you were going for and the other is that you WERE trying to pick her up and so you'll be on your merry way.
A woman will know exactly what it is that you want from her in how you hold yourself, your body and how you are speaking to her. Nervousness, anxiety, slumpy posture, weak voice, scared face, etc. All those things communicate to her that you're worried as you're talking to her, whether it be that you're like that all the time or that you're that way because you're hoping to get a chance with her, both communicate something she doesn't want to be a part of.
That's getting into things one post won't be able to cover, but just remember that you already have the ammo to conquer being that way around a woman when she says or brings up that she has a bf. Normally I say one of two things: "Jeez, I wasn't looking for a date." or "Oh, I guess this should be the part where I'm supposed to be all disappointed and leave?"
You can come up with your own and in fact those weren't the best examples of what to say, but it's about you not being phased or affected by what she's saying. WE give that power to her with OUR beliefs about OURSELVES!! Remember that, it isn't anything to do with what SHE THINKS it's about what YOU think of yourself. Being negative and unsure of yourself will come out whenever you communicate to her or sometimes even guys.
Have you ever seen how the guy that seems to not take himself so seriously, but who respects himself and has ACCEPTED himself and obviously takes care and loves himself, is usually a guy who's energy you want to be around? You can forever not be those things and feel sorry for yourself and have people understand. OR you can do all those things for the better good of yourself, the sooner you do those things the better because people won't care one way or another, but I bet you'd care when you see the good it does.
btw, i speak sometimes in describing guys who may or may not be you. its used as other a learning/reflecting point or as a reference to what you shouldn't be doing, but never a direct representation of the person I'm writing it to.