How does Hamlet behave initially with Rosecrantz and Guildenstern, and how does he change when he realizes that the two were sent for by Claudius and Gertrude?
When Rosencrantz and Guildenstern first arrive, Hamlet seems genuinely glad to see them. He greets them as his "excellent good friends!" and asks them how they are, making lots of sexual jokes and puns that make it seem as though they have a jovial and cordial relationship. Hamlet asks them for any news, but they do not have much to give him. However, once he begins to suspect that they have actually come at the behest of his uncle and stepfather, King Claudius (they try not to answer when he asks them repeatedly what brought them to Elsinore),he becomes importunate. He says,
But let me conjure you, by the rights of our fellowship, by the consonancy of our youth, by the obligation of our ever-preserved love, and by what more dear a better proposer could charge you withal: be even and direct with me whether you were sent for or no. (2.2.277–281)
They answer in the affirmative, saying that they have been sent for, and his demeanor toward them changes. He says that he would not have them reveal the secrets they have with the king and queen, speaking sarcastically, and he tells them that he no longer takes any pleasure in company of men or women. He begins to act sort of "mad" around them, too, indicating his distrust of them.
At first (Act II scene ii) Hamlet is glad to see his old school chums, and starts to bare his soul to them, in the famous speech—“I have of late lost all my mirth…” describing the world as “a foul and pestilent congregation of vapours” but when he ask penetratingly whether they have come out of friendship and they admit they were “sent for” (in other words, were being paid to spy on him), he turns ironic again and keeps his “antic disposition on”. The beautiful metaphor with the musician’s flute (“though you can fret me, you cannot play upon me”) brings out his disdain for their false friendship, another “betrayal” like his uncle’s. Of course, on the boat trip, he manufactures a letter condemning them to death.