System.Windows.Controls.TextBox Handle - c#

I feel like I'm missing something really obvious here.
I know that forms in general have a Handle property, but I am using a System.Windows.Controls.TextBox and it does not have a Handle property.
How do I access the TextBox's handle? I noticed that RichTextBox has the Handle property, so why not the regular TextBox?

You can just call Handle on the TextBox. It is an inherited property from System.Windows.Forms.Control.
Edit: Question was updated to ask about WPF
WPF doesn't use handles like a typical Win32 application or WinForms application. Each control is not its own Window in WPF. You can verify this with Spy++, it cannot differentiate between each control. Therefore you cannot SendMessage to the individual controls like you can with WinForms and Win32 apps.
You can use WindowInteropHelper to get the parent window handle of a WPF window.

Related

C# Windows Forms and XAML

I would like to know if it is possible to combine XAML with Window Form or reverse. Also I would like to know a good place to learn how to work with both of them.
For example I want to make a XAML window on which I want to add a button with an event action wrote in C#.
Yes this is possible (both ways)
WinForms in WPF
WPF in WinForms
In both cases you add a special control that acts as a host for the other environment.
WindowsFormsHost allows you to host a Windows Forms control on a WPF page.

Detect HWND Visibility Change

Is there a way to register a handler to be notified when my application's windows change visibility based on the OS (or anyone else) accessing the HWND directly via something like ShowWindow() with SW_HIDE, etc.?
Assuming your application is WinForms based. Yes, that should be possible by overriding the WndProc of your main Form.
See WndProc MSDN documentation for details.

how to find .Handle of RichTextBox?

in winForms, I have .Handle property, how do I get the equivalent to in WPF?
I tried using WindowInteropHelper but it does not suporte RichTextBox,only Window types.
Thanks in advance.
Controls in WPF do not have associated WinAPI handle, like WinForms control do. WPF uses only one WinAPI window to process window messages from OS and encapsulates it in Window class instance. The best you can do is to navigate through parents of your control until you find a Window and get the handle. As far as I understand this will likely be the same handle as one returned by WindowInteropHeler.
UPD.
To make this answer more useful and out-of-box'y here is some code (not tested though):
IntPtr hwnd = new WindowInteropHelper(Window.GetWindow(userControlRefernce)).Handle;
RichTextBox does not have HWND handle in WPF. You can still use a RichTextBox from WinForms library using WindowsFormsIntegration assembly, if you really need this handle.
Remember that you can always check what elements have or do not have handles using Spy++ utility.

Given a WPF Image control, is it possible to determine its HWND?

I am integrating a webcam in a WPF application. I can see the camera feed in the main window, as I pass its HANDLE on to the DirectShow functions. But this is not what I want.
The main form has a Image control, where I'd like to see the output. However, in order to do this, I need the control's Handle.
Any hint on how to do this?
Thanks in advance,
Gianluca.
An Image Control in WPF, unlike Windows Forms, doesn't actually have an HWND.
WPF works differently than Windows Forms - each control is not a wrapper around a native "window" with a handle, but rather composed together using Direct3D at runtime by the layout system.
If you need to actually host output from a Webcam inside of a WPF window, you should look at using HwndHost (or a subclass). The simplest way is often to just host a Windows Forms control inside of a WindowsFormsHost, though managing an HWND yourself via HwndHost is more efficient.
Using an WindowsFormsHost and a picture box inside is a good solution that I used for a similar problem where I needed a handle to display a video stream. But be careful, in order to work, the Window that is hosting the WindowsFormsHost control must have AllowsTransparency="false"!

Is there a way to prevent WinForms controls in a WPF window from being unstyled?

I have some WinForms controls that I need to use in a WPF window. I'm able to get the controls to show up just fine and everything works as I would expect, but I'm experiencing one issue: all the WinForms controls are unstyled.
I'd like for the WinForms controls to at least use the default OS style (like I would see in a WinForms application). Is there any way to control this, or do I have to live with the controls the way they are?
The WinForms controls can't use the WPF styles, because Windows Forms doesn't understand the WPF styling and templating system.
To get them to use the "OS style" (the OS visual theme), try calling System.Windows.Forms.Application.EnableVisualStyles in your Main method. (I thought WPF handled this automatically, but I guess that's not what you're seeing.) This must be called before any controls get created!

Categories

Resources