TextBox emulation Drag pick text

Source: Internet
Author: User
Tags relative

We know rich text or Word or VB programming environment, you can move the mouse to select the text press mouse left to do the function of dragging and moving, then think, the TextBox can do? This is really a lot of pain, this program simulates its approach, but the main spirit is in the understanding of the TextBox.

First, after a text is selected in a textbox, we simply press Mosue to invalidate the area of the select and may go to another select field, so the first thing is how to select a region, The practice of this action is in MouseUp check if there is no choice of text, if there is, the use of subclass technology, intercept the mouse left button, so when we left, no longer have the choice of text and disappeared.

Second, we did not press mouse, then how to know where mouse is in the end is a textbox of which word, fortunately em_charfrompos this message can be sent to the textbox, its return value hiword the character is in the first few lines 0 as base, The Lowword is the position of the character in the textbox (including the newline and linefeed), so we can single

By MouseMove then know when mouse if arrow number, when is the default I-shaped mouse. Of course I want to know that mouse can be located through the x, y coordinates of the mouse event, but they are twips, and you can also use GetCursorPos () to know the location of the mouse, but this is relative to the screen, Emcharfrompos's message needs to be relative to the textbox.

Coordinates, there are many ways to do this conversion, but I choose screentoclient () This API, more direct.

Third, how does caret hide? Use HideCaret to complete, but this function can only be called once, so that the next ShowCaret () when the caret show out, this is because call 2 times HideCaret, also call 2 times ShowCaret to make caret appear. Another set of caret Setcaretpos () API just makes caret appear in what, but if you keyin any word, still appear in the original place, but not just set up place, but to use em_ SetSel message to set the position of the keyin is the caret position.

Another obtains the first charindex character in the textbox, the position of the mouse in the textbox (the upper-left corner of the textbox is the origin).

pos = SendMessage (hwnd, Em_posfromchar, CHARINDEX, 0)

my = pos \ 2 ^ y coordinates

MX = pos Mod 2 ^ x coordinates

The emphasis of this program is on what is written above, and the others are painstaking

' In the following. Bas

' Note: This program is to use a variable to save caret is hidden, because: when HideCaret () call n times

' You have to call N times ShowCaret () to recover, and vice versa, so in the program, use a variable to confirm the Hide/show

' The action is only done once

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.