我在单独的子类文件中有编辑控件。在开始工作 SetWindowSubclass 函数后(我是 C++ 新手,之前我使用 SetWindowLongPtr 进行子类化,它工作得很好,但有人建议我开始使用 SetWindowSubclass),我遇到了这个问题:
编译程序后,应用程序绘制空窗口,该窗口立即停止响应(我必须通过任务管理器关闭它)。输出窗口中的结果错误:
TaskTracklist.exe 中的 0x635F3DEF (msftedit.dll) 处引发异常:0xC0000005:读取位置 0x00000008 时发生访问冲突。
整个代码:
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include <string.h>
#include "SearchEditBox.h"
/*global vars*/
LPCWSTR SearchEditBox::editBoxDefText = L"Search...";
bool SearchEditBox::firstLoad = false;
int SearchEditBox::width = 0;
int SearchEditBox::height = 0;
HWND SearchEditBox::editBox;
/*functions*/
LRESULT CALLBACK EditBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uint_ptr, DWORD dwRefData);
void ChangeEdit(const HWND hwnd, const bool change);
/*set-get functions*/
HWND SearchEditBox::getEditBox()
{
return SearchEditBox::editBox;
}
SearchEditBox …
Run Code Online (Sandbox Code Playgroud) 根据 Herb Sutter 的C++ Coding Standards: 101 Rules, Guidelines, and Best Practices程序员应该避免 c 样式转换:
C 风格的强制转换根据上下文具有不同的(通常是危险的)语义,所有这些都隐藏在单一语法后面。用 C++ 风格的强制转换替换 C 风格的强制转换有助于防止意外错误
我正在尝试传递指向p_ctrl
WinAPI 回调函数的指针,为此我想使用回调函数的 DWORD_PTR 参数(下面的示例正在运行,但包含注释的 C 样式强制转换):
WndCtrls* WndCtrls::Button ( WndCtrls* const p_ctrl, HWND hwnd, RECT const &rc )
{
p_ctrl->ctrl = CreateWindowEx (
0,
L"BUTTON",
p_ctrl->w_classNameButton.c_str (),
WS_VISIBLE | WS_CHILD | BS_OWNERDRAW,
rc.left,
rc.top,
rc.right,
rc.bottom,
hwnd,
0,
(HINSTANCE)GetWindowLongPtr ( hwnd, GWL_HINSTANCE ), // Problematic C-style cast for which I already know workaround
p_ctrl
);
SetWindowSubclass …
Run Code Online (Sandbox Code Playgroud)