使用C#和WIN32滚动记事本记事本

2023-09-04 04:21:25 作者:社会不惯人

我试图滚动使用C#应用程序中的记事本窗口。有关code块是低于,调用移动/调整窗口大小的作品,所以我知道这个句柄有效

请你能看到这个的时候运行什么,我缺什么也不会发生。

  [国旗]
    公共枚举SetWindowPosFlags:UINT
    {
        SWP_ASYNCWINDOWPOS = 0x4000的,
        SWP_DEFERERASE =为0x2000,
        SWP_DRAWFRAME = 0×0020,
        SWP_FRAMECHANGED = 0×0020,
        SWP_HIDEWINDOW = 0x0080,会
        SWP_NOACTIVATE = 0×0010,
        SWP_NOCOPYBITS = 0100,
        SWP_NOMOVE = 0×0002,
        SWP_NOOWNERZORDER = 0x0200,
        SWP_NOREDRAW =×0008,
        SWP_NOREPOSITION = 0x0200,
        SWP_NOSENDCHANGING =的0x0400,
        SWP_NOSIZE = 0×0001,
        SWP_NOZORDER =×0004,
        SWP_SHOWWINDOW = 0x0040,
    }


    私人const int的WM_SCROLL = 276; //水平滚动
    私人const int的WM_VSCROLL = 277; //垂直滚动
    私人const int的SB_LINEUP = 0; //滚动一行向上
    私人const int的SB_LINELEFT = 0; //滚动一个单元格离开
    私人const int的SB_LINEDOWN = 1; //滚动下一行
    私人const int的SB_LINERIGHT = 1; //滚动一个单元的权利
    私人const int的SB_PAGEUP = 2; //滚动一页
    私人const int的SB_PAGELEFT = 2; //滚动一页左
    私人const int的SB_PAGEDOWN = 3; //翻一页
    私人const int的SB_PAGERIGTH = 3; //滚动一页权
    私人const int的SB_PAGETOP = 6; //滚动到左上
    私人const int的SB_LEFT = 6; //滚动到左边
    私人const int的SB_PAGEBOTTOM = 7; //滚动到右上
    私人const int的SB_RIGHT = 7; //滚动向右
    私人const int的SB_ENDSCROLL = 8; //结束滚动


    [的DllImport(user32.dll中,字符集= CharSet.Auto)
    私人静态外部的IntPtr SendMessage函数(IntPtr的的HWND,诠释消息,IntPtr的wParam中,IntPtr的lParam的);

    [的DllImport(user32.dll中)
    [返回:的MarshalAs(UnmanagedType.Bool)
    静态的extern BOOL SetWindowPos(IntPtr的的HWND,IntPtr的hWndInsertAfter,INT X,INT Y,INT CX,INT CY,SetWindowPosFlags和uFlags);



    公共无效滚动()
    {


        IntPtr的处理= IntPtr.Zero;

        流程[]进程= Process.GetProcessesByName(记事本);

        的foreach(在工艺过程P)
        {
            处理= p.MainWindowHandle;

            Console.WriteLine(GOT手柄:+ p.MainWindowTitle);

            打破;
        }

        //这是测试我有一个有效的句柄
        SetWindowPos(处理,新的IntPtr(0),10,10,1024,350,SetWindowPosFlags.SWP_DRAWFRAME);


        SendMessage函数(手柄,WM_VSCROLL,(IntPtr的)SB_LINEDOWN,IntPtr.Zero);
        SendMessage函数(手柄,WM_VSCROLL,(IntPtr的)SB_PAGEDOWN,IntPtr.Zero);

    }
 

解决方案 野猫软件榜之文本编辑与对比工具,一键找出2篇文章的不同

这将失败,因为您要发送的WM_VSCROLL消息发送到主窗口。您需要将消息发送到记事本的编辑控件,这与滚动条的窗口。

您可以使用的 EnumChildWindows 。孩子带班编辑是一个你想要的。

I'm trying to scroll a Notepad window using a C# application. The relevant code block is below, the call to move/size the window works so I know the handle is valid

please can you see what I am missing nothing happens when this is run.

    [Flags]
    public enum SetWindowPosFlags : uint
    {
        SWP_ASYNCWINDOWPOS = 0x4000,
        SWP_DEFERERASE = 0x2000,
        SWP_DRAWFRAME = 0x0020,
        SWP_FRAMECHANGED = 0x0020,
        SWP_HIDEWINDOW = 0x0080,
        SWP_NOACTIVATE = 0x0010,
        SWP_NOCOPYBITS = 0x0100,
        SWP_NOMOVE = 0x0002,
        SWP_NOOWNERZORDER = 0x0200,
        SWP_NOREDRAW = 0x0008,
        SWP_NOREPOSITION = 0x0200,
        SWP_NOSENDCHANGING = 0x0400,
        SWP_NOSIZE = 0x0001,
        SWP_NOZORDER = 0x0004,
        SWP_SHOWWINDOW = 0x0040,
    }


    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


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

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);



    public void scroll()
    {


        IntPtr handle = IntPtr.Zero;

        Process[] processes = Process.GetProcessesByName("Notepad");

        foreach (Process p in processes)
        {
            handle = p.MainWindowHandle;

            Console.WriteLine("Got Handle: " + p.MainWindowTitle);

            break;
        }

        //this is to test I have a valid handle
        SetWindowPos(handle, new IntPtr(0), 10, 10, 1024, 350, SetWindowPosFlags.SWP_DRAWFRAME);


        SendMessage(handle, WM_VSCROLL,  (IntPtr)SB_LINEDOWN, IntPtr.Zero);
        SendMessage(handle, WM_VSCROLL, (IntPtr)SB_PAGEDOWN, IntPtr.Zero);

    }

解决方案

This fails because you are sending the WM_VSCROLL message to the main window. You need to send the message to Notepad's edit control, which is the window with the scrollbar.

You can enumerate Notepad's child windows using EnumChildWindows. The child with class "Edit" is the one you want.