Click lost on focusing form - windows

Question:
Is there a way to always let a click, that brings a form in to focus, through an have effect on the form?
Background:
With my (C# win form) application out of focus I can hover the form and get shades and borders indicating where my mouse is.
Clicking for example a menu entry (File) the form gets focus but the file menu does not get click. This takes an additional click.
For an ordinary button on the form only one click is required.

This can be fixed by setting focus before the click occurs. Se code:
class ToolStripEx : System.Windows.Forms.ToolStrip
{
protected override void WndProc(ref Message m)
{
// WM_MOUSEACTIVATE = 0x21
if (m.Msg == 0x21 && this.CanFocus && !this.Focused)
{
this.Focus();
}
base.WndProc(ref m);
}
}
This approach also works on MenuStrip

I found a few helpful articles – especially this one by Rick Brewster. The solution lies in overriding the WndProc method for the ToolStrip (or MenuStrip):
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (this.clickThrough &&
m.Msg == NativeConstants.WM_MOUSEACTIVATE &&
m.Result == (IntPtr)NativeConstants.MA_ACTIVATEANDEAT)
{
m.Result = (IntPtr)NativeConstants.MA_ACTIVATE;
}
}

Related

New UI Button misunderstood pointer click and pointer down action

I am making a game with ui controller. i use button to make that controller.
in the picture, i have "A" button. when my "character" facing npc, i want to talk with that npc with click that button. when dialogue is over, i need to close the dialogue box using this button too.
but what i got is, this button clicked over and over, so the first dialogue is skipped, and the dialogue which is showed up is the third or maybe the fourth dialog.
i attach event trigger on this button.
and this is my code that is attached on that event trigger
using UnityEngine;
using System.Collections;
public class aButtonScript : MonoBehaviour {
void Start(){
}
public void click(){
if(walkingScript.walking.interact == true)
{
loadGame.loadSave.objPrince.GetComponent<plantingScript>().aButton = false;
loadGame.loadSave.objPrince.GetComponent<walkingScript>().aButton = true;
}
}
public void clicked()
{
if(plantingScript.planting.toolBoxCanvas != null)
{
loadGame.loadSave.objPrince.GetComponent<plantingScript>().aButton = false;
choosingTools.ct.setTool();
}
}
public void unclicked()
{
loadGame.loadSave.objPrince.GetComponent<plantingScript>().aButton = false;
}
}
well, this is my first time making a game. hope whoever who read this question can help me. sorry for my bad english. thank you.
Not quite sure, but I've had similar problems when using GetMouseButton instead of GetMouseButtonDown. The first one triggers for each frame the button is pressed. Looking at your UI setup it seems you acivate the script when the button is pressed, for each frame it is pressed, and finally when it is released.

back button handling wp7

My problem is that I have a list. When I long press a particular item in the list then it opens a context menu and when I click on a menu item inside context menu then it opens a popup,so on pressing the hardware back button i want that i again go back to the list.
so for doing this my code is:
protected override void OnBackKeyPress(object sender,System.ComponentModel.CancelEventArgs e)
{
if (calendarDescripton.Visibility == Visibility.Visible)
{
calendarDescripton.Visibility = Visibility.Collapsed;
e.Cancel = true;
}
}
After using this code when I click the button that opens the list,the application exits,it does not open list also.
I think first the Navigation should be canceled, before making any other changes. Try this
protected override void OnBackKeyPress(object sender,System.ComponentModel.CancelEventArgs e)
{
if (calendarDescripton.Visibility == Visibility.Visible)
{
e.Cancel = true;
calendarDescripton.Visibility = Visibility.Collapsed;
}
}
If this doesn't help, place a break piont at the if condition and check if it is entering inside the if or not
If the break point doesn't hit, means there is something wrong with your Navigation approach.
If you are using NavigationService.Navigate() method for page navigation, it should work.
Otherwise, if you are using,
App.Current.RootVisual = new MyPage();, then BackKey cannot be overridden.

Telerik RadGrid ExportToPDF() or ExportToExcel() not working

I have a simple class inheriting RadGrid. I am adding a button to the RadGrid and a Click Event handler to that button. The button is correctly added in the required position and the click event handler is firing, but radGrid.ExportToExcel() is not doing anything. In fact, upon click and when page posts back, the button disappears. Why is this happening?
I tried to add the button control to the Page.Form control collection, but still nothing happens.
[ToolboxData("<{0}:RadGridDp runat=server></{0}:RadGridDp>")]
public class RadGridDP : RadGrid
{
public RadGridDP()
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Button btnExport = new Button();
btnExport.ID = "Export";
btnExport.Text = "Export";
btnExport.Click += new EventHandler(btnExport_Click);
btnExport.CommandArgument = this.ID;
this.MasterTableView.Controls.Add(btnExport);
}
void btnExport_Click(object sender, EventArgs e)
{
Button btnExport = (Button)sender;
string RadGridId = btnExport.CommandArgument.ToString();
RadGridDP radGrid = (RadGridDP)this.Parent.Parent.FindControl(RadGridId);
radGrid.ExportSettings.IgnorePaging = true;
radGrid.ExportSettings.OpenInNewWindow = true;
radGrid.ExportSettings.ExportOnlyData = true;
radGrid.MasterTableView.ExportToExcel();
}
}
When I do same thing in a UserControl and use that UserControl on any page, it works fine. What's the difference?
I found out the solution. Whenever RadGrid Loads, it calls various events in this fashion:
1. Page OnLoad
m. RadGrid OnLoad
x. NeedDataSource
and upon click of the button (added in the manner above), events are called in this fashion
1. Page_OnLoad
m. RadGrid OnLoad
n. btnExport_Click
x. NeedDataSource
(as for strange serial numbers, these events may have other events in between, but the order of occurance is correct)
so, entire Grid is rebound with the data, and hence command to exportPdf is flushed. So nothing happens.
Interestingly, there's no need of adding one extra button, telerik provides its own buttons to do so. which can be customized as well(by implementing ITemplate). This is how am generating Reports now(though not specific to the original question):
[ToolboxData("<{0}:RadGridDP runat=server></{0}:RadGridDP>")]
public class RadGridDP : RadGrid
{
//custom logic
public RadGridDP()
{
this.ItemCreated += new GridItemEventHandler(RadGrid_ItemCreated);
this.Load += new EventHandler(RadGridDP_Load);
this.ItemCommand += new GridCommandEventHandler(RadGrid_ItemCommand);
this.PdfExporting += new OnGridPdfExportingEventHandler(RadGridDP_PdfExporting);
this.GridExporting += new OnGridExportingEventHandler(RadGridDP_GridExporting);
this.ExportSettings.ExportOnlyData = true;
this.ExportSettings.IgnorePaging = true;
// this.ExportSettings.OpenInNewWindow = true;
DoPdfFormatting();
DoExcelFormatting();
}
protected void RadGridDP_PdfExporting(object sender, GridPdfExportingArgs e)
{
e.RawHTML = e.RawHTML.Replace("border=\"1\"", "").Replace("style=\"", "style=\" border:0.5px Solid black; ")
.Replace("<thead>", String.Format("<thead>{0}", TableHeader)).Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
}
protected void RadGridDP_GridExporting(object sender, GridExportingArgs e)
{
e.ExportOutput = e.ExportOutput.Replace("<thead>", String.Format("<thead>{0}", TableHeader))
.Replace("</tbody>", String.Format("{0}</tbody>", TableFooter));
}
}
so basically i had to handle PdfExporting(for Pdf) and GridExporting(for excel)..
I had to handle Load, ItemCommand and ItemCreated as well. While the former one was required for some conditional logic, later two were required for formatting of the PDF document

Let my MFC dialog receive keystroke events before its controls (MFC/Win32 equivalent of WinForms "KeyPreview")

I have an MFC dialog containing a dozen or so buttons, radio buttons and readonly edit controls.
I'd like to know when the user hits Ctrl+V in that dialog, regardless of which control has the focus.
If this were C#, I could set the KeyPreview proprety and my form would receive all the keystrokes before the individual controls - but how do I do that in my MFC dialog?
JTeagle is right. You should override PreTranslateMessage().
// Example
BOOL CDlgFoo::PreTranslateMessage( MSG* pMsg )
{
// Add your specialized code here and/or call the base class
if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
{
int idCtrl= this->GetFocus()->GetDlgCtrlID();
if ( idCtrl == IDC_MY_EDIT ) {
// do something <--------------------
return TRUE; // eat the message
}
}
return CDialog::PreTranslateMessage( pMsg );
}
Add a handler to override PreTranslateMessage() in the dialog class, and check the details of the MSG struct received there. Be sure to call the base class to get the right return value, unless you want to eat the keystroke to prevent it going further.

Detecting when a CControlBar's docking state has changed

I'm using a CControlBar-derived class and I would like to detect when the docking state of the CControlBar has changed (i.e., docking from vertical to horizontal or when it goes into floating mode).
Of course, I could handle WM_SIZE but it seems to be a waste of ressources doing the task every time a WM_SIZE message is fired instead of only when the docking state has changed.
Can anyone please point me in the right direction?
You can override the CControlBar::OnBarStyleChange virtual function to detect changes in the control bar style (CBRS_XXX values - look in the <afxres.h> header file for details).
To determine whether the control bar is floating/docked, check the CBRS_FLOATING style. To check for horizontal/vertical orientation, use the CBRS_ORIENT_HORZ and CBRS_ORIENT_VERT styles.
So, using CToolBar (which is derived from CControlBar) as an example:
class CMyToolBar : public CToolBar {
public:
virtual void OnBarStyleChange(DWORD dwOldStyle, DWORD dwNewStyle);
};
void CMyToolBar::OnBarStyleChange(DWORD dwOldStyle, DWORD dwNewStyle)
{
// Call base class implementation.
CToolBar::OnBarStyleChange(dwOldStyle, dwNewStyle);
// Use exclusive-or to detect changes in style bits.
DWORD changed = dwOldStyle ^ dwNewStyle;
if (changed & CBRS_FLOATING) {
if (dwNewStyle & CBRS_FLOATING) {
// ToolBar now floating
}
else {
// ToolBar now docked
}
}
if (changed & CBRS_ORIENT_ANY) {
if (dwNewStyle & CBRS_ORIENT_HORZ) {
// ToolBar now horizontal
}
else if (dwNewStyle & CBRS_ORIENT_VERT) {
// ToolBar now vertical
}
}
}
I hope this helps!

Resources