Best method for storing this pointer for use in WndProc

前端 未结 10 822
南旧
南旧 2020-11-29 20:06

I\'m interested to know the best / common way of storing a this pointer for use in the WndProc. I know of several approaches, but each as I underst

10条回答
  •  佛祖请我去吃肉
    2020-11-29 20:43

    You should use GetWindowLongPtr()/SetWindowLongPtr() (or the deprecated GetWindowLong()/SetWindowLong()). They are fast and do exactly what you want to do. The only tricky part is figuring out when to call SetWindowLongPtr() - You need to do this when the first window message is sent, which is WM_NCCREATE.
    See this article for sample code and a more in-depth discussion.

    Thread-local storage is a bad idea, since you may have multiple windows running in one thread.

    A hash map would also work, but computing the hash function for every window message (and there are a LOT) can get expensive.

    I'm not sure how you mean to use thunks; how are you passing around the thunks?

提交回复
热议问题