
Help. Is there a way to find character index on MouseDown/Up event in DataGrid control?
Ken,
Thank you for your help.
The only problem is that it does not work exactly
as you say .
The selection is changed only when I click mouse
button the second time over the desired cell.
After the 1st click .selstart, .sellength ,.seltext
.row and .col still point to the previously selected
cell . 2nd click eventually changes the selection
to the clicked cell .
I hardly belevie that but maybe there is a problem with my
settings or installation (I have Visual Basic 6.0
and Office 2000 premium ) or I miss something
in using the control?
Now , what I needed it for?
(Sorry, probably I should've explained that in the beginning)
In fact I just wanted to implement drag-and-drop to DataGrid
control , so that the pasted text is inserted at the point
where mouse button was released. That's why I kept asking
about smth like EM_CHARFROMPOS.
Eventually I implemented it in C++ in very hacky way.
The code below sends buttondown and buttonup messages
to the datagrid control window and then to hwndeditor
(after mapping coordinates from the main datagrid
control window to hwndeditor ones)
This way it works. Smth like can be also implemented in VB
too. I wonder if there is a better way to do that?
---------------------------------------------------------
BOOL CDEDropTarget::OnDrop(CWnd* pWnd, COleDataObject* pDataObject,
DROPEFFECT dropEffect, CPoint point)
{
ASSERT(ms_pctlDataGrid);
if (!ms_bBeginDrop)
return FALSE;
if (ms_pctlTestDataDlg->IsInDragging() )
return DROPEFFECT_NONE;
short col = ms_pctlDataGrid->ColContaining((float)point.x);
short row = ms_pctlDataGrid->RowContaining((float)point.y);
if(col < 0 || col >= vCols || row <0 || row > vRows) {
return DROPEFFECT_NONE;
}
HGLOBAL hData=pDataObject->GetGlobalData(CF_TEXT);
if (!hData) {
TRACE("Fail in getting data\n");
return FALSE;
}
LPCSTR lpcszData=(LPCSTR)GlobalLock(hData);
CString lstrSel;
LPARAM lpar = MAKELPARAM(point.x, point.y);
ms_pctlDataGrid->SendMessage(WM_LBUTTONDOWN, 0, lpar);
ms_pctlDataGrid->SendMessage(WM_LBUTTONUP, 0, lpar);
HWND lHwnd = (HWND)ms_pctlDataGrid->GetHWndEditor();
RECT lrectDataGrid,lrectScrDataGrid,
lrectDataGridEditor, lrectScrDataGridEditor;
ms_pctlDataGrid->GetClientRect(&lrectDataGrid);
lrectScrDataGrid = lrectDataGrid;
ms_pctlDataGrid->ClientToScreen(&lrectScrDataGrid);
if(0 != lHwnd ) {
POINT lptScrDataGridEditor,lptScrDataGridEditor1;
::GetClientRect(lHwnd,&lrectDataGridEditor);
lptScrDataGridEditor.x = lrectDataGridEditor.left;
lptScrDataGridEditor.y = lrectDataGridEditor.top;
lptScrDataGridEditor1.x = lrectDataGridEditor.right;
lptScrDataGridEditor1.y = lrectDataGridEditor.bottom;
::ClientToScreen(lHwnd,&lptScrDataGridEditor);
::ClientToScreen(lHwnd,&lptScrDataGridEditor1);
CPoint lptGridDataOffs(lrectScrDataGrid.left,lrectScrDataGrid.top);
CPoint lptGridDataEditorOffs(lptScrDataGridEditor.x ,lptScrDataGridEditor.y);
// offset of the editor window relatively to the data grid control
// window in screen coordinates (and in client coords too)
CPoint lptOffs = lptGridDataEditorOffs - lptGridDataOffs;
// screen (and client) offset inside editor window
lptOffs = point - lptOffs;
lpar = MAKELPARAM(lptOffs.x, lptOffs.y);
::SendMessage(lHwnd , WM_LBUTTONDOWN, 0, lpar);
::SendMessage(lHwnd , WM_LBUTTONUP, 0, lpar);
}
ms_pctlDataGrid->SetSelText(lpcszData);
GlobalUnlock(hData);
return TRUE;
Quote:
}
---------------------------------------------------------
Quote:
> What I should've posted is...
> Debug.Print Mid$(DataGrid1.Text, DataGrid1.SelStart + 1, 1)
> but... I posted past my bed time so..
> --
> Ken Halter
> MS-MVP-VB
> http://www.vbsight.com
> Please respond only to the newsgroups so all can benefit.
> Besides.. I check my email only once a week :-)
> > The character index in the selected cell would be SelStart, so what Ken
> told
> > you was correct, except that the selection need not be changed just to get
> > the character index.
> > > Not exactly,
> > > The snippet that you posted
> > > just changes the length of selection.
> > > What I want is something like:
> > > Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, X As
> > > Single, Y As Single)
> > > Dim iSelLength As Integer
> > > Dim iSelStart As Integer
> > > Dim charIndex As Integer
> > > '---------------
> > > ' call some fn to get charIndex in the clecked cell from x and y
> > > charIndex = somefn(X,Y)
> > > '---------------
> > > ' and use it later , for instance like:
> > > With DataGrid1
> > > iSelLength = .SelLength 'Save
> > > iSelStart = .SelStart
> > > .SelLength = 1 'Set = 1
> > > .SelStart = charIndex
> > > End With
> > > End Sub
> > > If you check help for the message EM_CHARFROMPOS
> > > for edit controls you can understand my question better
> > > Anyway , thank you for your reply
> > > Eugene
> > > > Do you mean... something like...
> > > > '=============
> > > > Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, X
> As
> > > > Single, Y As Single)
> > > > Dim iSelLength As Integer
> > > > With DataGrid1
> > > > iSelLength = .SelLength 'Save
> > > > .SelLength = 1 'Set = 1
> > > > Debug.Print ".SelText="; .SelText 'Show
> > > > .SelLength = iSelLength 'Restore
> > > > End With
> > > > End Sub
> > > > '=============
> > > > --
> > > > Ken Halter
> > > > MS-MVP-VB
> > > > http://www.vbsight.com
> > > > Please respond only to the newsgroups so all can benefit.
> > > > Besides.. I check my email only once a week :-)
> > > > > Hello folks,
> > > > > I use Microsoft Datagrid Control 6.0.
> > > > > I just wonder if it is possible while
> > > > > being inside MouseDown(or Up) handler
> > > > > to find what nearest character was just hit.
> > > > > In other words I want to map X and Y parameters
> > > > > of MouseDown handler to the selected DataGrid cell text
> > > > > char index.
> > > > > There is a way to do that for instance for Text Edit controls
> > > > > using message EM_CHARFROMPOS
> > > > > If I send this message to text edit window
> > > > > with the coordinates of mouse pointer, I get
> > > > > the index of the nearest character to the mouse pointer.
> > > > > Can I do something like that with a cell of datagrid control?
> > > > > I appreciate any help/comment/advice and code sample(not
> neccessarily
> > > > > written on VB, C/C++ is OK too)
> > > > > Eugene