Temp

<pre name="code" class="cpp"> Listener ls;
    SampleButton btn(60,80,45,100,"Button");
    SampleText   textbox(60,220,45,100,"TextBox1");
    um.SetContainer(hwnd);
    btn.SetCallBack("OnClick",btn_1_click);
    btn.SetCallBack("OnFocus",btn_1_Focus);
    textbox.SetCallBack("OnFocus",txtbox_Focus);
    textbox.SetCallBack("TextIn",txtbox_TextIn);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (1)
    {
        if(PeekMessage(&messages, NULL, 0, 0, PM_REMOVE)){
        if(ls.GetEvent(messages))
            ls.ProcessEvent();
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
        um.Update();


    }

#ifndef SAMPLEUI_H#define SAMPLEUI_H#include "HashMap.h"#include <string>using namespace std;class SampleUI;typedef void(*func)(SampleUI*,void*args);class SampleUI{ public: int x,y,height,width,m_id; SampleUI(); ~SampleUI(); virtual void OnCreate() = 0; virtual void OnDispose() = 0; virtual void OnPaint() = 0; virtual void SetCallBack(string name,func callback) = 0; virtual HashMap<string,func> GetCallBackList()=0; virtual string GetText() = 0; virtual void SetText(string txt) = 0; protected: HashMap<string,func> CallBackList; string text; private:};#endif // SAMPLEUI_H
 
#ifndef UIMANAGER_H
#define UIMANAGER_H
#include <windows.h>
#include "SampleUI.h"
#include "Listener.h"
#include <stdio.h>
typedef struct Message{
    int ID;
    string msgtype;
    void *args;
    int time;
};
class UIManager
{
    public:
        UIManager();
        virtual ~UIManager();
        int Add(SampleUI* control);
        void Remove(int id);
        void Update();
        void SetContainer(HWND hwnd){container = hwnd;};
        HWND GetContainer(){return container;};
        static UIManager* GetInstance();
        int QueryForID(POINT pt);
        bool InsertMessage(int id,string msgtype,void* args,int time);
        void PopMessage();
        void SetFocus(int id){
            if(id!= -1)
                focusid = id;
        };
        int GetFocusID(){return focusid;};
        vector<Message> GetMessageList(){return messageList;};
    protected:
        int controlcount;
        int focusid;
        HWND container;
        HashMap<int,SampleUI*> controlList;
        vector<Message> messageList;
    private:
};

#endif // UIMANAGER_H

#include "UIManager.h"
#include <stdio.h>
static UIManager* ptr_UIManager;
UIManager::UIManager()
{
    controlcount = 0;
    focusid = -1;
    ptr_UIManager = this;
}

UIManager::~UIManager()
{
    //dtor
}

int UIManager::Add(SampleUI* control){
    controlList.InsertEnd(controlcount,control);
    return controlcount++;
}

void UIManager::Remove(SampleUI* control){

}

UIManager* UIManager::GetInstance(){
    return ptr_UIManager;
}

int UIManager::QueryForID(POINT pt){
    ScreenToClient(container,&pt);
    int x = pt.x;
    int y = pt.y;
    for(int i = 0;i<controlcount;i++){
        //printf("%d %d %d %d %d %d\n",controlList[i]->x,controlList[i]->x+controlList[i]->width,controlList[i]->y,controlList[i]->y+controlList[i]->height,x,y);
        if(controlList[i]->x<=x&&controlList[i]->x+controlList[i]->width>=x&&controlList[i]->y<=y&&controlList[i]->y+controlList[i]->height>=y){
            return i;
        }

    }
    return -1;
}

void UIManager::Update(){
    if(!messageList.empty()){
        printf("%d\n",messageList.size());
        for(vector<Message>::size_type i=0;i!=messageList.size();i++){
            if(messageList[i].ID == -1)
                break;
            if(controlList[messageList[i].ID]->GetCallBackList()[messageList[i].msgtype]!=NULL)
                controlList[messageList[i].ID]->GetCallBackList()[messageList[i].msgtype](controlList[messageList[i].ID],messageList[i].args);
        }
        messageList.clear();
    }
    for(int i = 0;i<controlcount;i++){
        controlList[i]->OnPaint();
    }
}

bool UIManager::InsertMessage(int id,string msgtype,void *args,int time){
    Message msg;
    msg.ID = id;
    msg.msgtype = msgtype;
    msg.time = time;
    msg.args = args;
    messageList.insert(messageList.end(),msg);
}

void UIManager::PopMessage(){
    messageList.erase(messageList.begin());
}

#ifndef LISTENER_H
#define LISTENER_H
#include "UIManager.h"
class Listener
{
    public:
        MSG Message;
        Listener();
        ~Listener();
        bool GetEvent(MSG message);
        void ProcessEvent();
    protected:
    private:
};

#endif // LISTENER_H

#include "Listener.h"
#include <stdio.h>
Listener::Listener()
{
    //ctor
}

Listener::~Listener()
{
    //dtor
}

bool Listener::GetEvent(MSG message){
    this->Message = message;
    return true;
}

void Listener::ProcessEvent(){
    int id;
    id = UIManager::GetInstance()->QueryForID(Message.pt);
    switch(Message.message){
        case WM_LBUTTONDOWN:
        case WM_LBUTTONDBLCLK:
            if(id == -1)
                break;
            UIManager::GetInstance()->InsertMessage(id,"OnClick",NULL,Message.time);
            UIManager::GetInstance()->InsertMessage(id,"OnFocus",NULL,Message.time);
            break;
        case WM_CHAR:
            printf("%c\n",Message.wParam);
            UIManager::GetInstance()->InsertMessage(UIManager::GetInstance()->GetFocusID(),"TextIn",(void *)&(Message.wParam),Message.time);
            break;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值