1.主窗口
自定义FramWnd继承WindowImplBase,
重写CreateControl,HandleMessage.
组合CPaintManagerUI.
2.自定义控件
自定义mycontrol 继承CControlUI
重写DoEvent,DoPaint,SetPos.
main.cpp
#include "FramWnd.h" int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow) { CPaintManagerUI::SetInstance(hInstance); CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()); FramWnd* pFrame = new FramWnd(); if (pFrame == NULL) return 0; pFrame->Create(NULL, _T("测试"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); pFrame->ShowWindow(true); CPaintManagerUI::MessageLoop(); return 0; }
FramWnd.h
#pragma once #include "UIlib.h" using namespace DuiLib; class FramWnd :public WindowImplBase { public: FramWnd(); virtual ~FramWnd() {} // 通过 CWindowWnd 继承 virtual LPCTSTR GetWindowClassName() const override; UINT GetClassStyle() const override; LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override; CPaintManagerUI m_pm; // 通过 WindowImplBase 继承 virtual CDuiString GetSkinFolder() override; virtual CDuiString GetSkinFile() override; virtual CControlUI* CreateControl(LPCTSTR pstrClass); };
FramWnd.cpp
#include "FramWnd.h" #include "mycontrol.h" LPCTSTR FramWnd::GetWindowClassName() const { return _T("FramWnd"); } UINT FramWnd::GetClassStyle() const { return UI_CLASSSTYLE_FRAME | CS_DBLCLKS; ; } LRESULT FramWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_CREATE) { m_pm.Init(m_hWnd); CDialogBuilder builder; CControlUI* pRoot = builder.Create(_T("WindowsProject1.xml"), 0, this, &m_pm); m_pm.AttachDialog(pRoot); m_pm.AddNotifier(this); return 0; } else if (uMsg == WM_DESTROY) { ::PostQuitMessage(0); } LRESULT lRes = 0; if (m_pm.MessageHandler(uMsg, wParam, lParam,lRes)) return lRes; return CWindowWnd::HandleMessage(uMsg, wParam, lParam); } CDuiString FramWnd::GetSkinFolder() { return CDuiString(); } CDuiString FramWnd::GetSkinFile() { return CDuiString(); } CControlUI* FramWnd::CreateControl(LPCTSTR pstrClass) { if (_tcsicmp(pstrClass, _T("mycontrol")) == 0) { return new mycontrol; } return NULL; } FramWnd::FramWnd() { }
mycontrol.h
#pragma once #include "UIlib.h" typedef struct { POINT startPoint; // 鼠标按下时的坐标点, POINT endPoint; // 鼠标释放时的坐标点, }line; namespace DuiLib { class mycontrol : public CControlUI { public: mycontrol(void); ~mycontrol(void); virtual void SetPos(RECT rc, bool bNeedInvalidate = true); bool DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl); virtual void DoEvent(TEventUI& event); private: POINT _startPoint; // 鼠标按下时的坐标点, POINT _endPoint; // 鼠标释放时的坐标点 bool _lbtnDown; // 鼠标左键是否按下。 std::vector<line> _lines; // 存储所有绘制的线段。 }; }
mycontrol.cpp
#include "mycontrol.h" #include "UILib.h" namespace DuiLib { mycontrol::mycontrol(void){} mycontrol::~mycontrol(void) {} void mycontrol::SetPos(RECT rc, bool bNeedInvalidate) { CControlUI::SetPos(rc, bNeedInvalidate); } bool mycontrol::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl) { RECT rc = GetPos() ; if (IsRectEmpty(&rc)) { return false; } COLORREF bgColor = RGB(255, 255, 255); // 默认白色 HBRUSH hBrush = CreateSolidBrush(bgColor); if (hBrush) { HGDIOBJ hOldBrush = SelectObject(hDC, hBrush); FillRect(hDC, &rc, hBrush); DrawText(hDC, _T("Hello, World!"), -1, (LPRECT)&rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER); HPEN hPen2 = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); SetPixel(hDC, 30, 10, RGB(255, 0, 0)); SetPixel(hDC, _startPoint.x, _startPoint.y-3, RGB(255, 0, 0)); SelectObject(hDC, hPen2); MoveToEx(hDC, 0, 0, NULL); LineTo(hDC, rc.right, rc.bottom); MoveToEx(hDC, _startPoint.x, _startPoint.y, NULL); LineTo(hDC, _endPoint.x, _endPoint.y); for (auto line:_lines) { MoveToEx(hDC, line.startPoint.x, line.startPoint.y, NULL); LineTo(hDC, line.endPoint.x, line.endPoint.y); } DeleteObject(hPen2); SelectObject(hDC, hOldBrush); DeleteObject(hBrush); } return false; } void mycontrol::DoEvent(TEventUI& event) { switch (event.Type) { case UIEVENT_MOUSEMOVE: if (_lbtnDown) { _endPoint = event.ptMouse; Invalidate(); } return; case UIEVENT_BUTTONUP: _lbtnDown = false; line l; l.startPoint = _startPoint; l.endPoint = _endPoint; _lines.push_back(l); return; case UIEVENT_BUTTONDOWN: _lbtnDown = true; _startPoint = event.ptMouse; return; default: break; } CControlUI::DoEvent(event); } }
WindowsProject1.xml
<?xml version="1.0"?> <Window mininfo="200,360" size=" 480,320 "> <VerticalLayout bkcolor="#FFFF00FF"> <mycontrol /> </VerticalLayout> </Window>
效果
标签:控件,endPoint,return,自定义,duilib,FramWnd,mycontrol,hDC,startPoint From: https://www.cnblogs.com/liuguoyao514257665/p/18206103