日期:2014-05-17  浏览次数:20939 次

有谁用过GetClassName这个API函数(C#中的字符串指针该如何处理)
int WINAPI GetClassName(
  _In_   HWND hWnd,
  _Out_  LPTSTR lpClassName,
  _In_   int nMaxCount
);
这个API的第二个参数要的是一个字符串类型的指针,C#中好像没有字符串类型的指针,我是个新手,求高人指导,小弟不胜感激!
------最佳解决方案--------------------
用sb


------其他解决方案--------------------
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,int nMaxCount);


------其他解决方案--------------------
引用:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,int nMaxCount);

高手,问你一个问题,我是用这个来获取控件的类型的,程序的效果是当我鼠标点击控件时首先获取控件的句柄,然后就用如上函数获取控件的类名(我想获取文本输入控件的类名),为什么当我点击textbox时获取的类名是windowFo,点击richtextbox也是windowFo,点击窗体和panel也是windowFo,只有点击comboBox时获取的类名才是Edit,这是为什么呢?如下图:
------其他解决方案--------------------
StringBuilder sb = new StringBuilder (256);


------其他解决方案--------------------
引用:
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,int nMaxCount);

高人,再问你一个问题,我是想用这个函数查询控件的类名,程序的效果是,当我点击鼠标左键时首先获取控件的句柄,然后再用这个函数根据控件的句柄查出控件的类名,为什么当我点击窗体,textbox,richtextbox,panel时获取的类名都是windowFo,而只有comboBox获取的才是Edit呢,这是为什么呢???(我想获取文本输入控件)
------其他解决方案--------------------
引用:
StringBuilder sb = new StringBuilder (256);

256长度还是不行。。。。。
------其他解决方案--------------------
引用:
StringBuilder sb = new StringBuilder (256);

我错了,有一个地方没给长度。。。。改过来了,好了。。。。
------其他解决方案--------------------
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll")]
        static extern IntPtr WindowFromPoint(Point Point);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetCursorPos(out Point lpPoint);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);