C++ Keybinder fehler

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • C++ Keybinder fehler

    Hallo,

    ich hab einen keybinder programmiert. er ist noch nicht fertig (net beschriftet, und befehle sollen erstmal net simuliert sondern in msgbox ausgegeben werden). mein code ist folgender:

    C-Quellcode

    1. #include <windows.h>
    2. #include <iostream>
    3. using namespace std;
    4. /* Declare Windows procedure */
    5. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    6. /* Make the class name into a global variable */
    7. char szClassName[ ] = "Keybinder";
    8. string num[9]; // die null will ich net benutzen damit es übersichtliger ist
    9. std::string GetKey(int Key)
    10. {
    11. switch(Key){
    12. case VK_NUMPAD1: return num[1];
    13. break;
    14. case VK_NUMPAD2: return num[2];
    15. break;
    16. case VK_NUMPAD3: return num[3];
    17. break;
    18. case VK_NUMPAD4: return num[4];
    19. break;
    20. case VK_NUMPAD5: return num[5];
    21. break;
    22. case VK_NUMPAD6: return num[6];
    23. break;
    24. case VK_NUMPAD7: return num[7];
    25. break;
    26. case VK_NUMPAD8: return num[8];
    27. break;
    28. case VK_NUMPAD9: return num[9];
    29. break;
    30. }
    31. }
    32. DWORD WINAPI keystate(void *param){
    33. while(true)
    34. {
    35. string fenster;
    36. //Verhindert CPU Auslastung 5ms sleep
    37. Sleep(5);
    38. for(int i = 8; i < 191; i++)
    39. {
    40. if(GetAsyncKeyState(i)&1 ==1)
    41. {
    42. HWND hwnd = GetForegroundWindow(); // aktives Fenster holen
    43. TCHAR szWindowText[100]; // darin wird der Titel gespeichert
    44. GetWindowText(hwnd, szWindowText, 100); // Titel mit hwnd holen
    45. fenster=szWindowText;
    46. if(fenster=="GTA:SA:MP"){
    47. MessageBox ( NULL, GetKey(i).c_str(), "INHALT", MB_OK);
    48. }
    49. }
    50. }
    51. }
    52. }
    53. int WINAPI WinMain (HINSTANCE hThisInstance,
    54. HINSTANCE hPrevInstance,
    55. LPSTR lpszArgument,
    56. int nFunsterStil)
    57. {
    58. int param = 100;
    59. HANDLE hThreadHandle = CreateThread( 0,
    60. 0,
    61. keystate,
    62. &param,
    63. 0,
    64. 0 );
    65. HWND hwnd; /* This is the handle for our window */
    66. MSG messages; /* Here messages to the application are saved */
    67. WNDCLASSEX wincl; /* Data structure for the windowclass */
    68. /* The Window structure */
    69. wincl.hInstance = hThisInstance;
    70. wincl.lpszClassName = szClassName;
    71. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
    72. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
    73. wincl.cbSize = sizeof (WNDCLASSEX);
    74. /* Use default icon and mouse-pointer */
    75. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    76. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    77. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    78. wincl.lpszMenuName = NULL; /* No menu */
    79. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
    80. wincl.cbWndExtra = 0; /* structure or the window instance */
    81. /* Use Windows's default color as the background of the window */
    82. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    83. /* Register the window class, and if it fails quit the program */
    84. if (!RegisterClassEx (&wincl))
    85. return 0;
    86. /* The class is registered, let's create the program*/
    87. hwnd = CreateWindowEx (
    88. 0, /* Extended possibilites for variation */
    89. szClassName, /* Classname */
    90. szClassName, /* Title Text */
    91. WS_OVERLAPPEDWINDOW, /* default window */
    92. CW_USEDEFAULT, /* Windows decides the position */
    93. CW_USEDEFAULT, /* where the window ends up on the screen */
    94. 544, /* The programs width */
    95. 375, /* and height in pixels */
    96. HWND_DESKTOP, /* The window is a child-window to desktop */
    97. NULL, /* No menu */
    98. hThisInstance, /* Program Instance handler */
    99. NULL /* No Window Creation data */
    100. );
    101. /* Make the window visible on the screen */
    102. ShowWindow (hwnd, nFunsterStil);
    103. /* Run the message loop. It will run until GetMessage() returns 0 */
    104. while (GetMessage (&messages, NULL, 0, 0))
    105. {
    106. /* Translate virtual-key messages into character messages */
    107. TranslateMessage(&messages);
    108. /* Send message to WindowProcedure */
    109. DispatchMessage(&messages);
    110. }
    111. /* The program return-value is 0 - The value that PostQuitMessage() gave */
    112. return messages.wParam;
    113. }
    114. /* This function is called by the Windows function DispatchMessage() */
    115. LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    116. {
    117. HWND numedit[9];
    118. switch (message) /* handle the messages */
    119. {
    120. case WM_CREATE :{
    121. int x;
    122. for(int i = 1; i < 10; i++){
    123. x=10+(i*20);
    124. numedit[i] = CreateWindow("EDIT","", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,20,x,200,15,hwnd,0,
    125. ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
    126. }
    127. }break;
    128. case WM_DESTROY:
    129. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
    130. break;
    131. case WM_COMMAND: {
    132. int iLenght;
    133. char * text;
    134. for(int i=1;i<10;i++){
    135. iLenght = GetWindowTextLength(numedit[i])+1;
    136. text = new char[iLenght+1];
    137. GetWindowText(numedit[i],text ,iLenght);
    138. num[i]=text;
    139. }
    140. }break;
    141. default:
    142. return DefWindowProc (hwnd, message, wParam, lParam);
    143. }
    144. return 0;
    145. }
    Alles anzeigen

    es kommt ein fehler wenn man auf die editboxen drückt. ich glaub der fehler kommt wenn das for() im case WM_COMMAND fertig ist raus, weil ich an sämtlichen stellen ein beep() eingesetzt habe und alle die vor den nachm for() sind gingen.


    MfG met0in
  • Quellcode

    1. #include <windows.h>
    2. #include <iostream>
    3. using namespace std;
    4. /* Declare Windows procedure */
    5. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    6. /* Make the class name into a global variable */
    7. char szClassName[ ] = "Keybinder";
    8. string num[10]; // die null will ich net benutzen damit es übersichtliger ist
    9. std::string GetKey(int Key)
    10. {
    11. switch(Key){
    12. case VK_NUMPAD1: return num[1];
    13. break;
    14. case VK_NUMPAD2: return num[2];
    15. break;
    16. case VK_NUMPAD3: return num[3];
    17. break;
    18. case VK_NUMPAD4: return num[4];
    19. break;
    20. case VK_NUMPAD5: return num[5];
    21. break;
    22. case VK_NUMPAD6: return num[6];
    23. break;
    24. case VK_NUMPAD7: return num[7];
    25. break;
    26. case VK_NUMPAD8: return num[8];
    27. break;
    28. case VK_NUMPAD9: return num[9];
    29. break;
    30. }
    31. }
    32. DWORD WINAPI keystate(void *param){
    33. while(true)
    34. {
    35. string fenster;
    36. //Verhindert CPU Auslastung 5ms sleep
    37. Sleep(5);
    38. for(int i = 8; i < 191; i++)
    39. {
    40. if(GetAsyncKeyState(i)&1 ==1)
    41. {
    42. HWND hwnd = GetForegroundWindow(); // aktives Fenster holen
    43. TCHAR szWindowText[100]; // darin wird der Titel gespeichert
    44. GetWindowText(hwnd, szWindowText, 100); // Titel mit hwnd holen
    45. fenster=szWindowText;
    46. if(fenster=="GTA:SA:MP"){
    47. MessageBox ( NULL, GetKey(i).c_str(), "INHALT", MB_OK);
    48. }
    49. }
    50. }
    51. }
    52. }
    53. int WINAPI WinMain (HINSTANCE hThisInstance,
    54. HINSTANCE hPrevInstance,
    55. LPSTR lpszArgument,
    56. int nFunsterStil)
    57. {
    58. int param = 100;
    59. HANDLE hThreadHandle = CreateThread( 0,
    60. 0,
    61. keystate,
    62. &param,
    63. 0,
    64. 0 );
    65. HWND hwnd; /* This is the handle for our window */
    66. MSG messages; /* Here messages to the application are saved */
    67. WNDCLASSEX wincl; /* Data structure for the windowclass */
    68. /* The Window structure */
    69. wincl.hInstance = hThisInstance;
    70. wincl.lpszClassName = szClassName;
    71. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
    72. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
    73. wincl.cbSize = sizeof (WNDCLASSEX);
    74. /* Use default icon and mouse-pointer */
    75. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    76. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    77. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    78. wincl.lpszMenuName = NULL; /* No menu */
    79. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
    80. wincl.cbWndExtra = 0; /* structure or the window instance */
    81. /* Use Windows's default color as the background of the window */
    82. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    83. /* Register the window class, and if it fails quit the program */
    84. if (!RegisterClassEx (&wincl))
    85. return 0;
    86. /* The class is registered, let's create the program*/
    87. hwnd = CreateWindowEx (
    88. 0, /* Extended possibilites for variation */
    89. szClassName, /* Classname */
    90. szClassName, /* Title Text */
    91. WS_OVERLAPPEDWINDOW, /* default window */
    92. CW_USEDEFAULT, /* Windows decides the position */
    93. CW_USEDEFAULT, /* where the window ends up on the screen */
    94. 544, /* The programs width */
    95. 375, /* and height in pixels */
    96. HWND_DESKTOP, /* The window is a child-window to desktop */
    97. NULL, /* No menu */
    98. hThisInstance, /* Program Instance handler */
    99. NULL /* No Window Creation data */
    100. );
    101. /* Make the window visible on the screen */
    102. ShowWindow (hwnd, nFunsterStil);
    103. /* Run the message loop. It will run until GetMessage() returns 0 */
    104. while (GetMessage (&messages, NULL, 0, 0))
    105. {
    106. /* Translate virtual-key messages into character messages */
    107. TranslateMessage(&messages);
    108. /* Send message to WindowProcedure */
    109. DispatchMessage(&messages);
    110. }
    111. /* The program return-value is 0 - The value that PostQuitMessage() gave */
    112. return messages.wParam;
    113. }
    114. /* This function is called by the Windows function DispatchMessage() */
    115. LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    116. {
    117. HWND numedit[9];
    118. switch (message) /* handle the messages */
    119. {
    120. case WM_CREATE :{
    121. int x;
    122. for(int i = 1; i < 10; i++){
    123. x=10+(i*20);
    124. numedit[i] = CreateWindow("EDIT","", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,20,x,200,15,hwnd,0,
    125. ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;
    126. }
    127. }break;
    128. case WM_DESTROY:
    129. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
    130. break;
    131. case WM_COMMAND: {
    132. int iLenght;
    133. char * text;
    134. for(int i=1;i<10;i++){
    135. iLenght = GetWindowTextLength(numedit[i])+1;
    136. text = new char[iLenght+1];
    137. GetWindowText(numedit[i],text ,iLenght);
    138. num[i]=text;
    139. }
    140. }break;
    141. default:
    142. return DefWindowProc (hwnd, message, wParam, lParam);
    143. }
    144. return 0;
    145. }
    Alles anzeigen


    Probiere es mal mit:

    Quellcode

    1. string num[10];

    Wenn du die 0 nicht benutzen möchtest...
    Du bist Terrorist, warum? Siehe hier