首页  编辑  

通过Graphics对象获取它所属的Control

Tags: /C#/API调用/   Date Created:

using System . Runtime . InteropServices;

[ DllImport ( "user32.dll" )]

public static extern IntPtr WindowFromDC( IntPtr hDC);        

private void button1_Click( object sender , EventArgs e)

{

    Graphics vGraphics = Graphics . FromHwnd(Handle);

    Control vControl = Control . FromHandle(WindowFromDC(vGraphics . GetHdc()));

    if (vControl != null ) MessageBox . Show(vControl . Name);

}