简体   繁体   中英

c# Access control in a native c++ application

I have to access a custom control in a native c++-mfc-application. I need to read the content of the control. The control consists of two buttons and a label between the buttons. The label contains a month and a year and with the buttons i can navigate one month back or in the future. Is there a way to read the text in this control and to access the two buttons?

I'm already able to access the window where the controls are in. Therefor i am using a framework provided by my company. But with this framework it's not possible to get the text and the buttons in the mentioned control.

You'd have to access the controls by their window handle. So you'd have to obtain the window handle of the native application's window, then try to find the window handles of the label and the buttons ( FindWindow WinAPI function). Then you could send respective window messages to the windows to obtain the text or "push" the buttons.

You need to use FindWindow and GetWindowText from win32 API (from pinvoke ):

FindWindow :

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter:

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

You can also call FindWindow(default(string), lpWindowName) or FindWindow((string)null, lpWindowName)

and GetWindowText :

(from msdn) Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM