twindow.cpp

来自「从FFMPEG转换而来的H264解码程序,VC下编译..」· C++ 代码 · 共 1,109 行 · 第 1/3 页

CPP
1,109
字号
 mii.hSubMenu=subMenu;
 mii.dwTypeData=LPTSTR(caption);
 mii.cch=(LONG)strlen(mii.dwTypeData);
 InsertMenuItem(hm,ord++,TRUE,&mii);
}

void Twindow::insertMenuItem(HMENU hm,int &ord,int id,const char_t *caption,bool checked,bool newcolumn)
{
 MENUITEMINFO mii;
 mii.cbSize=sizeof(mii);
 mii.fMask=MIIM_ID|MIIM_STATE|MIIM_TYPE;
 mii.fType=MFT_STRING|(newcolumn?MFT_MENUBREAK:0);
 mii.wID=id;
 mii.fState=(checked)?MFS_CHECKED:0;
 mii.dwTypeData=LPTSTR(caption);
 mii.cch=(LONG)strlen(mii.dwTypeData);
 InsertMenuItem(hm,ord++,TRUE,&mii);
}
void Twindow::enable(HMENU hm,int ord,bool is)
{
 MENUITEMINFO mii;
 mii.cbSize=sizeof(mii);
 mii.fMask=MIIM_STATE;
 GetMenuItemInfo(hm,ord,TRUE,&mii);
 if (is)
  mii.fState|=MFS_ENABLED;
 else
  mii.fState|=MFS_DISABLED;
 SetMenuItemInfo(hm,ord,TRUE,&mii);
}
int Twindow::selectFromMenu(const char_t **items,int id,bool translate,int columnHeight)
{
 HMENU hm=CreatePopupMenu();
 for (int ord=0,i=0;items[ord];i++)
  insertMenuItem(hm,ord,i+1,items[i],false,columnHeight && i && (i%columnHeight)==0);
 RECT r;
 if (id)
  GetWindowRect(GetDlgItem(m_hwnd,id),&r);
 else
  {
   POINT pt;
   GetCursorPos(&pt);
   r.left=pt.x;r.bottom=pt.y;
  }
 int cmd=TrackPopupMenu(translate?_(hm):hm,TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RETURNCMD,r.left-1,r.bottom,0,m_hwnd,0);
 DestroyMenu(hm);
 return cmd?cmd-1:-1;
}
int Twindow::selectFromMenu(const strings &items,int id,bool translate,int columnHeight)
{
 Tstrptrs ptritems;
 for (strings::const_iterator s=items.begin();s!=items.end();s++)
  ptritems.push_back(s->c_str());
 ptritems.push_back(NULL);
 return selectFromMenu(&ptritems[0],id,translate,columnHeight);
}

bool Twindow::changeDir(int idff,const char_t *caption)
{
 char_t dir[MAX_PATH];cfgGet(idff,dir,MAX_PATH);
 if (dlgGetDir(m_hwnd,dir,caption))
  {
   cfgSet(idff,dir);
   return true;
  }
 else
  return false;
}

bool Twindow::chooseColor(int idff)
{
 CHOOSECOLOR cc;
 memset(&cc,0,sizeof(cc));
 cc.lStructSize=sizeof(cc);
 cc.hwndOwner=m_hwnd;
 cc.rgbResult=cfgGet(idff);
 static const int idffDlgCustColors[16]={IDFF_dlgCustColor0,IDFF_dlgCustColor1,IDFF_dlgCustColor2,IDFF_dlgCustColor3,IDFF_dlgCustColor4,IDFF_dlgCustColor5,IDFF_dlgCustColor6,IDFF_dlgCustColor7,IDFF_dlgCustColor8,IDFF_dlgCustColor9,IDFF_dlgCustColor10,IDFF_dlgCustColor11,IDFF_dlgCustColor12,IDFF_dlgCustColor13,IDFF_dlgCustColor14,IDFF_dlgCustColor15};
 COLORREF custColors[16];
 for (int i=0;i<16;i++)
  custColors[i]=cfgGet(idffDlgCustColors[i]);
 cc.lpCustColors=custColors;
 cc.Flags=CC_ANYCOLOR|CC_FULLOPEN|CC_RGBINIT;
 if (ChooseColor(&cc))
  {
   cfgSet(idff,cc.rgbResult);
   for (int i=0;i<16;i++)
    cfgSet(idffDlgCustColors[i],custColors[i]);
   return true;
  }
 else
  return false;
}

void Twindow::setSize(HWND hwnd,int dx,int dy)
{
 SetWindowPos(hwnd,NULL,0,0,dx,dy,SWP_NOMOVE|SWP_NOZORDER);
}
void Twindow::setSize(int dx,int dy)
{
 setSize(m_hwnd,dx,dy);
}
void Twindow::setItemSize(int id,int dx,int dy)
{
 setSize(GetDlgItem(m_hwnd,id),dx,dy);
}
void Twindow::setSizeD(HWND hwnd,int dx,int dy)
{
 CRect r;
 GetWindowRect(hwnd,&r);
 SetWindowPos(hwnd,NULL,0,0,r.Width()+dx,r.Height()+dy,SWP_NOMOVE|SWP_NOZORDER);
}
void Twindow::setSizeD(int dx,int dy)
{
 setSizeD(m_hwnd,dx,dy);
}
void Twindow::setItemSizeD(int id,int dx,int dy)
{
 setSizeD(GetDlgItem(m_hwnd,id),dx,dy);
}

void Twindow::setPos(HWND hwnd,int x,int y)
{
 SetWindowPos(hwnd,NULL,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
}
void Twindow::setPos(int x,int y)
{
 setPos(m_hwnd,x,y);
}
void Twindow::setItemPos(int id,int x,int y)
{
 setPos(GetDlgItem(m_hwnd,id),x,y);
}
void Twindow::setItemPosD(int id,int dx,int dy)
{
 HWND h=GetDlgItem(m_hwnd,id);
 CRect r;GetWindowRect(h,&r);
 CPoint pt=r.TopLeft();
 ScreenToClient(m_hwnd,&pt);
 SetWindowPos(h,NULL,pt.x+dx,pt.y+dy,0,0,SWP_NOSIZE|SWP_NOZORDER);
}

HWND Twindow::createHintWindow(HWND parent,int timePop,int timeInit,int timeReshow)
{
 HWND hhint=CreateWindowEx(WS_EX_TOPMOST,
                          TOOLTIPS_CLASS,
                          NULL,
                          WS_POPUP|TTS_NOPREFIX|TTS_ALWAYSTIP,
                          CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
                          parent,NULL,hi,NULL
                         );
 SetWindowPos(hhint,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
 SendMessage(hhint,TTM_SETDELAYTIME,TTDT_AUTOPOP,MAKELONG(timePop,0));
 SendMessage(hhint,TTM_SETDELAYTIME,TTDT_INITIAL,MAKELONG(timeInit,0));
 SendMessage(hhint,TTM_SETDELAYTIME,TTDT_RESHOW,MAKELONG(timeReshow,0));
 SendMessage(hhint,TTM_SETMAXTIPWIDTH,0,470);
 return hhint;
}

TOOLINFO Twindow::addHint(int id,const char_t *text)
{
 if (!hhint) hhint=createHintWindow(m_hwnd,15000);
 TOOLINFO ti;
 ti.cbSize=sizeof(TOOLINFO);
 ti.uFlags=TTF_SUBCLASS|TTF_IDISHWND;
 ti.hwnd=m_hwnd;
 ti.uId=(LPARAM)GetDlgItem(m_hwnd,id);
 ti.lpszText=LPTSTR(_(-dialogId,text));
 SendMessage(hhint,TTM_ADDTOOL,0,(LPARAM)&ti);
 return ti;
}

void Twindow::enableHints(bool is)
{
 if (hhint)
  SendMessage(hhint,TTM_ACTIVATE,is?TRUE:FALSE,0);
}

CRect Twindow::getChildRect(int id) const
{
 CRect rs;GetWindowRect(GetDlgItem(m_hwnd,id),&rs);
 CPoint tl=rs.TopLeft();ScreenToClient(m_hwnd,&tl);
 CPoint br=rs.BottomRight();ScreenToClient(m_hwnd,&br);
 return CRect(tl,br);
}

Twindow::Tanchor::Tanchor(int Iid,int Ianchors,const Twindow &parent,const CRect &parentRect):id(Iid),anchors(Ianchors)
{
 CRect r=parent.getChildRect(id);
 if (anchors==(TanchorInfo::LEFT|TanchorInfo::TOP)) return;
 if (TanchorInfo::RIGHT&anchors)
   if (TanchorInfo::LEFT&anchors)
    rules.x=r.Width();
   else
    rules.x=r.left;
  else
   rules.x=r.left+r.Width()/2;

 if (TanchorInfo::BOTTOM&anchors)
  if (TanchorInfo::TOP&anchors)
   rules.y=r.Height();
  else
   rules.y=r.top;
 else
  rules.y=r.top+r.Height()/2;
 originalParentSize=CSize(parentRect.Width(),parentRect.Height());
}

void Twindow::Tanchor::resize(const Twindow &parent,const CRect &newrect)
{
 HWND h=GetDlgItem(parent.m_hwnd,id);
 if (!h) return;
 CRect r=parent.getChildRect(id);
 int newLeft=r.left;
 int newTop=r.top;
 int newWidth=r.Width();
 int newHeight=r.Height();
 CSize parentSize=newrect.Size();
 if (TanchorInfo::RIGHT&anchors)
  if (TanchorInfo::LEFT&anchors)
   newWidth=parentSize.cx-(originalParentSize.cx-rules.x);
  else
   newLeft=parentSize.cx-(originalParentSize.cx-rules.x);
 else if (!(TanchorInfo::LEFT&anchors))
   newLeft=rules.x*parentSize.cx/originalParentSize.cx-newWidth/2;

 if (TanchorInfo::BOTTOM&anchors)
  if (TanchorInfo::TOP&anchors)
   newHeight=parentSize.cy-(originalParentSize.cy-rules.y);
  else
   newTop=parentSize.cy-(originalParentSize.cy-rules.y);
 else if (!(TanchorInfo::TOP&anchors))
  newTop=rules.y*parentSize.cy/originalParentSize.cy-newHeight/2;
 MoveWindow(h,newLeft,newTop,newWidth,newHeight,TRUE);
 repaint(h);
}

void Twindow::Tanchors::add(const TanchorInfo *ainfo,const Twindow &parent)
{
 CRect r;GetWindowRect(parent.m_hwnd,&r);
 for (int i=0;ainfo[i].id;i++)
  push_back(Tanchor(ainfo[i].id,ainfo[i].anchor,parent,r));
}
void Twindow::Tanchors::resize(const Twindow &parent,const CRect &newrect)
{
 for (iterator a=begin();a!=end();a++)
  a->resize(parent,newrect);
}

template<class Tret,class TcreateFunc1,class TcreateFunc2> Tret Twindow::createDlg_Box(int dlgId,HWND hWndParent,LPARAM lparam,TcreateFunc1 createFunc1,TcreateFunc2 createFunc2)
{
 if (bindsHtrack)
  for (int i=0;i<bindsHtrack[i].idc;i++)
   bindTrackbarsMap.insert(std::make_pair(bindsHtrack[i].idc,bindsHtrack+i));
 if (bindsVtrack)
  for (int i=0;i<bindsVtrack[i].idc;i++)
   bindTrackbarsMap.insert(std::make_pair(bindsVtrack[i].idc,bindsVtrack+i));
 if (bindsCheckbox)
  for (int i=0;i<bindsCheckbox[i].idc;i++)
   bindCheckboxesMap.insert(std::make_pair(bindsCheckbox[i].idc,bindsCheckbox+i));
 HINSTANCE hi=this->hi?this->hi:g_hInst;//HACK!!
 if (!tr || !tr->isCustomFont())
  {
   char_t lang[20],pth[MAX_PATH];
   getpth(pth);
   getUILanguage(pth,lang);
   Ttranslate tr1(g_hInst,pth); // Create temporary translator to calculate window size.
   tr1.init(lang,0);            // don't care translatorMode, because we just want size.
   DLGTEMPLATE *dlg=tr1.translateDialogTemplate(dlgId);
   Tret ret=createFunc2(hi,dlg,hWndParent,msgProc0,lparam);
   LocalFree(dlg);
   return ret;
  }
 else
  {
   DLGTEMPLATE *dlg=tr->translateDialogTemplate(dlgId);
   Tret ret=createFunc2(hi,dlg,hWndParent,msgProc0,lparam);
   LocalFree(dlg);
   return ret;
  }
}
HWND Twindow::createDialog(int dlgId,HWND hWndParent)
{
 return createDlg_Box<HWND>(dlgId,hWndParent,(LPARAM)new TwindowWidget(NULL,this),CreateDialogParam,CreateDialogIndirectParam);
}
HWND Twindow::createDialog(int dlgId,HWND hWndParent,LPARAM lparam)
{
 return createDlg_Box<HWND>(dlgId,hWndParent,lparam,CreateDialogParam,CreateDialogIndirectParam);
}
INT_PTR Twindow::dialogBox(int dlgId,HWND hWndParent)
{
 return createDlg_Box<INT_PTR>(dlgId,hWndParent,(LPARAM)new TwindowWidget(NULL,this),DialogBoxParam,DialogBoxIndirectParam);
}
INT_PTR Twindow::dialogBox(int dlgId,HWND hWndParent,LPARAM lparam)
{
 return createDlg_Box<INT_PTR>(dlgId,hWndParent,lparam,DialogBoxParam,DialogBoxIndirectParam);
}

bool Twindow::onTrack(TbindTrackbars bindsTrack,LPARAM lParam)
{
 if (bindsTrack)
  {
   int id=getId(HWND(lParam));
   for (int i=0;bindsTrack[i].idc;i++)
    if (bindsTrack[i].idc==id)
     {
      if (bindsTrack[i].idff<0)
       cfgSet(-bindsTrack[i].idff,-tbrGet(bindsTrack[i].idc));
      else
       cfgSet(bindsTrack[i].idff,tbrGet(bindsTrack[i].idc));
      if (bindsTrack[i].onClick)
       (this->*bindsTrack[i].onClick)();
      return true;
     }
  }
 return false;
}

template<class TbindEdit> bool Twindow::onCtlColorEdit(TbindEdit *bindsEdit,LPARAM lParam,WPARAM wParam)
{
 HWND hwnd=HWND(lParam);
 int id=getId(hwnd);
 for (int i=0;bindsEdit[i].idc;i++)
  if (bindsEdit[i].idc==id)
   if (!eval(hwnd,bindsEdit[i].min,bindsEdit[i].max))
    {
     HDC dc=HDC(wParam);
     SetBkColor(dc,RGB(255,0,0));
     return true;
    }
 return false;
}
HBRUSH Twindow::getRed(void)
{
 if (!red) red=CreateSolidBrush(RGB(255,0,0));
 return red;
}

INT_PTR Twindow::msgProc(UINT uMsg,WPARAM wParam,LPARAM lParam)
{
 switch (uMsg)
  {
   case WM_INITDIALOG:
    init();
    //subClass();
    break;
   case WM_DESTROY:
    {
     THWNDs wnds;getChildWindows(m_hwnd,wnds);
     for (THWNDs::const_iterator wnd=wnds.begin();wnd!=wnds.end();wnd++)
      {
       ::Twidget *item=Twidget::getDlgItem(*wnd);
       if (item)
        {
         item->restore();
         delete item;
        }
      }
     ::Twidget *item=Twidget::getDlgItem(m_hwnd);
     if (item)
      {
       item->restore();
       delete item;
      }
     if (red) DeleteObject(red);red=NULL;
     break;
    }
   case WM_HSCROLL:
    if (onTrack(bindsHtrack,lParam))
     return TRUE;
    break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?