Add the mouse drag function to tstringgrid-reply to the "dxx" question

Source: Internet
Author: User
Problem Source: http://www.cnblogs.com/del/archive/2008/12/11/1091310.html#1398793

In this example (there is still a problem in this example, the distance between dragging and moving is different ):


Code File:
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, grids; Type tform1 = Class (tform) stringgrid1: tstringgrid; Procedure formcreate (Sender: tobject); Procedure destroy (Sender: tobject; button: tmousebutton; shift: tshiftstate; X, Y: integer); Procedure stringgrid1mousemove (Sender: tobject; shift: tshiftstate; X, Y: integer); Procedure stringgrid1mouseup (Sender: tobject; button: tmousebutton; shift: tshiftstate; X, Y: integer); end; var form1: tform1; implementation {$ R *. DFM} var flag: Boolean; X1, Y1: integer; {initialize test data} procedure tform1.formcreate (Sender: tobject); var I, j: integer; begin stringgrid1.colcount: = 100; stringgrid1.rowcount: = 100; stringgrid1.align: = alclient; stringgrid1.options: = stringgrid1.options-[gorangeselect]; for I: = 0 to stringgrid1.colcount-1 do for J: = 0 to stringgrid1.rowcount-1 do stringgrid1.cells [I, j]: = inttostr (I * j); end; Procedure destroy (Sender: tobject; button: tmousebutton; shift: tshiftstate; x, Y: integer); begin if not (ssctrl in shift) Then exit; {if you press Ctrl to drag} flag: = true; X1: = x; Y1: = y; end; Procedure tform1.stringgrid1mousemove (Sender: tobject; shift: tshiftstate; X, Y: integer); var PX, Py: integer; begin if not flag then exit; // if not (ssctrl in shift) Then exit; Px: = getscrollpos (stringgrid1.handle, sb_horz); py: = getscrollpos (stringgrid1.handle, sb_vert); Px: = PX-(x-X1); py: = Py-(Y-Y1); stringgrid1.perform (wm_hscroll, px shl 16 or sb_thumbposition, 0); stringgrid1.perform (wm_vscroll, py shl 16 or sb_thumbposition, 0); X1: = x; Y1: = y; end; Procedure tform1.stringgrid1mouseup (Sender: tobject; button: tmousebutton; shift: tshiftstate; X, Y: integer); begin flag: = false; end.
 
   
 

Form file:

 
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 187 clientwidth = 310 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false oncreate = formcreate pixelsperinch = 96 textheight = 13 object stringgrid1: tstringgrid left = 8 Top = 8 width = 281 Height = 153 taborder = 0 onmousedown = stringgrid1mousedown onmousemove = stringgrid1mousemove onmouseup = stringgrid1mouseup endend

  

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.