My son noticed in Ⅱ Sh'muel chapters 5–6 that, although David is generally called "David", there are times he's called "hamelech" (="the king") or "hamelech David" (="the king David"). (I'm speaking of the narrative, not reported speech.) What determines when he's called which (also in other chapters, like 15–16)?
The father of a friend of mine works with the police. He's in the area of criminology and teaches interviewers how to interpert the answers they receive based on a system he developed. The base of it is - notice variations of words and when they're used, it's the same but has a different meaning to the intervewee.
For example, if I say "John Doe", or "John" or "Johnny" or "my friend" I'm in a different state of feeling towards him at that specific part of the story.
L'havdil, tanach uses a same method. See Malbim on beginning of Megilla regarding "Vashti", "Hamalka Vashti" and "Vashti Hamalka". Achashverosh as well.
Iwould assume something similar for David Hamelech in the sources you mention.
Sefer Magen Shaul explains why in Shmuel2 13:39 it says Dovid HaMelech instead of HaMelech Dovid. He says that at this point Dovid lost the strength of Malchus by losing interest in chasing after AvSholom. Therefore the word HaMelech is placed after his name in this instance.