Transparent control not capturing mouse events - wpf

I have a window with a transparent background (not null). Inside that window, I have a user control, also with a transparent background.
The window receives mouse events, but the user control does not.
If I change the background of the user control from Transparent to #01000000, then the user control starts to receive mouse events. However, controls hosted within the user control (which are themselves visible) never receive mouse events, regardless of the user control's background.
Any ideas?

There must be something handling the event or you have some configuration issue, since it works ok for me: http://share.linqpad.net/ijx3vb.linq. (Get Linqpad free.)
var t = new TextBlock() { Text = "test" };
var uc1 = new UserControl() { Background = new SolidColorBrush(Colors.Transparent) };
uc1.MouseEnter += (s, args) => { t.Text = "UC1"; };
var uc2 = new UserControl() { Background = new SolidColorBrush(Color.FromArgb(01, 00, 00, 00)) };
uc2.MouseEnter += (s, args) => { t.Text = "UC2"; };
var g = new Grid();
g.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
g.RowDefinitions.Add(new RowDefinition());
g.RowDefinitions.Add(new RowDefinition());
g.Children.Add(t);
g.Children.Add(uc1);
g.Children.Add(uc2);
Grid.SetRow(t, 0);
Grid.SetRow(uc1, 1);
Grid.SetRow(uc2, 2);
PanelManager.DisplayWpfElement(g);

Related

WPF: Move control into new window

I have a control that placed into a custom panel. Into view model exist a boolean variable IsStandAlone. I wish when IsStandAlone is true that this control will be in new window.
I do this on setting IsStandAlone to true:
var window = new Window();
window.Content = this;
window.Closed += (s, ea) =>
{
window.Content = null;
ViewModel.IsStandAlone = false;
}
window.Show();
It's work good when i set IsStandAlone true but when i close the window control doesn't appear in the panel.
Like this?
...
If(IsStandAlone){
var newWindow = new MyNewWindow();
newWindow.Show();
}
...

How do I make a Panel respond to Tap events but not allow the TextBox to be edited?

I have the following code:
public class IdentityCell : Panel
{
public IdentityCell()
{
Margin = m_zeroThickness;
VerticalAlignment = VerticalAlignment.Top;
Width = double.NaN;
m_labelIcon = new Image();
{
m_labelIcon.VerticalAlignment = VerticalAlignment.Center;
m_labelIcon.Width = getIconSizeToUseInPixels();
m_labelIcon.Height = getIconSizeToUseInPixels();
}
Children.Add(m_labelIcon);
m_labelName = new TextBox();
{
m_labelName.Margin = m_zeroThickness;
m_labelName.VerticalAlignment = VerticalAlignment.Center;
m_labelName.TextAlignment = TextAlignment.Center;
m_labelName.FontSize = (double)
Application.Current.Resources["PhoneFontSizeMediumLarge"];
m_labelName.Padding = m_zeroThickness;
m_labelName.IsHitTestVisible = false;
m_labelName.BorderThickness = m_zeroThickness;
m_labelName.Width = double.NaN;
m_labelName.Foreground = m_phoneForegroundBrush;
m_labelName.Background = null;
}
Children.Add(m_labelName);
IsHitTestVisible = true;
Tap += onTap;
}
...
}
When the m_labelIcon is tapped, onTap() is called. But when m_labelName is tapped, onTap() is NOT called. If I set m_labelName.IsHitTestVisible to true, tapping on m_labelName causes the control to enter edit mode and the onscreen keyboard to popup. m_labelName is supposed to just be a static text control without editing support. I've tried using the IsEnabled or IsReadOnly properties, but these alter the appearance.
My TextBox is next to my image. The layout looks like this:
---- Panel --------------------
<Image> < TextBox >
---- Panel --------------------
Any help would be appreciated!
Thanks
If m_labelName is supposed to be just a static text control without editing support, try using a TextBlock instead.
see TextBlock Class on msdn.

Change border color of Windows Forms Control on focus

Is there a way to change a border color of some common controls in Windows Forms (TextBox, ComboBox, MaskedTextBox, ...) when they are in focus? I would like to achieve that in my dialog, so when control is in focus it's border becomes blue?
I suggest to draw a rectangle around the active control as the following:
I need a method to get all the controls which in the form, even which they're in nested Panel or GroupBoxe.
The method:
// Get all controls that exist in the form.
public static List<Control> GetAllControls(IList controls)
{
List<Control> controlsCollectorList = new List<Control>();
foreach (Control control in controls)
{
controlsCollectorList.Add(control);
List<Control> SubControls = GetAllControls(control.Controls);
controlsCollectorList.AddRange(SubControls);
}
return controlsCollectorList;
}
Then.. Drawing functionality..
The code:
public Form1()
{
InitializeComponent();
// The parents that'll draw the borders for their children
HashSet<Control> parents = new HashSet<Control>();
// The controls' types that you want to apply the new border on them
var controlsThatHaveBorder = new Type[] { typeof(TextBox), typeof(ComboBox) };
foreach (Control item in GetAllControls(Controls))
{
// except the control if it's not in controlsThatHaveBorder
if (!controlsThatHaveBorder.Contains(item.GetType())) continue;
// Redraw the parent when it get or lose the focus
item.GotFocus += (s, e) => ((Control)s).Parent.Invalidate();
item.LostFocus += (s, e) => ((Control)s).Parent.Invalidate();
parents.Add(item.Parent);
}
foreach (var parent in parents)
{
parent.Paint += (sender, e) =>
{
// Don't draw anything if this is not the parent of the active control
if (ActiveControl.Parent != sender) return;
// Create the border's bounds
var bounds = ActiveControl.Bounds;
var activeCountrolBounds = new Rectangle(bounds.X - 1, bounds.Y - 1, bounds.Width + 1, bounds.Height + 1);
// Draw the border...
((Control)sender).CreateGraphics().DrawRectangle(Pens.Blue, activeCountrolBounds);
};
}
}
Good luck!

How to show and hide the Popup?

void Start()
{
System.Windows.Controls.Primitives.Popup p = new System.Windows.Controls.Primitives.Popup();
p.HorizontalOffset = this.ActualWidth / 2;
p.Width = 100;
p.Height = 100;
p.VerticalOffset = this.ActualHeight / 2;
DockPanel dock = new DockPanel();
dock.Children.Add(new Button() { Content = "Обновлено" });
p.Child = dock;
p.IsOpen = true;
Thread t = new Thread(StopPopup);
t.Start(p);}
function:
private void StopPopup(object obj)
{
try
{
System.Windows.Controls.Primitives.Popup p = (System.Windows.Controls.Primitives.Popup)obj;
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{
dataGrid1.DataContext = DataSetCreator.AllItems();
Thread.Sleep(1500);
p.IsOpen = false;
}));
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
but why this code is triggered once = (
}
It looks like you're trying to show a control from a non-UI thread and not connecting it to the Application UI in any way (as far as I can see here). WPF UI elements need to created and manipulated on the UI thread and need to be associated with some Window based control in order to be rendered.
In addition to what John said, I would suggest looking at a Modeless dialog box. When you call Show() on the dialog box, the method returns immediately. This allows the application to continue instead waiting for a response from the dialog. You can also attach to button's click event so you know when the button is clicked.

Using IVMRWindowlessControl to display video in a Winforms Control and allow for full screen toggle

I've recently switched from using the IVideoWindow interface to IVMRWindowlessControl in my custom Winforms control to display video.
The reason for this was to allow zoom capabilities on the video within the control.
However in switching over, I've found that the FullScreen mode from IVideoWindow is not available and I am currently trying to replicate this using the SetVideoWindow() method.
I'm finding that I size the video in my control to be at the same resolution as the screen however I can't get the control to position itself to the top/left of the screen and become the top most window.
Any ideas on how to achieve this since the IVideoWindow::put_FullScreenMode just did it all for you?
Resolved the FullScreen problem by hosting the video control in a fresh form which I resized to the size of the current screen, then handled the 'Escape' key press in the form, to toggle back to the normal size video. Here's an extract of the code:-
Members
private Rectangle fullScreenRectangle;
private bool fullScreen;
private Form fullScreenForm;
private Control fullScreenParent;
Toggle FullScreen code
/// <summary>
/// Toggle Full Screen Mode
/// </summary>
public bool FullScreen
{
get
{
return this.fullScreen;
}
set
{
this.fullScreen = value;
if (this.fullScreen)
{
// If switch to full screen, save the current size of the control
this.fullScreenRectangle = new Rectangle(this.Location, this.Size);
// Get the current screen resolution and set that to be the control's size
Rectangle screenRect = Screen.GetBounds(this);
// Create a new form on which to host the control whilst we go to full screen mode.
this.fullScreenForm = new Form();
this.fullScreenForm.Location = PointToScreen(new Point(0, 0));
this.fullScreenForm.Size = new Size(screenRect.Width, screenRect.Height);
this.fullScreenForm.BackColor = Color.Black;
this.fullScreenForm.ShowInTaskbar = false;
this.fullScreenForm.ShowIcon = false;
this.fullScreenForm.FormBorderStyle = FormBorderStyle.None;
this.fullScreenForm.KeyPreview = true;
this.fullScreenForm.PreviewKeyDown += new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenParent = this.Parent;
this.fullScreenForm.Controls.Add(this);
this.fullScreenForm.Show();
this.windowlessControl.SetVideoPosition(null, screenRect);
}
else
{
// Revert to the original control size
this.Location = PointToScreen(new Point(this.fullScreenRectangle.Left, this.fullScreenRectangle.Top));
this.Size = new Size(this.fullScreenRectangle.Width, this.fullScreenRectangle.Height);
this.windowlessControl.SetVideoPosition(null, this.fullScreenRectangle);
if (this.fullScreenForm != null)
{
this.fullScreenForm.Controls.Remove(this);
if (this.fullScreenParent != null)
this.Parent = this.fullScreenParent;
this.fullScreenForm.PreviewKeyDown -= new PreviewKeyDownEventHandler(fullScreenForm_PreviewKeyDown);
this.fullScreenForm.Close();
}
}
}
}
void fullScreenForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
var viewer = this.Controls[0] as ViewerControl;
if (viewer != null)
viewer.FullScreen = false;
}
}

Resources