Ownerdrawn Winform Button: Handling AcceptButton cues - c#

I've made my own custom ownerdrawn button control, inherited from the Button class itself. I draw the control all by myself in the OnPaint override procedure, this includes painting the background of the button, drawing its borders, text and image.
I also need to incorporate the capability for the button to show an Active color border in case the button has focus, or is defined as the AcceptButton by the parent form. For focus, I use this.ContainsFocus to know if the button currently has focus.
How to know if my button control is defined as the default AcceptButton on the parent form, so I can draw the active border color around it accordingly? I don't see any inherited property in the Button class to find this out.
Thanks.

Related

Why does my FlowLayoutPanel automatically scroll back to the top when the form loses and regains focus?

I am creating an image browser that uses a FlowLayoutPanel to display thumbnails of images. See animated GIF that shows how I scroll down the panel, switch to another window, and then back to the form which causes the FlowLayoutPanel to scroll back to the top. I can't imagine any reason why it would do this.
Also, I seem to be able set the scroll location by clicking on the panel. When the form loses and regains focus, it scrolls back to the last Y position that I clicked.
Why is it exhibiting this behavior and how can I prevent it from occurring?
The effect described is quite common: when a FlowLayoutPanel contains Controls that can be activated and one of these child Controls is selected at some point (in this case a UserControl, which has the WS_EX_CONTROLPARENT extended style, so SetStyle(ControlStyles.Selectable, false) won't do much) and the FlowLayoutPanel is then scrolled to hide this Control, when the Form is deactivated and then activated again, the ActiveControl is scrolled into view.
This causes the FlowLayoutPanel to scroll to a position where the child ActiveControl is visible.
▶ This doesn't happen when the child Controls are not selectable, as the PictureBox Control, for example. If this Control were used to present the thumbnails (as shown in the question), the FlowLayoutPanel would not scroll.
I think the simplest way to prevent the FlowLayoutPanel from scrolling to the ActiveControl is to set the FlowLayoutPanel itself as the ActiveControl when the Form is deactivated, handling the Deactivate event.
private void form1_Deactivate(object sender, EventArgs e)
{
this.ActiveControl = this.flowLayoutPanel1;
}
This has no meaningful side affects, except the Control that previously was the ActiveControl will raise the Leave event.
It may be also used to suspend some other activity, since the User is now focusing on another Window.
▶ To set the ActiveControl to the default one instead (the Control that is activated when the Form is first shown), set this.ActiveControl = null;. It will be reset when the Form is activated again.
I've seen sometimes the Activated and Deactivate events used to disable and enable back a ContainerControl: this also prevents the scrolling, of course, but may cause unwanted cascading effects when child Controls are disabled.
But it may also be something expected and possibly desired. It depends on what happens behind the scene (implementation details).
The solution proposed by #Loathing in comments can also work, deriving a Custom Control from FlowLayoutPanel. It depends on the use case.
Stop form from scrolling when moving controls

How to set Label or userControl is real transparent

Environment: Visual studio VB or C#
I have some problem about layer of control. My user control has a panel it's set color to be transparent. In main form I create new instant of user control .But a panel of user control is not transparent.
Ok,a background of user control change to same a background of a main form.
but when I use Ctrl.bringToFront()
a panel of user control will cover all of existing control on the form.
Below image, I add user control to main form. MainForm has green panel. all of them is cover by user control.
I try to use a label control. it has a same problem.
According to third image a label should be blue and red color.
What should i do? if my user control is not rectangle. In my user control,I must use panel for combine many control.it' easy for move or resize.

how to create transparent label in toolbox in winforms

I have a System.Windows.Forms.ToolStrip which contains a Label.
The ToolStrip has a special fancy background. But the label is just gray.
It's not possible to use transparency because the parent of ToolStrip is a form. Also it's not possible to change the parent, because the collection of Controls in ToolStrip is read only.
Is it possible to create a Label that is transparent and which has a ToolStrip parent?
Give the following a shot.
Set the backcolor of the label to Color.Transparent.
Then you have to add the label directly to the control collection of the control of which you want the label to appear transparent on top of.
If you just delete the assigned background color of the ToolStripLabel then it ought to inherit the background of its parent ToolStrip. I use a custom ToolStripRenderer to draw a customized background for tool strips, and my labels do not need any special handling in order to inherit the parent's background. Just make sure you aren't trying to assign a background color.

Responding to keypress and mouse click on fully-transparent panel

I've created a form that's maximized over the entire screen.
Within that form, and sized to fill the entire form, I've placed a panel with background color set to Red. The form's TransparencyKey is set to Red.
Therefore, the Panel is like a "keyhole" - you can see the desktop that's directly underneath it.
When the user clicks on the panel, OR, presses a key on the keyboard, I want to take action.
But, because the panel is completely transparent, when it is clicked or a key is pressed, nothing happens. If I make the panel non-transparent (setting it's background color to Blue, for example), it does respond to clicks.
What's the best way to get the panel to respond to clicks and keypresses?
Do I have to hook all mouse an keyboard events on the entire system or is there a simpler way?
The panel does not take in text input, you can set your main form to handle the keypress event.
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.form_KeyPress);
http://screensnapr.com/v/VovWAi.png
As for getting the mouse location on your panel, you can use the mouseclick event
this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseClick);
http://screensnapr.com/v/DB3kCQ.png
I have tested it, and it works on a fullsize transparent panel
I've discovered that I can use GetAsyncKeyState to respond to both keypresses and mouse clicks.
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(System.Windows.Forms.Keys vKey);
Then, I can call GetAsyncKeyState(Keys.LButton) and GetAsyncKeyState(Keys.Escape), for example. Much simpler than hooking all keyboard and mouse events.

ToolStrip control never fire Enter/Leave event?

I created a movable panel in WinForms. I use a ToolStrip as a titlebar in the panel. I'll use the ToolStrip to move the panel as well as indicating the panel is "active" or not. So when the panel is active, I want to change the ToolStrip's BackColor to Red.
UPDATE: The panel will host other controls, such as a listview. I want the panel being shown as "active" when the hosted control get focus, kind like the behavior of a normal window, whereas the window becomes the panel, and the titlebar becomes the ToolStrip.
When the panel is considered as "active"
hosted control get focus
ToolStrip being MouseDown/MouseClick
ToolStrip being dragged by mouse
The idea is capturing ToolStrip's Enter/Leave event to change the color, but it seems those events are never fired.
Are those events truly never fired? Should I capture other events?
mmmm there are a few ways to do this I guess.
You could use hook into the IMessageFilter message and see if its over your toolstrip/panel and then act accordingly (WM_LBUTTONDOWN, WM_MOUSEMOVE, WM_LBUTTONUP etc - see windows documentation on WIndows Messages for hex message codes for all windows messages). There are many examples about - for example: How to detect if the mouse is inside the whole form and child controls?
Setting a boolen based on whether active or not, an override in the paint can allow the colour settings - or simply flip them along with/instead of the boolean. Same for moving, set a boolean for move active/or not - and then allow WM_MOUSEMOVE to move the form/panel (or not) as needed.

Categories

Resources