Click here to Skip to main content
15,885,216 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CString multiple assignment leak? PinPopular
Adam Roderick J4-Apr-10 21:40
Adam Roderick J4-Apr-10 21:40 
AnswerRe: CString multiple assignment leak? Pin
Joe Woodbury5-Apr-10 6:41
professionalJoe Woodbury5-Apr-10 6:41 
QuestionSecond Window in Win32 Pin
Fareed Rizkalla4-Apr-10 14:51
Fareed Rizkalla4-Apr-10 14:51 
AnswerRe: Second Window in Win32 Pin
enhzflep4-Apr-10 15:09
enhzflep4-Apr-10 15:09 
GeneralRe: Second Window in Win32 Pin
Fareed Rizkalla4-Apr-10 20:53
Fareed Rizkalla4-Apr-10 20:53 
GeneralRe: Second Window in Win32 Pin
enhzflep5-Apr-10 1:59
enhzflep5-Apr-10 1:59 
GeneralRe: Second Window in Win32 Pin
Fareed Rizkalla5-Apr-10 3:51
Fareed Rizkalla5-Apr-10 3:51 
GeneralRe: Second Window in Win32 Pin
enhzflep5-Apr-10 8:36
enhzflep5-Apr-10 8:36 
I still can't help but get the feeling that you're using a single WindowProcedure to handle both the Parent window and the Child window.

As far as I know, you have to use _seperate_ procs for each window. With that in mind, there is no issue when it comes to finding out who generated a message - the message automatically goes to it's own parent. Perhaps I'm missing something?

Here's about the smallest, simplest (& unfortunately perhaps the nastiest) example I can think of at 4:30am
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK childWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK parentWndProc(HWND, UINT, WPARAM, LPARAM);
bool myRegClass(WNDPROC lpfnWndProc, char *szClassName, HINSTANCE hInst);
char *szParentClassName = "parentClass", *szChildClassName = "childClass";
HINSTANCE hThisInstance;
HWND parentWnd;

#define IDC_FROMPARENT   1000
#define IDC_FROMCHILD   1001


int WINAPI WinMain (HINSTANCE hCurInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwndParent;                          /* This is the handle for our window */
    MSG messages;                             /* Here messages to the application are saved */

    hThisInstance = hCurInstance;

    if (myRegClass(parentWndProc, szParentClassName, hThisInstance) == false)
        return 0;

    if (myRegClass(childWndProc, szChildClassName, hThisInstance) == false)
        return 0;

    /* both the classes are registered, let's create the program*/
    parentWnd = CreateWindowEx(
           0,
           szParentClassName,         /* Classname */
           "Code::Blocks Template Windows App - parent",       /* Title Text */
           WS_OVERLAPPEDWINDOW|WS_VISIBLE, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* 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 */
           );

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}


bool myRegClass(WNDPROC lpfnWndProc, char *szClassName, HINSTANCE hInst)
{
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hInst;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = lpfnWndProc;      /* 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 true;
    else
        return false;
}

/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK parentWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND childWnd, listBox;
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
               childWnd = CreateWindowEx(
               0,
               szChildClassName,         /* Classname */
               "Code::Blocks Template Windows App - child",       /* Title Text */
               WS_OVERLAPPEDWINDOW|WS_VISIBLE, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544/2,               /* The programs width */
               375/2,               /* and height in pixels */
               NULL,          /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
            CreateWindowEx(0, "BUTTON", "Msg to Child", WS_VISIBLE | WS_CHILD | WS_TABSTOP | 0x00000001, 8, 8, 150, 23, hwnd, (HMENU)IDOK, hThisInstance, 0);
            return 0;
            break;

         case IDC_FROMCHILD:
            MessageBox(NULL, "Child window sent me a message", "Parent", MB_OK);
         break;

        case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                 case IDOK:
                    PostMessage(childWnd, IDC_FROMPARENT, 0, 0);
                 break;
                }
            break;

        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}

/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK childWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            CreateWindowEx(0, "BUTTON", "Msg to Parent", WS_VISIBLE | WS_CHILD | WS_TABSTOP | 0x00000001, 8, 8, 150, 23, hwnd, (HMENU)IDOK, hThisInstance, 0);
            //parentWnd = GetParent(hwnd);
            return 0;
            break;

        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

         case IDC_FROMPARENT:
            MessageBox(NULL, "Parent window sent me a message", "Child", MB_OK);
         break;

        case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                 case IDOK:
                    PostMessage(parentWnd, IDC_FROMCHILD, 0, 0);
                 break;
                }
            break;

        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}

QuestionRe: Second Window in Win32 Pin
Fareed Rizkalla5-Apr-10 15:41
Fareed Rizkalla5-Apr-10 15:41 
AnswerRe: Second Window in Win32 Pin
enhzflep6-Apr-10 2:12
enhzflep6-Apr-10 2:12 
GeneralRe: Second Window in Win32 Pin
Fareed Rizkalla6-Apr-10 5:13
Fareed Rizkalla6-Apr-10 5:13 
GeneralRe: Second Window in Win32 Pin
enhzflep6-Apr-10 17:42
enhzflep6-Apr-10 17:42 
GeneralRe: Second Window in Win32 Pin
Fareed Rizkalla5-Apr-10 16:28
Fareed Rizkalla5-Apr-10 16:28 
AnswerRe: Second Window in Win32 Pin
Garth J Lancaster4-Apr-10 15:11
professionalGarth J Lancaster4-Apr-10 15:11 
Questionsocket programming in c help Pin
kedah4-Apr-10 8:48
kedah4-Apr-10 8:48 
AnswerRe: socket programming in c help Pin
Richard Andrew x644-Apr-10 11:49
professionalRichard Andrew x644-Apr-10 11:49 
AnswerRe: socket programming in c help Pin
Garth J Lancaster4-Apr-10 12:29
professionalGarth J Lancaster4-Apr-10 12:29 
QuestionIs the handle from OpenProcess Unique Pin
ForNow4-Apr-10 7:37
ForNow4-Apr-10 7:37 
QuestionRecieving device events in a service Pin
Ap0ll0-134-Apr-10 3:42
Ap0ll0-134-Apr-10 3:42 
QuestionHow to get the net speed of your computer and how to get the current webpage's speed? Pin
Aric Wang4-Apr-10 0:42
Aric Wang4-Apr-10 0:42 
QuestionRe: How to get the net speed of your computer and how to get the current webpage's speed? Pin
enhzflep4-Apr-10 3:31
enhzflep4-Apr-10 3:31 
QuestionApplication crash - on use of IXMLHTTPRequest send method Pin
dinesh babu3-Apr-10 20:47
dinesh babu3-Apr-10 20:47 
QuestionIs my merge list right? Pin
wbgxx3-Apr-10 18:36
wbgxx3-Apr-10 18:36 
AnswerRe: Is my merge list right? Pin
Luc Pattyn3-Apr-10 21:06
sitebuilderLuc Pattyn3-Apr-10 21:06 
QuestionCWinApp <-> DLL communication Pin
hxhl953-Apr-10 12:09
hxhl953-Apr-10 12:09 

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.