My DS does this when he needs to eat - transitions from a happy-go-lucky kid to a really grumpy feral one. Once we get him to eat something (which can be a real challenge) the happy kid comes back.
My response to the sort of behaviour you describe is "you're really grumpy, do you need food? do you need a rest? or do you need to go and dig in the garden?" I don't try to get him to realise that (for example) slamming the computer mouse because "the computer isn't listening" is unacceptable - we talk about that once he's a bit happier.
I think that learning to manage their own disappointments and temper is a huge step that happens around now - all you can do is set the best possible example you can, and also give him some tools (like escaping to the garden, or whatever may work for him) to work it out for himself.