无法在编辑控件上设置字体

时间:2012-11-20 00:15:58

标签: c++ winapi

我在设置编辑控件的字体时遇到了困难。我使用过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上的值我回来了,它们非常合理,但我可以根据要求显示它们。

有关字体未被修改的原因的任何建议吗?

1 个答案:

答案 0 :(得分:3)

构造函数中该死的变量阴影。传递的指针实际上是Render::Font*,而不是存储变量的HFONT。当然,我没有正确地测试是SetFont是不起作用,还是构造函数不起作用。如果只有Windows使用实际函数而不是那些icky消息,那么我没有reinterpret_cast,那就有一个很好的编译器错误。