Re: [問題] 請問 這迴圈在哪?
※ 引述《charles0203 (Mango)》之銘言:
: 最近在學API
: API的GetWindowText
: 這程式大概是 根據視窗title 來找到 該視窗介的控件
: 在表單上 建立一個button 就好了
: 我不懂的是 究竟是怎樣產生迴圈效果
: 讓他可以不斷執行 後面這行:
: Console.WriteLine(" Contains text of contro:"+ editText);
: 程式是用XP的 音量控制 來測試的
: 程式碼:
: using System;
: using System.Text;
: using System.Windows.Forms;
: using System.Runtime.InteropServices;
: namespace user32_api_test1
: {
: public partial class Form1 : Form
: {
: [DllImport("User32.dll")]
: public static extern Int32 FindWindow(String lpClassName, String
: lpWindowName);
: [DllImport("User32.dll")]
: public static extern Boolean EnumChildWindows(int hWndParent,
: Delegate lpEnumFunc, int lParam);
: [DllImport("User32.dll")]
: public static extern Int32 GetWindowText(int hWnd, StringBuilder s,
: int nMaxCount);
: [DllImport("User32.dll")]
: public static extern Int32 GetWindowTextLength(int hwnd);
: [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
: public static extern int GetDesktopWindow();
: int hWnd;
: public delegate int Callback(int hWnd,int lParam);
: public Form1()
: {
: InitializeComponent();
: }
: private void button2_Click(object sender, EventArgs e)
: {
: }//
: private void button1_Click(object sender, EventArgs e)
: {
: Callback myCallBack = new Callback(EnumChildGetValue);
: hWnd = FindWindow(null, "主音量");
: if(hWnd == 0)
: {
: MessageBox.Show("Please Start Calling Window Application");
: }
: else
: {
: EnumChildWindows(hWnd,myCallBack,0);
: }
: }
: public int EnumChildGetValue(int hWnd,int lParam)
: {
: StringBuilder formDetails = new StringBuilder(256);
: int txtValue;
: string editText="";
: txtValue =GetWindowText(hWnd,formDetails,256);
: editText = formDetails.ToString().Trim();
: Console.WriteLine(" Contains text of contro:"+ editText);
: return 1;
: }
: }//
: }
: Thanks in advance for any help
調用EnumChildWindows這個API的時候
直到調用到最后一個子窗體或者回調函數返回false
否則會一直枚舉下去
所以除非想列舉所有子窗體
否則,如果找子窗體中的其中一個
可以在回調函數EnumChildGetValue中做判斷
如果相等,則返回0,這樣枚舉方法就中止不再枚舉之后的子窗體
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.202.244.1
推
08/18 17:57, , 1F
08/18 17:57, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):