Page 1 of 1

How can I get caret position under Microsoft Word

Posted: Sun Dec 18, 2011 6:09 pm
by dashunbaba
Aloha wonderful PureBasic people,

I am a Autohotkey user and I am having trouble getting the caret position (the typing position) under Microsoft Word when some text is selected.

The funny thing is that unlike in other Word processing programs, the caret stops blinking when you select some text under Microsoft Word! I am looking into the API to find out a possible solution but I am just too dumb to get things work.

http://msdn.microsoft.com/en-us/library/ff468799.aspx

BTW, I have downloaded the trial version of PureBasic and read through the guides and the tutorial at http://bluez.home.xs4all.nl and I am fascinated. And I read somewhere that people here are just gorgeous!

Thank you so much!

Dallas

Re: How can I get caret position under Microsoft Word

Posted: Sun Dec 18, 2011 6:20 pm
by Kiffi
@dashunbaba: Generally it's possible to automate Word-Objects
(with COMate) but not with the trial version of PureBasic because
with it you have no native access to the Windows API.

Greetings ... Kiffi

Re: How can I get caret position under Microsoft Word

Posted: Sun Dec 18, 2011 7:02 pm
by dashunbaba
Thank you for answering Kiffi.

I tried to use the api under Autohotkey:-) I am coding a commercial program that sells quite nicely and I have been looking for a tool that I can use to write dlls and I found it here!

If I can solve this caret issue with PB I will buy a copy immediately.

It seems quite challenging though. This C# code doesn't work either:

http://www.codeproject.com/KB/dialog/CaretPosition.aspx

Again it works when you don't select any text but loses the caret immediately when you select some text.

Initially I tried use to timer to check every half second where the caret is so that I can get a approximate position when some text is selected. It worked but it makes double click to select under word unusable.

I tried the COM solution too but with no success. (With Com I can get correct positions relative to the page border or text border, but not to the window border! I cannot get the position of the page border on the screen.)

Dallas

Re: How can I get caret position under Microsoft Word

Posted: Sun Dec 18, 2011 7:08 pm
by dashunbaba
I am thinking maybe MS Word hides the blinking cursor when some text is selected for cosmetic reasons. If I can use API to show the hidden cursor and then use GetCursorPos to get the position...

On Ms API page I found these APIs. Can anybody make at least "ShowCaret" work? Thanks again!

Dallas

CreateCaret
DestroyCaret
GetCaretBlinkTime
GetCaretPos
HideCaret
SetCaretBlinkTime
SetCaretPos
ShowCaret

Re: How can I get caret position under Microsoft Word

Posted: Sun Dec 18, 2011 7:48 pm
by dashunbaba
Hi everyone,

I searched and found that HideCaret and ShowCaret will work if WndProc is overwritten. Otherwise they will return an error of "5: access denied."

I found two examples in VB.net and C#. Can anybody help make HideCaret and ShowCaret work using PB?

Dallas


Public Class MyTextBox
Inherits TextBox
<DllImport("user32.dll", EntryPoint:="HideCaret")> Public Shared Function HideCaret(ByVal hwnd As IntPtr) As Boolean
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
HideCaret(Me.Handle)
End Sub
End Class


Here is the conversion result:
public class MyTextBox : TextBox
{

[DllImport("user32.dll", EntryPoint = "HideCaret")]
public static extern bool HideCaret(IntPtr hwnd);


protected override void WndProc(ref System.Windows.Forms.Message m)
{

base.WndProc(m);


HideCaret(this.Handle);
}

}

Re: How can I get caret position under Microsoft Word

Posted: Sun Dec 18, 2011 9:10 pm
by dashunbaba

Code: Select all

Private Declare Function HideCaret Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowCaret Lib "user32" (ByVal hwnd As Long) As Long

Private Sub Text1_GotFocus()
'MsgBox Text1.hwnd
HideCaret Text1.hwnd
End Sub

The above code works! So it seems that if we can get the hwnd of current control and then use HideCaret, it will work.


dashunbaba wrote:Hi everyone,

I searched and found that HideCaret and ShowCaret will work if WndProc is overwritten. Otherwise they will return an error of "5: access denied."

I found two examples in VB.net and C#. Can anybody help make HideCaret and ShowCaret work using PB?

Dallas


Public Class MyTextBox
Inherits TextBox
<DllImport("user32.dll", EntryPoint:="HideCaret")> Public Shared Function HideCaret(ByVal hwnd As IntPtr) As Boolean
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
HideCaret(Me.Handle)
End Sub
End Class


Here is the conversion result:
public class MyTextBox : TextBox
{

[DllImport("user32.dll", EntryPoint = "HideCaret")]
public static extern bool HideCaret(IntPtr hwnd);


protected override void WndProc(ref System.Windows.Forms.Message m)
{

base.WndProc(m);


HideCaret(this.Handle);
}

}