C# winfrom datagridview数据获取

C# winfrom datagridview数据获取,第1张

办法三种(限制Winform对Winform *** 作):

数据量轻量级,实时传值。用Remoting,建立类库,编写方法放在类库文件中,编译成dll文件。建立两个项目,引用他,实现remoting *** 作实时传值

数据量,提倡方法,代码贴出

[DllImport("User32dll", EntryPoint = "FindWindow")]

        public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32dll", EntryPoint = "FindWindowEx")]

        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

        [DllImport("User32dll", EntryPoint = "FindEx")]

        public static extern IntPtr FindEx(IntPtr hwnd, IntPtr hwndChild, string lpClassName, string lpWindowName);

        [DllImport("User32dll", EntryPoint = "SendMessage")]

        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);

        [DllImport("user32dll ", EntryPoint = "GetDlgItem")]

        public static extern IntPtr GetDlgItem(IntPtr hParent, int nIDParentItem);

        [DllImport("user32dll", EntryPoint = "GetWindowText")]

        public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);//在事件里写IntPtr maindHwnd = FindWindow(null, "Form1"); //获得句柄   

            int i = 0;

            if (maindHwnd != IntPtrZero)

            {

                MessageBoxShow("找到了窗体!");

                //控件id

                int controlId = 0x000003F4;

                //获取子窗口句柄

                IntPtr EdithWnd = GetDlgItem(maindHwnd, controlId);

                SendMessage(EdithWnd, i, (IntPtr)0, stringFormat("当前时间是:{0}", DateTimeNow)); //赋值没问题,表示句柄正确

                StringBuilder stringBuilder = new StringBuilder(512);

                GetWindowText(EdithWnd, stringBuilder, stringBuilderCapacity);

                MessageBoxShow(stringFormat("取到的值是:{0}", stringBuilderToString()));//取值一直是空字符串

            }

            else

            {

                MessageBoxShow("没有找到窗口");

            }

3 大数据的话还是用数据库吧

mfc中从编辑框中获取数据的函数是UpdateData。

UpdateData() 是MFC的窗口函数,用来刷新数据的。

函数原型 BOOL UpdateData( BOOL bSaveAndValidate = TRUE );

参数说明

布尔型参数bSaveAndValidate:标明那对话框是初始化(FALSE)还是数据恢复(TRUE)。

总的来说: *** 作系统会调用这个函数来初始化对话框中的数据,或者检索或者验证对话框中的数据。

简单说就是:要从编辑框中获取数据就调用 UpdateData(true),要把数据输出给编辑框就掉用 UpdateData(false)

返回值:

成功返回1,否则返回0。

获得窗体的句柄可以用GetWindow方法获得窗体标题用GetWindowText方法,显示隐藏某个窗体用ShowWindow方法,给你举个例子

using SystemRuntimeInteropServices;

private const int WS_VISIBLE = 268435456;//窗体可见

private const int WS_MINIMIZEBOX = 131072;//有最小化按钮

private const int WS_MAXIMIZEBOX = 65536;//有最大化按钮

private const int WS_BORDER = 8388608;//窗体有边框

private const int GWL_STYLE = (-16);//窗体样式

private const int GW_HWNDFIRST = 0;

private const int GW_HWNDNEXT = 2;

private const int SW_HIDE = 0;

private const int SW_SHOW = 5;

[DllImport("User32dll")]

private extern static int GetWindow(int hWnd, int wCmd);

[DllImport("User32dll")]

private extern static int GetWindowLongA(int hWnd, int wIndx);

[DllImport("user32dll")]

private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);

[DllImport("user32dll", CharSet = CharSetAuto)]

private extern static int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32dll")]

public static extern int ShowWindow(int hwnd, int nCmdShow);

//获得包含窗体可见、有边框、有最大化按钮的窗体的句柄和标题(窗体的属性出这几种外还有很多种)

public static List<FromInfo> GetHandleList(int Handle)

{

List<FromInfo> fromInfo = new List<FromInfo>();

int handle = GetWindow(Handle, GW_HWNDFIRST);

while (handle > 0)

{

int IsTask = WS_VISIBLE | WS_BORDER | WS_MAXIMIZEBOX;//窗体可见、有边框、有最大化按钮

int lngStyle = GetWindowLongA(handle, GWL_STYLE);

bool TaskWindow = ((lngStyle & IsTask) == IsTask);

if (TaskWindow)

{

int length = GetWindowTextLength(new IntPtr(handle));

StringBuilder stringBuilder = new StringBuilder(2 length + 1);

GetWindowText(handle, stringBuilder, stringBuilderCapacity);

string strTitle = stringBuilderToString();

if (!stringIsNullOrEmpty(strTitle))

{

fromInfoAdd(new FromInfo(strTitle, handle));

}

else

{

fromInfoAdd(new FromInfo("", handle));

}

}

handle = GetWindow(handle, GW_HWNDNEXT);

}

return fromInfo;

}

//获得所有窗体的句柄和标题

public static List<FromInfo> GetHandleList(int Handle)

{

List<FromInfo> fromInfo = new List<FromInfo>();

int handle = GetWindow(Handle, GW_HWNDFIRST);

while (handle > 0)

{

int length = GetWindowTextLength(new IntPtr(handle));

StringBuilder stringBuilder = new StringBuilder(2 length + 1);

GetWindowText(handle, stringBuilder, stringBuilderCapacity);

string strTitle = stringBuilderToString();

if (!stringIsNullOrEmpty(strTitle))

{

fromInfoAdd(new FromInfo(strTitle, handle));

}

else

{

fromInfoAdd(new FromInfo("", handle));

}

handle = GetWindow(handle, GW_HWNDNEXT);

}

return fromInfo;

}

public class FromInfo

{

public FromInfo(string title, int handle)

{

thistitle = title;

thishandle = handle;

}

private string title;

private int handle;

public string Title

{

get { return title; }

set { title = value; }

}

public int Handle

{

get { return handle; }

set { handle = value; }

}

}

//获得窗体句柄和标题

private void button1_Click(object sender, EventArgs e)

{

List<FromInfo> fromInfo = GetHandleList(thisHandleToInt32());

}

private void button2_Click(object sender, EventArgs e)

{

//隐藏窗体

ShowWindow(thisHandleToInt32(), SW_HIDE);

//显示窗体

ShowWindow(thisHandleToInt32(), SW_SHOW);

}

不明白你在说什么。

文本框的内容可以用GetWindowText获取,如果是对话框,还可用 GetDlgItemText,也可以发送窗口消息 WM_GETTEXT

int GetWindowText(

HWND hWnd, // handle to window or control

LPTSTR lpString, // text buffer

int nMaxCount // maximum number of characters to copy

);

UINT GetDlgItemText(

HWND hDlg, // handle to dialog box

int nIDDlgItem, // control identifier

LPTSTR lpString, // pointer to buffer for text

int nMaxCount // maximum size of string

);

SendMessage(

(HWND) hWnd, // handle to destination window

WM_GETTEXT, // message to send

(WPARAM) wParam, // number of characters to copy

(LPARAM) lParam // text buffer

);

给ComboBox绑定一个变量,如下,不绑定也可以(会比较烦)

 CString strCBText;

 m_comboxGetLBText(m_comboxGetCurSel(), strCBText);

 或

 ((CComboBox)GetDlgItem(IDC_COMBO1))->GetLBText(((CComboBox)GetDlgItem(IDC_COMBO1))->GetCurSel(), strCBText);

var keyword="";//关键字

这样写就可以:

var keyword=documentforms[0]keywordvalue;//关键字

但是,你的这样语句就是页面加载的时候执行一次,因为你写在onLoad里面的,那么当页面加载以后你修改keyword的值是没有效果的。

要能够多次执行,应该这样:

<html><head>

<script language="JavaScript">

function func()

{

var keyword=documentforms[0]keywordvalue;//关键字

var r=documentbodycreateTextRange();

var s='<font style="background-color: #FFFF00;">'+keyword+'</font>';

while(rfindText(keyword)){

for(var o=rparentElement();o&&otagName!="A";o=oparentElement);

if(!o)try{

rpasteHTML(s);

}catch(e){}

rcollapse(false);

}

return false;

}

</script>

</head><body>

<form onSubmit="return func();">

<input name='keyword' type='text' value="">

<input type=submit value='Search'>

</form>

</body></html>

CString str;

CWnd h1;

h1=GetDlgItem(IDC_STATIC1);

h1->SetWindowText(str);

IDC_STATIC1要显示的控件IDC

  List<string> list = new List<string>();

        [DllImport("user32dll")]

        [return: MarshalAs(UnmanagedTypeBool)]

        public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

 

        [DllImport("user32dll")]

        public static extern int GetWindowText(int hWnd, IntPtr lpString, int nMaxCount);

 

        private void toolStripButton5_Click(object sender, EventArgs e)

        {

            listClear();

            EnumChildWindows(thisHandle, thisEnumWindowsMethod, IntPtrZero);

            //这里得到了所有的子窗口listCount;

        }

 

        private bool EnumWindowsMethod(int hWnd, int lParam)

        {

            IntPtr lpString = MarshalAllocHGlobal(200);

            GetWindowText(hWnd, lpString, 200);

            var text = MarshalPtrToStringAnsi(lpString);

            if (!stringIsNullOrWhiteSpace(text))

                listAdd(text);

            return true;

        }

以上就是关于C# winfrom datagridview数据获取全部的内容,包括:C# winfrom datagridview数据获取、mfc中从编辑框中获取数据的函数是什么、c#如何获得系统所有窗口名称(包括隐藏窗口)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9428084.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存