gpt4 book ai didi

c++ - cpp WinAPI WndProc 包装在类中

转载 作者:行者123 更新时间:2023-11-28 05:56:44 25 4
gpt4 key购买 nike

我想创建一个类来动态实例化窗口,但是由于几个星期无法让它工作,我们将不胜感激任何帮助

由于我无法使用 GetObjectFromHandle 函数检索正确的数据,我尝试使用 std::map 来存储类实例,并且在构造函数中我可以按预期访问 map 中的数据,但是从消息循环中我只能访问垃圾,而 HWND hWnd 是正确的。

这是代码

.h

#ifndef BASE_WINDOW_H
#define BASE_WINDOW_H

#include "GlobalApp.h"
#include <string>
#include <map>

namespace G{
class cWin;

static void SetObjectToHandle( HWND hWnd, LPARAM lParam );
static cWin *GetObjectFromHandle( HWND hWnd );

class cWin{
static LRESULT CALLBACK internal_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
public:
static std::map<HWND, cWin*> hwndMap;
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
cWin();

int registerCls();
HWND createWnd();
HWND hWnd;
};
}

#endif

和.cpp

#include "stdafx.h"
#include "BaseWindow.h"

HWND G::cWin::createWnd(){
HWND hWnd;
hWnd = ::CreateWindowEx(WS_EX_WINDOWEDGE, L"div", NULL,
WS_CHILD | WS_VISIBLE,
50, 50, 50, 50,
G::hWnd, NULL, G::hInst, this );

::UpdateWindow( hWnd );

return hWnd;
}

int G::cWin::registerCls(){
WNDCLASSEX wcex;

if ( !::GetClassInfoEx(G::hInst, L"div", &wcex) ){
wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)this->internal_WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = DLGWINDOWEXTRA;
wcex.hInstance = G::hInst;
wcex.hIcon = LoadIcon(G::hInst, MAKEINTRESOURCE(IDI_WINAPITWO));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+3);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"div";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

if ( ::RegisterClassEx(&wcex) == 0 ){
G::Console( L"wcex_ERR" );
return -1;
}
}
return 0;
}

G::cWin::cWin(){
this->registerCls();

this->hWnd = this->createWnd();
G::Console( L"wndCreated", this->hWnd );

this->hwndMap.insert( std::pair< HWND, G::cWin*>( this->hWnd, this ) );

G::cWin *pWnd = this->hwndMap[ this->hWnd ];
G::Console( L"map:", pWnd->hWnd ); //point to correct data
}

LRESULT G::cWin::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
if ( !this->hwndMap.count( hWnd ) ){
return DefWindowProc(hWnd, message, wParam, lParam);
}
G::cWin *pWnd = this->hwndMap[ hWnd ];

switch (message){
case WM_LBUTTONDOWN:
G::Console( L"ButtonDown", pWnd->hWnd ); // not correct, why?
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

std::map<HWND, G::cWin*> G::cWin::hwndMap;


LRESULT CALLBACK G::cWin::internal_WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
if( uMsg == WM_NCCREATE ){
G::SetObjectToHandle( hWnd, lParam );
}

G::cWin *pWnd = G::GetObjectFromHandle( hWnd );

if( pWnd ){
return pWnd->WindowProc( hWnd, uMsg, wParam, lParam );
} else
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

void G::SetObjectToHandle( HWND hWnd, LPARAM lParam ){
LPCREATESTRUCT cs = reinterpret_cast<LPCREATESTRUCT>( lParam );
G::cWin *pWnd = reinterpret_cast<G::cWin*>( cs->lpCreateParams );

SetLastError( 0 );

if( !SetWindowLongPtr( hWnd, GWL_USERDATA, reinterpret_cast<LONG_PTR>( pWnd ) ) && GetLastError() ){
G::Console( L"Error" );
}
}

G::cWin *G::GetObjectFromHandle( HWND hWnd ){
return reinterpret_cast<G::cWin*>( GetWindowLongPtr( hWnd, GWL_USERDATA ) );
}

我用的是visual studio 2005

最佳答案

您缺少一个析构函数来销毁您的 HWND 并清除对它的所有引用。 HWND 可以在销毁后重新使用。如果你不从你的 std::map 中移除一个被破坏的 HWND,你最终会得到陈旧的 cWin* 指针。

就此而言,您的 std::map 是不必要的。在访问 std::map 之前,您依靠 GetObjectFromHandle() 返回有效的 cWin* 指针,但您说 GetObjectFromHandle () 一开始就无法正常工作。所以只要去掉 std::map,你不需要它。

尝试更像这样的东西:

.h

#ifndef BASE_WINDOW_H
#define BASE_WINDOW_H

#include "GlobalApp.h"
#include <string>

namespace G
{
class cWin
{
private:
HWND hWnd;
LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);

static LRESULT CALLBACK internal_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

public:
cWin();
~cWin();

HWND getWnd();
int registerCls();
int createWnd();
};
}

#endif

.cpp

#include "stdafx.h"
#include "BaseWindow.h"

G::cWin::cWin()
{
registerCls();
createWnd();
G::Console( L"wndCreated", hWnd );
}

G::cWin::~cWin()
{
if ( hWnd )
{
DestroyWindow( hWnd );
}
}

HWND G::cWin::getWnd()
{
return hWnd;
}

int G::cWin::createWnd()
{
hWnd = ::CreateWindowEx(WS_EX_WINDOWEDGE, L"div", NULL,
WS_CHILD | WS_VISIBLE,
50, 50, 50, 50,
G::hWnd, NULL, G::hInst, this );

if ( !hWnd )
{
G::Console( L"hwnd_ERR" );
return -1;
}

::UpdateWindow( hWnd );

return 0;
}

int G::cWin::registerCls()
{
WNDCLASSEX wcex;

if ( !::GetClassInfoEx(G::hInst, L"div", &wcex) )
{
wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = &internal_WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = DLGWINDOWEXTRA;
wcex.hInstance = G::hInst;
wcex.hIcon = LoadIcon(G::hInst, MAKEINTRESOURCE(IDI_WINAPITWO));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+3);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"div";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

if ( !::RegisterClassEx(&wcex) )
{
G::Console( L"wcex_ERR" );
return -1;
}
}

return 0;
}

LRESULT G::cWin::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
G::Console( L"ButtonDown", hWnd );
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_NCDESTROY:
this->hWnd = NULL;
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;
}

LRESULT CALLBACK G::cWin::internal_WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
G::cWin *pWnd;

if( uMsg == WM_NCCREATE )
{
LPCREATESTRUCT cs = reinterpret_cast<LPCREATESTRUCT>( lParam );
pWnd = reinterpret_cast<G::cWin*>( cs->lpCreateParams );

SetLastError( 0 );
if( !SetWindowLongPtr( hWnd, GWL_USERDATA, reinterpret_cast<LONG_PTR>( pWnd ) ) )
{
if( GetLastError() != 0 )
G::Console( L"Error" );
}
}
else
{
pWnd = reinterpret_cast<G::cWin*>( GetWindowLongPtr( hWnd, GWL_USERDATA ) );
}

if( pWnd )
{
return pWnd->WindowProc( uMsg, wParam, lParam );
}
else
{
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
}

关于c++ - cpp WinAPI WndProc 包装在类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com