c# sendmessage control to scroll

Declare some constants:
private const int WM_SCROLL = 276; // Horizontal scroll
private const int WM_VSCROLL = 277; // Vertical scroll
private const int SB_LINEUP = 0; // Scrolls one line up
private const int SB_LINELEFT = 0;// Scrolls one cell left
private const int SB_LINEDOWN = 1; // Scrolls one line down
private const int SB_LINERIGHT = 1;// Scrolls one cell right
private const int SB_PAGEUP = 2; // Scrolls one page up
private const int SB_PAGELEFT = 2;// Scrolls one page left
private const int SB_PAGEDOWN = 3; // Scrolls one page down
private const int SB_PAGERIGTH = 3; // Scrolls one page right
private const int SB_PAGETOP = 6; // Scrolls to the upper left
private const int SB_LEFT = 6; // Scrolls to the left
private const int SB_PAGEBOTTOM = 7; // Scrolls to the upper right
private const int SB_RIGHT = 7; // Scrolls to the right
private const int SB_ENDSCROLL = 8; // Ends scroll

Add a using reference
using System.Runtime.InteropServices;

Declare external SendMessage:
[DllImport("user32.dll",CharSet=CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg,IntPtr wParam, IntPtr lParam);

Usage:
If you have some textbox on the form...

SendMessage( Control Handle , WM Scroll Message, (IntPtr) Scroll Command ,IntPtr.Zero);

Scroll page up
SendMessage(textBox1.Handle,WM_VSCROLL,(IntPtr)SB_PAGEUP,IntPtr.Zero);

Scroll page down
SendMessage(textBox1.Handle,WM_VSCROLL,(IntPtr)SB_PAGEDOWN,IntPtr.Zero);

The DataGridView control has 2 sub-controls, a VScrollBar and a HScrollBar. If you want to do a scroll over a DataGridView you have to send the WM_VSCROLL or WM_HSCROLL message to the DataGridView handle, with wParam as the SB_xxxx value you want, and lParam as the scrollbar handle.

Example: 
public void ScrollControlDown(DataGridView dataGridView) 

VScrollBar barraVertical = null;

foreach (Control c in dataGridView.Controls) 

if (c is VScrollBar) barraVertical = (VScrollBar)c; 
}

if (barraVertical != null) 

SendMessage(dataGridView.Handle, WM_VSCROLL, (IntPtr)SB_LINEDOWN, barraVertical.Handle); 
SendMessage(dataGridView.Handle, WM_VSCROLL, (IntPtr)SB_ENDSCROLL, barraVertical.Handle); 

}

This method requires the DataGridView‘s ScrollBars property shows the scrollbar over you are going to do the programatical scroll. If you want to send WM_VSCROLL, ScrollBars property must be set to Both or Vertical. If you want to send WM_HSCROLL, ScrollBars propery must be set to Both or Horizontal.

时间: 2024-10-24 05:18:38

c# sendmessage control to scroll的相关文章

A Complete ActiveX Web Control Tutorial

? Introduction ActiveX is a Microsoft technology developed in the mid 90's, that allows for the creation of applet-like applications that can be downloaded and run within Microsoft's Web browser. This article is intended for Visual C++ developers who

【自动化__GUI自动化】__java__Windows应用程序识别__计算器

一.代码如下 package www.woniu.gui.one; import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.aw

用win32API 实现TextBox水印特效

demo效果: (1)创建一个SetWaterText静态类         需要添加的两个引用:using System.Runtime.InteropServices;using System.Windows.Forms; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System

Chrome-Console( Command Line API Reference)

来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference The Command Line API contains a collection of convenience functions for performing common tasks: selecting and inspecting DOM elements, displaying data in read

优雅且高效的使用Chrome Developer Tools(上)

Abstract:Chrome Developer Tools是前端开发不可缺少的利器,这里介绍几个有趣的Command Line API,使得开发调试过程中,不经意的透露出那点点的优雅. 主要内容: 记录ex的 $_ 记录现任的 $0 简洁的 $(selector) 和 $$(selector) 拷贝Object的 copy(Object) 更优雅的打断点 debug(function) 列出对象的所有属性 dir(objcet) 和 列出事件的getEventListeners(object

C# FolderBrowserDialog Scroll to Selected Path 文件夹选择框初始位置没有滚动到默认文件夹

public static class FolderBrowserLauncher { /// <summary> /// Using title text to look for the top level dialog window is fragile. /// In particular, this will fail in non-English applications. /// </summary> const string _topLevelSearchString

[.NET]让Panel对Mouse滚轮事件(Wheel)有感觉,而且能触发Scroll事件

之前有写了一篇"让Panel对Mouse滚轮事件(Wheel)有感觉",是透过 SendMessage 的方式去叫 Panel Scroll. 但是却不会触发Panel的Scroll事件.那怎么办呢? 在之前有写了一篇"让Panel对Mouse滚轮事件(Wheel)有感觉",是透过 SendMessage 的方式去叫 Panel Scroll. 但是却不会触发Panel的Scroll事件.那怎么办呢? 所以我们可以透过Control.FromHandle来取到那个控

Scroll View 深入

转载自:http://mobile.51cto.com/hot-430409.htm 可能你很难相信,UIScrollView和一个标准的UIView差异并不大,scroll view确实会多一些方法,但这些方法只是UIView一些属性的表面而已.因此,要想弄懂UIScrollView是怎么工作之前,你需要了解 UIView,特别是视图渲染过程的两步. 光栅化和组合 渲染过程的第一部分是众所周知的光栅化,光栅化简单的说就是产生一组绘图指令并且生成一张图片.比如绘制一个圆角矩形.带图片.标题居中的

How To Use Git Source Control with Xcode in iOS 7

Note from Ray: Tutorial Team member Felipe Laso Marsetti has ported this tutorial to iOS 7 as part of the iOS 7 feast. We hope you enjoy! Whether you're a solo developer or working on a team, if you're not using source control for your projects, you