⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interact.cxx

📁 windows mobile phone source code
💻 CXX
📖 第 1 页 / 共 3 页
字号:
}


PDim PInteractor::GetBorderSize() const
{
  return owner->GetBorderSize();
}


const PColour & PInteractor::GetBorderColour() const
{
  return owner->GetActiveBorderColour();
}


void PInteractor::SetBackgroundColour(const PColour & newColour)
{
  backgroundColour = newColour;
  Invalidate();
}


BOOL PInteractor::HandleScrollBar(HWND scrWnd, WPARAM code, int trackVal)
{
  if (scrWnd == NULL)
    return FALSE;

  PScrollBar * bar = (PScrollBar *)owner->GetWindowObject(scrWnd);
  if (bar == NULL)
    return FALSE;

  OnControlNotify(*bar, bar->TrackScrollBar(code, trackVal));

  return TRUE;
}


BOOL PInteractor::HandleCommand(NMHDR & msg)
{
  if (msg.hwndFrom == NULL)
    return FALSE;

  PControl * control = (PControl*)owner->GetWindowObject(msg.hwndFrom);
  if (control == NULL)
    return FALSE;

  _WindowsMessage * oldMessage = control->_msg;
  control->_msg = _msg;
  int option = control->TranslateOption(msg);
  control->_msg = oldMessage;

  if (option < 0)
    return option < -1;

  OnControlNotify(*control, option);

  return !control->GetNotifier().IsNULL();
}


BOOL PInteractor::HandleDrawItem(const DRAWITEMSTRUCT FAR * dis)
{
  PControl * item = (PControl *)owner->GetWindowObject(dis->hwndItem);
  if (item == NULL)
    return FALSE;

  switch (dis->CtlType) {
    case ODT_BUTTON :
      if (!((PPushButton *)item)->IsOwnerDraw())
        return FALSE;
      break;

    case ODT_LISTBOX :
      if (!item->IsDescendant(PCustomListBox::Class()))
        return FALSE;
      break;

    default :
      return FALSE;
  }

  PDrawCanvas canvas(item, dis->hDC, FALSE, FALSE);
  BOOL focus = (dis->itemState&ODS_FOCUS) != 0;
  BOOL select = (dis->itemState&ODS_SELECTED) != 0;

  if (dis->CtlType == ODT_BUTTON) {
    PPushButton * btn = (PPushButton *)item;
    PRect bounds = btn->DrawButtonSurround(canvas, select);
    btn->OnDrawFace(canvas, bounds, focus, select);
  }
  else {
    PRect bounds = canvas.FromPixels(PRect(dis->rcItem));
    if ((int)dis->itemID < 0)
      canvas.DrawFocusRect(bounds);
    else {
      if (!item->IsEnabled()) {
        canvas.SetTextFgColour(owner->GetGrayTextColour());
        canvas.SetTextBkColour(owner->GetWindowBkColour());
      }
      else if (select) {
        canvas.SetTextFgColour(owner->GetHighlightFgColour());
        canvas.SetTextBkColour(owner->GetHighlightBkColour());
      }
      else {
        canvas.SetTextFgColour(owner->GetWindowFgColour());
        canvas.SetTextBkColour(owner->GetWindowBkColour());
      }
      canvas.SetPenFgColour(canvas.GetTextBkColour());
      canvas.SetPenBkColour(canvas.GetTextBkColour());
      canvas.SetFillFgColour(canvas.GetTextBkColour());
      canvas.SetFillBkColour(canvas.GetTextBkColour());
      ((PCustomListBox *)item)->OnDrawEntry(dis->itemID,
                     *(PObject *)dis->itemData, canvas, bounds, focus, select);
    }
  }

  return TRUE;
}


BOOL PInteractor::HandleMeasureItem(MEASUREITEMSTRUCT FAR * mis)
{
  if (mis->CtlType != ODT_LISTBOX)
    return FALSE;

  PCustomListBox * listBox =
             (PCustomListBox *)owner->GetWindowObject(GetDlgItem(_hWnd, mis->CtlID));
  if (listBox == NULL)
    return FALSE;

  if (!listBox->IsDescendant(PCustomListBox::Class()))
    return FALSE;

  PDrawCanvas listBoxCanvas(listBox);
  PDim dim = listBoxCanvas.ToPixels(listBox->OnMeasureEntry(mis->itemID,
                                    *(PObject *)mis->itemData, listBoxCanvas));
  mis->itemWidth = dim.Width();
  mis->itemHeight = dim.Height();
  return TRUE;
}


int PInteractor::HandleCompareItem(const COMPAREITEMSTRUCT FAR * cis)
{
  if (cis->CtlType != ODT_LISTBOX)
    return P_MAX_INDEX;

  PInteractor * listBox = owner->GetWindowObject(cis->hwndItem);
  if (listBox == NULL)
    return P_MAX_INDEX;

  if (!listBox->IsDescendant(PCustomListBox::Class()))
    return P_MAX_INDEX;

  PObject * obj1 = (PObject *)cis->itemData1;
  PObject * obj2 = (PObject *)cis->itemData2;
  if (obj1 == NULL || obj2 == NULL)
    return P_MAX_INDEX;

  return obj1->Compare(*obj2);
}


BOOL PInteractor::HandleDeleteItem(const DELETEITEMSTRUCT FAR * dis)
{
  if (dis->CtlType != ODT_LISTBOX)
    return FALSE;

  PListBox * listBox = (PListBox*)owner->GetWindowObject(dis->hwndItem);
  if (listBox == NULL)
    return FALSE;

  if (!listBox->IsDescendant(PCustomListBox::Class()))
    return FALSE;

  if (listBox->deleteObjects)
    delete (PObject *)dis->itemData;

  return TRUE;
}


static PKeyCode KeyFromMouseEvent(UINT event, WPARAM wParam)
{
  switch (event) {
    case WM_MBUTTONDOWN :
    case WM_MBUTTONDBLCLK :
    case WM_MBUTTONUP :
      return PKeyCode(TRUE, wParam, PKeyCode::MiddleButton);

    case WM_RBUTTONDOWN :
    case WM_RBUTTONDBLCLK :
    case WM_RBUTTONUP :
      return PKeyCode(TRUE, wParam, PKeyCode::RightButton);
  }
  return PKeyCode(TRUE, wParam, PKeyCode::LeftButton);
}


BOOL PInteractor::HandleCtlColour(HWND ctlWnd,
                         HDC hDC, const PColour & defFg, const PColour & defBk)
{
  PInteractor * control = owner->GetWindowObject(ctlWnd);
  if (control == NULL)
    return FALSE;

  const PColour & fg = control->GetForegroundColour();
  const PColour & bk = control->GetBackgroundColour();
  if (fg == defFg && bk == defBk)
    return FALSE;

  SetTextColor(hDC, fg.ToCOLORREF());
  SetBkColor(hDC, bk.ToCOLORREF());
  if (control->hBackgroundBrush == NULL)
    control->hBackgroundBrush = CreateSolidBrush(bk.ToCOLORREF());
  _msg->lResult = (LPARAM)control->hBackgroundBrush;
  return TRUE;
}


void PInteractor::WndProc()
{
  switch (_msg->event) {
    case WM_MOUSEMOVE :
      if (PGET_CLASS_VALUE(_hWnd, HCURSOR) == 0)
        SetWndCursor();
      _OnMouseMove(PKeyCode(TRUE, _msg->wParam, PKeyCode::NullValue), PPoint(_msg->lParam));
      return;

    case WM_LBUTTONDOWN :
    case WM_MBUTTONDOWN :
    case WM_RBUTTONDOWN :
      owner->DoBalloonHelp(NULL);
      if (GetCapture() == NULL)
        SetCapture(_hWnd);
      OnMouseDown(KeyFromMouseEvent(_msg->event, _msg->wParam), PPoint(_msg->lParam), FALSE);
      return;

    case WM_LBUTTONDBLCLK :
    case WM_MBUTTONDBLCLK :
    case WM_RBUTTONDBLCLK :
      owner->DoBalloonHelp(NULL);
      OnMouseDown(KeyFromMouseEvent(_msg->event, _msg->wParam), PPoint(_msg->lParam), TRUE);
      return;

    case WM_LBUTTONUP :
    case WM_MBUTTONUP :
    case WM_RBUTTONUP :
      _OnMouseUp(KeyFromMouseEvent(_msg->event, _msg->wParam), PPoint(_msg->lParam));
      if (GetCapture() == _hWnd)
        ReleaseCapture();
      return;

    case WM_KEYDOWN :
      owner->DoBalloonHelp(NULL);
      if (OnKeyDown(PKeyCode(FALSE, _msg->wParam, HIWORD(_msg->lParam)),
                      (_msg->lParam&0x40000000)!=0 ? LOWORD(_msg->lParam) : 0))
        DefWndProc();
      return;

    case WM_KEYUP :
      owner->DoBalloonHelp(NULL);
      OnKeyUp(PKeyCode(FALSE, _msg->wParam, HIWORD(_msg->lParam)));
      return;

    case WM_CHAR :
      OnKeyInput(PString((char)_msg->wParam));
      return;

    case WM_SETFOCUS : {
      caret.Activate(this, caretVisible <= 0);
      OnGainFocus();
      PInteractor * focus = GetFocusInteractor();
      if (focus != this && focus != NULL) {
        if (focus->OnEndInput()) {
          SetFocusInteractor(this);
          OnStartInput();
        }
        else
          focus->GrabFocus();
      }
      break;
    }

    case WM_KILLFOCUS :
      caret.Deactivate(this);
      OnLostFocus();
      break;

    case WM_VSCROLL :
    case WM_HSCROLL :
#if defined(_WIN32)
      if (HandleScrollBar((HWND)_msg->lParam,
                                   LOWORD(_msg->wParam), HIWORD(_msg->wParam)))
#else
      if (HandleScrollBar((HWND)HIWORD(_msg->lParam),
                                      _msg->wParam, (int)LOWORD(_msg->lParam)))
#endif
        return;
      break;

    case WM_COMMAND :
    {
      NMHDR msg;
#if defined(_WIN32)
      msg.hwndFrom = (HWND)_msg->lParam;
      msg.idFrom = LOWORD(_msg->wParam);
      msg.code = HIWORD(_msg->wParam);
#else
      msg.hwndFrom = (HWND)LOWORD(_msg->lParam);
      msg.idFrom = wParam;
      msg.code = HIWORD(_msg->lParam);
#endif
      if (HandleCommand(msg))
        return;
      break;
    }

    case WM_NOTIFY :
      if (HandleCommand(*(LPNMHDR)_msg->lParam))
        return;
      break;

    case WM_DRAWITEM :
      if (HandleDrawItem((const DRAWITEMSTRUCT FAR *)_msg->lParam)) {
        _msg->lResult = TRUE;
        return;
      }
      break;

    case WM_MEASUREITEM :
      if (HandleMeasureItem((MEASUREITEMSTRUCT FAR *)_msg->lParam), _hWnd) {
        _msg->lResult = TRUE;
        return;
      }
      break;

    case WM_COMPAREITEM :
      switch (HandleCompareItem((const COMPAREITEMSTRUCT FAR *)_msg->lParam)) {
        case PObject::LessThan :
          _msg->lResult = -1;
          return;
        case PObject::EqualTo :
          _msg->lResult = 0;
          return;
        case PObject::GreaterThan :
          _msg->lResult = 1;
          return;
      }
      break;

    case WM_DELETEITEM :
      if (HandleDeleteItem((const DELETEITEMSTRUCT FAR *)_msg->lParam)) {
        _msg->lResult = TRUE;
        return;
      }
      break;

    case WM_ERASEBKGND :
      if (PGET_CLASS_VALUE(_hWnd, HBRBACKGROUND) == 0) {
        HBRUSH b = CreateSolidBrush(backgroundColour.ToCOLORREF());
        RECT r;
        GetClientRect(_hWnd, &r);
        FillRect((HDC)_msg->wParam, &r, b);
        DeleteObject(b);
        _msg->lResult = TRUE;
        return;
      }
      break;

#ifdef _WIN32
    case WM_CTLCOLORBTN :
    case WM_CTLCOLORSTATIC :
      if (HandleCtlColour((HWND)_msg->lParam, (HDC)_msg->wParam,
                       owner->GetButtonFgColour(), owner->GetButtonBkColour()))
        return;
      break;
    case WM_CTLCOLORSCROLLBAR :
      if (HandleCtlColour((HWND)_msg->lParam, (HDC)_msg->wParam,
                     owner->GetScrollBarColour(), owner->GetScrollBarColour()))
        return;
      break;
    case WM_CTLCOLOREDIT :
    case WM_CTLCOLORLISTBOX :
      if (HandleCtlColour((HWND)_msg->lParam, (HDC)_msg->wParam,
                       owner->GetWindowFgColour(), owner->GetWindowBkColour()))
        return;
      break;
#else
    case WM_CTLCOLOR :
      switch (HIWORD(_msg->lParam)) {
        case CTLCOLOR_BTN :
          if (HandleCtlColour((HWND)LOWORD(_msg->lParam), (HDC)_msg->wParam,
                       owner->GetButtonFgColour(), owner->GetButtonBkColour()))
            return;
          break;
        case CTLCOLOR_SCROLLBAR :
          if (HandleCtlColour((HWND)LOWORD(_msg->lParam), (HDC)_msg->wParam,
                     owner->GetScrollBarColour(), owner->GetScrollBarColour()))
            return;
          break;
        case CTLCOLOR_EDIT :
        case CTLCOLOR_STATIC :
        case CTLCOLOR_LISTBOX :
          if (HandleCtlColour((HWND)LOWORD(_msg->lParam), (HDC)_msg->wParam,
                       owner->GetWindowFgColour(), owner->GetWindowBkColour()))
            return;
          break;
      }
      break;
#endif

    case WM_PAINT :
      if (_in_WM_PAINT)
        DefWndProc();
      else {
        _in_WM_PAINT = TRUE;
        PRedrawCanvas canvas(this, FALSE);
        OnRedraw(canvas);
        if (canvas.NullHDC())
          DefWndProc();
        _in_WM_PAINT = FALSE;
      }
      return;

    case WM_DESTROY :
      owner->DoBalloonHelp(NULL);
      owner->RemoveWindowHandle(_hWnd);
      RemoveWndFont();
      _hWnd = P_DEAD_WINDOW;
      return;
  }
  DefWndProc();
}


void PInteractor::DefWndProc()
{
  _msg->lResult=DefWindowProc(_hWnd, _msg->event, _msg->wParam, _msg->lParam);
  _msg->processed = FALSE;
}


void PInteractor::OnMouseDown(PKeyCode, const PPoint &, BOOL)
{
  if (GetCapture() == _hWnd)
    ReleaseCapture();
  DefWndProc();
}


void PInteractor::OnMouseUp(PKeyCode, const PPoint &)
{
  DefWndProc();
}


void PInteractor::OnMouseMove(PKeyCode, const PPoint &)
{
  DefWndProc();
}


BOOL PInteractor::OnKeyDown(PKeyCode, unsigned)
{
  return TRUE;
}


void PInteractor::OnKeyUp(PKeyCode key)
{
  DefWndProc();
}


void PInteractor::OnKeyInput(const PString &)
{
  DefWndProc();
}


void PInteractor::OnGainFocus()
{
  DefWndProc();
}


void PInteractor::OnLostFocus()
{
  DefWndProc();
}


void PInteractor::OnRedraw(PCanvas & canvas)
{
  if (canvas.NullHDC() && canvas.IsDescendant(PRedrawCanvas::Class())) {
    DefWndProc();
    canvas.SetHDC(GetDC(_hWnd));
  }
}


void PInteractor::SetWndFont() const
{
  RemoveWndFont();
  HFONT hFont = CreateFont(font.GetHeight(TRUE),
                           0, 0, 0,
                           font.IsBold() ? FW_BOLD : FW_NORMAL,
                           (BYTE)font.IsItalic(),
                           (BYTE)font.IsUnderlined(),
                           0, 0, 0, 0, 0, 0,
                           font.GetFacename());
  PAssertOS(hFont != NULL);
  SendMessage(GetHWND(), WM_SETFONT, (WPARAM)hFont, 0L);
  if ((HFONT)(UINT)SendMessage(GetHWND(), WM_GETFONT, 0, 0L) != hFont)
    DeleteObject(hFont);
}


void PInteractor::RemoveWndFont() const
{
  HFONT hFont = (HFONT)(UINT)SendMessage(GetHWND(), WM_GETFONT, 0, 0L);
  if (hFont == NULL)
    return;

  HWND parWnd = P_DEAD_WINDOW;
  if (GetParent() != NULL)
    parWnd = GetParent()->_hWnd;

  if (parWnd != P_DEAD_WINDOW &&
                  (HFONT)(UINT)SendMessage(parWnd, WM_GETFONT, 0, 0L) == hFont)
    return; // Let parent class delete the hFont

  SendMessage(GetHWND(), WM_SETFONT, NULL, 0L);
  DeleteObject(hFont);
}


void PInteractor::GetCreateWinInfo(WNDCLASS & wndClass)
{
  wndClass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
  wndClass.lpfnWndProc = owner->GetWndProcPtr(PApplication::WndProcType);
  wndClass.cbClsExtra = 0;
  wndClass.cbWndExtra = 0;
  wndClass.hInstance = owner->GetInstance();
  wndClass.hIcon = NULL;
  wndClass.hCursor = NULL;
  wndClass.hbrBackground = NULL;
  wndClass.lpszMenuName = NULL;
  wndClass.lpszClassName = GetClass();
  _styleBits = WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
  _exStyleBits = WS_EX_NOPARENTNOTIFY;
}


PINDEX PInteractor::GetNumChildren()
{
  if (_hWnd == NULL)
    GetHWND();
  return children.GetSize();
}

HWND PInteractor::GetHWND() const
{

⌨️ 快捷键说明

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