Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Keyboard response in a dialog, not the main window--thanks a lot

Keyboard response in a dialog, not the main window--thanks a lot

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sean_Yang
    wrote on last edited by
    #1

    :confused:I am a new programmer, can't figure out why the keyboard doesn't responds. I traced the following code, the doesn't enter the code corresponding to keyboard at all. Here I attached how the dialog is triggered. Hopefully someone can help me figure it out. Thanks a lot. int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow) { ..... while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { .... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper); } BOOL CALLBACK Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { case WM_CHAR: switch(wParam) { case '1': record_data_pair(1); break; case '2'://0x32: record_data_pair(2); break; case '3'://0x33: record_data_pair(1); break; default: break; } return FALSE; } ..... return FALSE; } Sean

    R 1 Reply Last reply
    0
    • S Sean_Yang

      :confused:I am a new programmer, can't figure out why the keyboard doesn't responds. I traced the following code, the doesn't enter the code corresponding to keyboard at all. Here I attached how the dialog is triggered. Hopefully someone can help me figure it out. Thanks a lot. int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow) { ..... while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { .... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper); } BOOL CALLBACK Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { case WM_CHAR: switch(wParam) { case '1': record_data_pair(1); break; case '2'://0x32: record_data_pair(2); break; case '3'://0x33: record_data_pair(1); break; default: break; } return FALSE; } ..... return FALSE; } Sean

      R Offline
      R Offline
      RicoH
      wrote on last edited by
      #2

      I think the problems lies in this part of your code... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper); If you specify 'NULL' as owner of this window, the Desktop will be the parent. However, since this dialogbox doesn't have it's own messageque it won't receive anything. So, if you do it like this, it should work just fine. hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Exper); Where 'hwnd' is the handle from your main window (in MainWndProc) Don't think you are, know you are...

      S 1 Reply Last reply
      0
      • R RicoH

        I think the problems lies in this part of your code... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper); If you specify 'NULL' as owner of this window, the Desktop will be the parent. However, since this dialogbox doesn't have it's own messageque it won't receive anything. So, if you do it like this, it should work just fine. hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Exper); Where 'hwnd' is the handle from your main window (in MainWndProc) Don't think you are, know you are...

        S Offline
        S Offline
        Sean_Yang
        wrote on last edited by
        #3

        Shall I declare a global HWND hmainwnd; and within the WinMain() function. if (!(hwnd = CreateWindow ("MAINWINDOW", "1DoF Stepper Motor Haptic Device", WS_OVERLAPPEDWINDOW, 0,0, 800, 150, NULL, NULL, hCurrentInst, NULL))) return (0); hmainwnd = hwnd;//like this??? and then when triggering the dialog, I do hwnddlg = CreateDialog(app_instance, "Experiment", hmainwnd, (DLGPROC) Parameter_Window_Exper); But I did so, it doesn't work. The sequence triggered is as following: Main->Dialog_Data->Dialog_Experiment. I want the Dialog_Experiment to respond my keyboard. My code(simplified) is like this:should I change all NULL to hmainwnd? ~~~~~~~~~~~~~~~~~~~~~~~~~~~` extern HWND hwnddlg; int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow) { ... if (!RegisterClass (&wc)) { MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed", (LPCTSTR) "Error!", MB_OK | MB_ICONEXCLAMATION); return(FALSE); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } ------------------------------------------ LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { ... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Data); } ------------------------------------------- BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { ... DestroyWindow(hWnd); hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, NULL, (DLGPROC) Parameter_Window_Exper); } ------------------------------------------- BOOL Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { case WM_CHAR: switch(wParam) { case '1': MessageBox(hmainwnd, "Happen?", "TEST Box", MB_OK); record_data_pair(1); break; case '2'://0x32: record_data_pair(2); break; case '3'://0x33: record_data_pair(1); break; default: break; } return FALSE; } Thanks Sean

        R 1 Reply Last reply
        0
        • S Sean_Yang

          Shall I declare a global HWND hmainwnd; and within the WinMain() function. if (!(hwnd = CreateWindow ("MAINWINDOW", "1DoF Stepper Motor Haptic Device", WS_OVERLAPPEDWINDOW, 0,0, 800, 150, NULL, NULL, hCurrentInst, NULL))) return (0); hmainwnd = hwnd;//like this??? and then when triggering the dialog, I do hwnddlg = CreateDialog(app_instance, "Experiment", hmainwnd, (DLGPROC) Parameter_Window_Exper); But I did so, it doesn't work. The sequence triggered is as following: Main->Dialog_Data->Dialog_Experiment. I want the Dialog_Experiment to respond my keyboard. My code(simplified) is like this:should I change all NULL to hmainwnd? ~~~~~~~~~~~~~~~~~~~~~~~~~~~` extern HWND hwnddlg; int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow) { ... if (!RegisterClass (&wc)) { MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed", (LPCTSTR) "Error!", MB_OK | MB_ICONEXCLAMATION); return(FALSE); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } ------------------------------------------ LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { ... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Data); } ------------------------------------------- BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { ... DestroyWindow(hWnd); hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, NULL, (DLGPROC) Parameter_Window_Exper); } ------------------------------------------- BOOL Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { case WM_CHAR: switch(wParam) { case '1': MessageBox(hmainwnd, "Happen?", "TEST Box", MB_OK); record_data_pair(1); break; case '2'://0x32: record_data_pair(2); break; case '3'://0x33: record_data_pair(1); break; default: break; } return FALSE; } Thanks Sean

          R Offline
          R Offline
          RicoH
          wrote on last edited by
          #4

          I don't think you have to declare a global variable. This should do the trick (and if not, I'll need to see more of your code): LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { ... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Data); } ------------------------------------------- BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { ... DestroyWindow(hWnd); hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, hWnd, (DLGPROC) Parameter_Window_Exper); } ------------------------------------------- You see, just give it the handle to the parent in stead of using NULL (which means the desktop) Don't think you are, know you are...

          S 1 Reply Last reply
          0
          • R RicoH

            I don't think you have to declare a global variable. This should do the trick (and if not, I'll need to see more of your code): LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { ... hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Data); } ------------------------------------------- BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) { ... DestroyWindow(hWnd); hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, hWnd, (DLGPROC) Parameter_Window_Exper); } ------------------------------------------- You see, just give it the handle to the parent in stead of using NULL (which means the desktop) Don't think you are, know you are...

            S Offline
            S Offline
            Sean_Yang
            wrote on last edited by
            #5

            I did so as you posted, but the child dialog (EXPER_INTRO_DIALOG) can't be displayed. I wonder if the problem now(not display at all) is because DestroyWindow(hWnd) has killed the parent dialog. Anyway, I guess I can get my program done under your help. Regards, Sean

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups