Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everybody,

The last time, I used to coded a virtual keyboard embeded in a applicaiotn by C#, and this time, i must coded V-keyboard by MFC for 1 application MFC.

The work i did when i code VK by C#:
1. Create a form don't get focus when click on it.(override WndProc funtion of Form and process mesage to prevent focus in itself)
2. Create a button inherited Button base, and this button also don't get focus when click on it.
a. Override CreateParams funtion with "createParams.ExStyle = WS_EX_NOACTIVATE".
b. Override WndProc funtion and process WM_MOUSEACTIVATE message.
3. Make event sendkey when click on button.

And this is questions for virtual keyboard MFC:
1. How to create a CDialog don't get focus when click on it in MFC?
2. How to create a new button which the same with button i created by C# (above).

P/S: I searched on google for many days, but i still not find answer exactly.
Please help me!!!

I'm sorry,my english is not good. so difficult to explain more clearly.
Hope you try to help me.

Best regard.
Posted
Updated 7-Feb-11 16:08pm
v2

I almost got it to work. I didn't add a new class for the button. Just do this:

To create the dialog box:
CNotFocusedDlg* dlg = new CNotFocusedDlg();
dlg->Create(IDD_NOT_FOCUSED);
dlg->ShowWindow(SW_SHOW);
//don't forget to delete the pointer
//when you don't need the dialog anymore


In your dialog class, add the WM_ACTIVATE and WM_MOUSEACTIVATE handlers with the following code:

In the .h:
class CNotFocusedDlg : public CDialog
{
	DECLARE_DYNAMIC(CNotFocusedDlg)
public:
	CNotFocusedDlg(CWnd* pParent = NULL);   // standard constructor
	virtual ~CNotFocusedDlg();
// Dialog Data
	enum { IDD = IDD_NOT_FOCUSED };
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	DECLARE_MESSAGE_MAP()

	afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
	afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message);
        afx_msg void OnBnClickedButton1();
};

Then in the .cpp:
BEGIN_MESSAGE_MAP(CNotFocusedDlg, CDialog)
    ON_WM_ACTIVATE()
    ON_WM_MOUSEACTIVATE()
    ON_BN_CLICKED(IDC_BUTTON1, &CNotFocusedDlg::OnBnClickedButton1)
END_MESSAGE_MAP()

void CNotFocusedDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
    if (nState == WA_ACTIVE || nState == WA_CLICKACTIVE)
    {
        if (pWndOther != NULL)
            pWndOther->SetActiveWindow();
    }

    CDialog::OnActivate(nState, pWndOther, bMinimized);
}

int CNotFocusedDlg::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
    return MA_NOACTIVATE;
}

void CNotFocusedDlg::OnBnClickedButton1()
{
    //do something here...
}


This code seems to work except for the first WM_ACTIVATE message. I tried to use the WM_EX_NOACTIVATE style but it seems I'm missing something there...
Anyway I hope it will help you a bit.
 
Share this answer
 
v4
I am not sure if it will work, but you may try to override the PreTranslateMessage function. Here, you can decide to process each message or not (WM_MOUSEACTIVATE for example).

To change the dialog styles, have a look to ModifyStyle and ModifyStyleEx

To send keyboard events from code, maybe this could help:
Keyboard Events Simulation using keybd_event() function[^]
 
Share this answer
 
Set the WS_EX_NOACTIVATE style for your keyboard dialog, that way it won't get focus. You should probably be able to get the same style to work on your button controls too.
 
Share this answer
 
v2
Thanks Olivier and Nishant. I understand your answer but the true is i don't know much about MFC,so i don't know that how to code? i just have code of C#.
Code C# for "Create a form don't get focus when click on it":
* In Keyboard class:
C#
protected override void WndProc(ref Message msg)
{
    if (msg.Msg == WM_ACTIVATE)
    {
        if (((int)msg.WParam & 0xFFFF) != WA_INACTIVE)
        {
            if (msg.LParam != IntPtr.Zero)
            {
                SetActiveWindow(msg.LParam);
            }
            else
            {
                SetActiveWindow(IntPtr.Zero);
            }
            base.WndProc(ref msg);
        }
    }
}


* In button class:
C#
protected override CreateParams CreateParams
    {
      get
      {
        CreateParams createParams = base.CreateParams;
        createParams.ExStyle = WS_EX_NOACTIVATE;
        return createParams;
      }
    }
protected override void WndProc(ref Message msg)
    {
      if (msg.Msg == WM_MOUSEACTIVATE)
      {
        msg.Result = (IntPtr)MA_NOACTIVATE;
      }
      else
        base.WndProc(ref msg);
    }


I coded for "Create a form don't get focus when click on it" successfull, but in button class, i don't know mean of CreateParams function, so i don't know how to convert it to MFC.
Please explain for me about it.

Please!!!
 
Share this answer
 
Thanks Olivier somuch, I will try your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900