2

我很难为编辑控件设置字体。我用过SendMessage(hwnd, WM_FONT, args),但是好像没什么效果。我添加了EM_SETMODIFY消息,但这也没有效果。这是我一直在使用的代码:

    class EditBox : public Wide::OS::EditBox {
        HWND box;
        std::unique_ptr<std::decay<decltype(*HFONT())>::type, decltype(&DeleteObject)> font; 
        Math::AbsolutePoint curr_pos;
        Math::AbsolutePoint curr_dim;
    public:
        void SetFont(std::shared_ptr<Render::Font> f) {
            font = decltype(this->font)(CreateFontIndirect(&dynamic_cast<Wide::Direct3D9::Font*>(f.get())->GetLogFont()), &DeleteObject);
            SendMessage(box, WM_SETFONT, reinterpret_cast<WPARAM>(font.get()), true);
            SendMessage(box, EM_SETMODIFY, true, 0);
        }
        EditBox(std::shared_ptr<Render::Font> font, HWND owner, Math::AbsolutePoint position, Math::AbsolutePoint dimensions, HINSTANCE hinst) 
        : curr_pos(position), curr_dim(dimensions), font(CreateFontIndirect(&dynamic_cast<Wide::Direct3D9::Font*>(font.get())->GetLogFont()), &DeleteObject){
            box = CreateWindowEx(
                0, 
                L"EDIT", 
                L"Type here", 
                WS_VISIBLE | WS_CHILD | WS_TABSTOP | ES_LEFT,
                position.x,
                position.y,
                dimensions.x,
                dimensions.y,
                owner,
                0,
                hinst,
                0);
            /*SetWindowSubclass(box, [](HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam, UINT_PTR, DWORD_PTR) -> LRESULT {
                if (msg != WM_PAINT)
                    return DefSubclassProc(hwnd, msg, wparam, lparam);
                PAINTSTRUCT paint;
                BeginPaint(hwnd, &paint);

                EndPaint(hwnd, &paint);
                return 0;
            }, 0, 0);*/
            SendMessage(box, WM_SETFONT, reinterpret_cast<WPARAM>(font.get()), true);
            SendMessage(box, EM_SETMODIFY, true, 0);
        }
        ~EditBox() { DestroyWindow(box); }
    };

我检查了我要返回的 LOGFONT 上的值,它们非常合理,但我可以根据要求显示它们。

关于为什么字体没有被改变的任何建议?

4

1 回答 1

3

构造函数中该死的变量阴影。传递的指针实际上是 a Render::Font*,而不是HFONT来自存储的变量。当然,我没有正确测试是SetFont哪个不起作用,还是构造函数不起作用。如果只有 Windows 使用实际函数而不是那些令人讨厌的消息,所以我不必这样做reinterpret_cast,就会有一个很好的编译器错误。

于 2012-11-20T11:13:27.167 回答