Click here to Skip to main content
15,892,927 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
SutterA11-Sep-08 3:24
SutterA11-Sep-08 3:24 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
David Crow11-Sep-08 3:20
David Crow11-Sep-08 3:20 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
SutterA11-Sep-08 3:26
SutterA11-Sep-08 3:26 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
David Crow11-Sep-08 3:40
David Crow11-Sep-08 3:40 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
SutterA11-Sep-08 3:45
SutterA11-Sep-08 3:45 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
David Crow11-Sep-08 3:51
David Crow11-Sep-08 3:51 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
enhzflep11-Sep-08 4:19
enhzflep11-Sep-08 4:19 
AnswerRe: Determine the CRichEditCtrl which holds the focus Pin
enhzflep11-Sep-08 5:01
enhzflep11-Sep-08 5:01 
I've not used MFC as I don't have access to it, though I'm sure you can translate this C code to one that suits your needs. It only requires that you handle the WM_SETFOCUS message for your button and handle the (user defined WM_COMMAND) MM_OLDHWND message in your main window's windowProc.

#include <windows.h>
#include <commctrl.h>

/*  Declare Windows procedures  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK btnWndProc(HWND, UINT, WPARAM, LPARAM);

long OldBtnProc;
HWND btnWnd, edit1Wnd, edit2Wnd, edit3Wnd;
#define IDOK               1000
#define IDC_EDIT1          1001
#define IDC_EDIT2          1002
#define IDC_EDIT3          1003
#define MM_OLDHWND         1004


/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HINSTANCE hInst = hThisInstance;
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           194,                 /* The programs width */
           128,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );


    HFONT hfont0 = CreateFont(-11, 0, 0, 0, 400, FALSE, FALSE, FALSE, 1, 400, 0, 0, 0, "Ms Shell Dlg 2");
    //HWND hwnd = CreateWindowEx(0, "WndClass0", "Dialog", WS_VISIBLE | WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_POPUP | WS_SYSMENU, 0, 0, 194, 128, 0, 0, hInst, 0);

    btnWnd = CreateWindowEx(0, WC_BUTTON, "OK", WS_VISIBLE | WS_CHILD | WS_TABSTOP, 101, 63, 75, 23, hwnd, (HMENU)IDOK, hInst, 0);
    SendMessage(btnWnd, WM_SETFONT, (WPARAM)hfont0, FALSE);
    edit1Wnd = CreateWindowEx(0, WC_EDIT, 0, WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 8, 8, 60, 20, hwnd, (HMENU)IDC_EDIT1, hInst, 0);
    SendMessage(edit1Wnd, WM_SETFONT, (WPARAM)hfont0, FALSE);
    edit2Wnd = CreateWindowEx(0, WC_EDIT, 0, WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 8, 36, 60, 20, hwnd, (HMENU)IDC_EDIT2, hInst, 0);
    SendMessage(edit2Wnd, WM_SETFONT, (WPARAM)hfont0, FALSE);
    edit3Wnd = CreateWindowEx(0, WC_EDIT, 0, WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL, 8, 63, 60, 20, hwnd, (HMENU)IDC_EDIT3, hInst, 0);
    SendMessage(edit3Wnd, WM_SETFONT, (WPARAM)hfont0, FALSE);

    OldBtnProc = (long) SetWindowLong(btnWnd, GWL_WNDPROC, (long)btnWndProc);

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_COMMAND:
            switch (wParam)
            {
                case MM_OLDHWND:
                    if ((HWND)lParam == edit1Wnd)
                        MessageBox(NULL, "Editbox 1 had focus", "Attention", MB_ICONEXCLAMATION);
                    else if ((HWND)lParam == edit2Wnd)
                        MessageBox(NULL, "Editbox 2 had focus", "Attention", MB_ICONEXCLAMATION);
                    else if ((HWND)lParam == edit3Wnd)
                        MessageBox(NULL, "Editbox 3 had focus", "Attention", MB_ICONEXCLAMATION);
                    else
                        MessageBox(NULL, "Unknown control had focus", "Attention", MB_ICONEXCLAMATION);
                    return 1;
            }
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}

LRESULT CALLBACK btnWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_SETFOCUS:
            PostMessage(GetParent(hwnd), WM_COMMAND, MM_OLDHWND, (LPARAM)wParam);
        default:                      /* for messages that we don't deal with */
            return CallWindowProc((WNDPROC)OldBtnProc, hwnd, message, wParam, lParam);
    }
}


Thanks for the thought inspiring question.
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
SutterA11-Sep-08 5:45
SutterA11-Sep-08 5:45 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
enhzflep11-Sep-08 5:53
enhzflep11-Sep-08 5:53 
GeneralRe: Determine the CRichEditCtrl which holds the focus Pin
SutterA11-Sep-08 21:54
SutterA11-Sep-08 21:54 
Questioncompiler in VC++6.0 Pin
aa_zz11-Sep-08 0:57
aa_zz11-Sep-08 0:57 
AnswerRe: compiler in VC++6.0 Pin
Rane11-Sep-08 1:01
Rane11-Sep-08 1:01 
AnswerRe: compiler in VC++6.0 Pin
_AnsHUMAN_ 11-Sep-08 1:12
_AnsHUMAN_ 11-Sep-08 1:12 
AnswerRe: compiler in VC++6.0 Pin
Hamid_RT11-Sep-08 1:25
Hamid_RT11-Sep-08 1:25 
AnswerRe: compiler in VC++6.0 Pin
CPallini11-Sep-08 1:35
mveCPallini11-Sep-08 1:35 
QuestionSetting full access permissions to a directory using CreateDirectory( ) Pin
V K 211-Sep-08 0:45
V K 211-Sep-08 0:45 
AnswerRe: Setting full access permissions to a directory using CreateDirectory( ) Pin
Rane11-Sep-08 0:58
Rane11-Sep-08 0:58 
AnswerRe: Setting full access permissions to a directory using CreateDirectory( ) Pin
santhoshv8411-Sep-08 1:01
santhoshv8411-Sep-08 1:01 
GeneralRe: Setting full access permissions to a directory using CreateDirectory( ) Pin
Rane11-Sep-08 1:03
Rane11-Sep-08 1:03 
GeneralRe: Setting full access permissions to a directory using CreateDirectory( ) Pin
V K 211-Sep-08 1:47
V K 211-Sep-08 1:47 
QuestionRe: Setting full access permissions to a directory using CreateDirectory( ) Pin
Mark Salsbery11-Sep-08 4:33
Mark Salsbery11-Sep-08 4:33 
GeneralRe: Setting full access permissions to a directory using CreateDirectory( ) Pin
V K 211-Sep-08 1:48
V K 211-Sep-08 1:48 
Questionhow to find out process state [modified] Pin
eusto11-Sep-08 0:32
eusto11-Sep-08 0:32 
AnswerRe: how to find out process state Pin
Rane11-Sep-08 0:52
Rane11-Sep-08 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.