It involves "capturing" the mouse so the API function is SetCapture. Have a look at this for more information:
Mouse Movement - Win32 apps | Microsoft Docs[
^]
I see you have edited your question. I recommend that you search for the things you "have tried." If capturing the mouse is not what you want then try a global hook for mouse input. There are lots of articles on the topic here and you can type that into the search box as easily as I can.