模仿Windows的文本框右键菜单

包括dxUndo,dxCut,dxCopy,dxDelete,dxPaste,dxSelectAll;分别对应 撤销,剪切,复制,删除,粘贴,全选;在Form的AppEventsIdle事件中加入以下代码,自动设置以上项目的Enabled属性(如:只有在选择文本后剪切和复制才可用)!

(尚待改进...)

void __fastcall TFormMain::AppEventsIdle(TObject *Sender, bool &Done)
{
    HWND hFocus = GetFocus();
    dxUndo->Enabled = SendMessage(hFocus, EM_CANUNDO, 0, 0);
    unsigned int pSelText = SendMessage(hFocus, EM_GETSEL, 0, 0);
    unsigned int SelLen = HIWORD(pSelText)-LOWORD(pSelText);
    dxCut->Enabled = SelLen>0;
    dxCopy->Enabled = dxCut->Enabled;
    dxDelete->Enabled = dxCut->Enabled;

    bool IsEdit;
    if (dynamic_cast<TCustomEdit*>(FindControl(hFocus)))
        IsEdit = true;
    else {
        char buf[17]="";
        IsEdit = GetClassName(hFocus,buf,sizeof(buf))>0 && !strcmpi(buf,"Edit");
    }
    dxPaste->Enabled = IsEdit && IsClipboardFormatAvailable(CF_TEXT);

    unsigned int TextLen = GetWindowTextLength(hFocus);
    dxSelectAll->Enabled = IsEdit && TextLen>0 && TextLen!=SelLen;
}

你可能感兴趣的:(windows)