Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[WinApi] Obsługa komunikatu, a raczej jej brak

Ostatnio zmodyfikowano 2014-02-12 23:15
Autor Wiadomość
AssSoft
Temat założony przez niniejszego użytkownika
[WinApi] Obsługa komunikatu, a raczej jej brak
» 2014-02-12 10:45:21
Witajcie.
Pisząc program do obliczania pola i objętości ( wersja II, pierwsza była na konsole ) wszystko szło dobrze do chwili gdy...
...Komunikat nie działał! Obsługa komunikatu nawet nie następowała. Po prostu klikałem na przycisk i... Nic. Oto kod:
C/C++
/* Notes
Jednostka ogólna = 1cm
*/
#include <string>
#include <windows.h>
#include <windowsx.h>
#include <cstring>
#include <cstring>
#define prostokat 0
#define trojkat 1
#define romb 2
#define trapez 3
#define rownoleglobok 4

#define ID_hm2dp    1001
#define ID_hm3dp   1002
#define ID_hsave1p  1003
#define ID_hinfo1st 1004
#define ID_herri1st 1005
#define ID_hwymXtx  1006
#define ID_hwymYtx  1007
#define ID_htype1se 1008
#define ID_htype2se 1009
#define ID_hmtypese 10010
#define ID_hresp 10011
#define ID_hsend1p 10012
//
#define ID2_hsend1p 100121
//
using namespace std;
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG Msg; /* A temporary location for all messages */


// Zmienne:
//HWND:
HWND hmram;

HWND hm2dp;
HWND hm3dp;
HWND hsave1p;
HWND hsend1p;
HWND hsend2p;
HWND hinfo1st;
HWND herri1st;
HWND hwymXtx;
HWND hwymYtx;
HWND hwymHtx;
HWND htype1se;
HWND htype2se;
HWND htype3se;
HWND htype4se;
HWND hmtypese;
HWND hresp;
HWND hany1st;
HWND hany2st;
HWND hany3st;
HWND hany4st;
HWND hany5st;
HWND hany6st;
//int i dword
int X;
int Y;
int Xpoz;
int Ypoz;
int scal;
int acstan;
int wynik;

int je1I;
int je2I;
int je3I;
int je4I;
//string char i podobne
LPSTR je1;
LPSTR je2;
LPSTR je3;
LPSTR je4;
LPSTR col1;
LPSTR col2;
LPSTR war1;
LPSTR war2;
string diag;
LPSTR diagL;
//HPEN i HBRUSH

//inne
HINSTANCE hNewInstance;
HFONT hNormalFont =( HFONT ) GetStockObject( DEFAULT_GUI_FONT );
/* This is where all the input to the window goes to */
void DestroyAllWindows()
{
    DestroyWindow( hsave1p );
    DestroyWindow( hsend1p );
    DestroyWindow( hsend2p );
    DestroyWindow( hinfo1st );
    DestroyWindow( herri1st );
    DestroyWindow( hwymXtx );
    DestroyWindow( hwymYtx );
    DestroyWindow( hwymHtx );
    DestroyWindow( htype1se );
    DestroyWindow( htype2se );
    DestroyWindow( htype3se );
    DestroyWindow( htype4se );
    DestroyWindow( hmtypese );
    DestroyWindow( hm2dp );
    DestroyWindow( hm3dp );
    DestroyWindow( hresp );
    DestroyWindow( hany1st );
    DestroyWindow( hany2st );
    DestroyWindow( hany3st );
    DestroyWindow( hany4st );
    DestroyWindow( hany5st );
    DestroyWindow( hany6st );
}
int ob_prost()
{
    int jall1;
    int jall2;
    int wyn;
    GetWindowText( hwymXtx, war1, GetWindowTextLength( hwymXtx ) );
    GetWindowText( hwymYtx, war2, GetWindowTextLength( hwymYtx ) );
    jall1 = atoi( war1 );
    jall2 = atoi( war2 );
    je1I = ComboBox_GetCurSel( htype1se );
    je2I = ComboBox_GetCurSel( htype2se );
    je3I = ComboBox_GetCurSel( htype3se );
    je4I = ComboBox_GetCurSel( htype4se );
    switch( je1I )
    {
    case 0:
        je1 = "mm";
        jall1 = jall1 / 10;
        break;
    case 1:
        je1 = "cm";
        jall1 = jall1;
        break;
    case 2:
        je1 = "dm";
        jall1 = jall1 * 10;
        break;
    case 3:
        je1 = "m";
        jall1 = jall1 * 100;
        break;
    case 4:
        je1 = "km";
        jall1 = jall1 * 1000;
        break;
    }
    switch( je2I )
    {
    case 0:
        je2 = "mm";
        jall2 = jall2 / 10;
        break;
    case 1:
        je2 = "cm";
        jall2 = jall2;
        break;
    case 2:
        je2 = "dm";
        jall2 = jall2 * 10;
        break;
    case 3:
        je2 = "m";
        jall2 = jall2 * 100;
        break;
    case 4:
        je2 = "km";
        jall2 = jall2 * 1000;
        break;
    }
    wyn = jall1 * jall2;
    switch( je3I )
    {
    case 0:
        je3 = "mm";
        wyn = wyn * 100;
        break;
    case 1:
        je3 = "cm";
        wyn = wyn;
        break;
    case 2:
        je3 = "dm";
        wyn = wyn / 100;
        break;
    case 3:
        je3 = "m";
        wyn = wyn / 1000;
        break;
    case 4:
        je3 = "km";
        wyn = wyn / 1000000;
        break;
    }
    return wyn;
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam ) {
    switch( Message ) {
       
        /* Upon destruction, tell the main thread to stop */
    case WM_DESTROY: {
            PostQuitMessage( 0 );
            break;
        }
    case WM_COMMAND:
        {
            switch( wParam )
            {
            case ID_hm2dp:
                DestroyAllWindows();
                hmtypese = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
                CBS_DROPDOWNLIST, 102, 52, 100, 200, hwnd,( HMENU ) ID_hmtypese, hNewInstance, NULL );
                hresp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Reset", WS_CHILD | WS_VISIBLE, 1, 1, 100, 50, hwnd,( HMENU ) ID_hresp, hNewInstance, NULL );
                hsend1p = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Rozpocznij", WS_CHILD | WS_VISIBLE, 102, 1, 100, 50, hwnd,( HMENU ) ID_hsend1p, hNewInstance, NULL );
                hany1st = CreateWindowEx( 0, "STATIC", "Wybierz figurę:", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 52, 100, 25, hwnd, NULL, hNewInstance, NULL );
                SetWindowText( hany1st, "Wybierz figurę" );
                SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "prostokąt" );
                SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "trójkąt" );
                SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "romb" );
                SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "trapez" );
                SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "równoległobok" );
                break;
            case ID_hm3dp:
                DestroyAllWindows();
                break;
            case ID_hsend1p:
                {
                    acstan = ComboBox_GetCurSel( hmtypese );
                    if( acstan == CB_ERR )
                    {
                        break;
                    }
                    DestroyAllWindows();
                    switch( acstan )
                    {
                    case prostokat:
                        {
                            hsend1p = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Oblicz", WS_CHILD | WS_VISIBLE, 1, 1, 100, 50, hwnd,( HMENU ) ID2_hsend1p, hNewInstance, NULL );
                            hresp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Reset", WS_CHILD | WS_VISIBLE, 1, 51, 100, 25, hwnd,( HMENU ) ID_hresp, hNewInstance, NULL );
                            herri1st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "Oczekiwanie na dane...", WS_CHILD | WS_VISIBLE | SS_LEFT, 102, 1, 380, 50, hwnd, NULL, hNewInstance, NULL );
                            hany1st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "bok A", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 75, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            hany2st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "bok B", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 100, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            hany3st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "jednostka", WS_CHILD | WS_VISIBLE | SS_LEFT, 101, 51, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            hany4st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "wartość", WS_CHILD | WS_VISIBLE | SS_LEFT, 201, 51, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            htype1se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
                            CBS_DROPDOWNLIST, 101, 76, 100, 200, hwnd, NULL, hNewInstance, NULL );
                            htype2se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
                            CBS_DROPDOWNLIST, 101, 101, 100, 200, hwnd, NULL, hNewInstance, NULL );
                            hwymXtx = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
                            201, 76, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            hwymYtx = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
                            201, 101, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            hany5st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "wynik w", WS_CHILD | WS_VISIBLE | SS_LEFT, 1, 126, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            hany6st = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "kolor", WS_CHILD | WS_VISIBLE | SS_LEFT, 301, 51, 100, 25, hwnd, NULL, hNewInstance, NULL );
                            htype3se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
                            CBS_DROPDOWNLIST, 301, 76, 100, 200, hwnd, NULL, hNewInstance, NULL );
                            htype4se = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
                            CBS_DROPDOWNLIST, 301, 101, 100, 200, hwnd, NULL, hNewInstance, NULL );
                            hmtypese = CreateWindowEx( WS_EX_CLIENTEDGE, "COMBOBOX", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
                            CBS_DROPDOWNLIST, 101, 126, 100, 200, hwnd, NULL, hNewInstance, NULL );
                           
                            SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "mm" );
                            SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "cm" );
                            SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "dm" );
                            SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "m" );
                            SendMessage( htype1se, CB_ADDSTRING, 0,( LPARAM ) "km" );
                           
                            SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "mm" );
                            SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "cm" );
                            SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "dm" );
                            SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "m" );
                            SendMessage( htype2se, CB_ADDSTRING, 0,( LPARAM ) "km" );
                           
                            SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "mm2" );
                            SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "cm2" );
                            SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "dm2" );
                            SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "m2" );
                            SendMessage( hmtypese, CB_ADDSTRING, 0,( LPARAM ) "km2" );
                           
                            SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "czarny" );
                            SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "niebieski" );
                            SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "czerwony" );
                            SendMessage( htype3se, CB_ADDSTRING, 0,( LPARAM ) "zielony" );
                           
                            SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "czarny" );
                            SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "niebieski" );
                            SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "czerwony" );
                            SendMessage( htype4se, CB_ADDSTRING, 0,( LPARAM ) "zielony" );
                            break;
                        }
                    }
                    break;
                }
            case ID2_hsend1p: // oto wadliwy komunikat
                {
                    SetWindowText( herri1st, "Rozpoczęto wstępne obliczanie" );
                    if( ComboBox_GetCurSel( htype1se ) == CB_ERR || ComboBox_GetCurSel( htype2se ) == CB_ERR )
                    {
                        SetWindowText( herri1st, "Błąd - brak któregoś argumentu" );
                        break;
                    }
                    if( ComboBox_GetCurSel( htype3se ) == CB_ERR || ComboBox_GetCurSel( htype4se ) == CB_ERR )
                    {
                        SetWindowText( herri1st, "Błąd - brak któregoś argumentu" );
                        break;
                    }
                    if( ComboBox_GetCurSel( hmtypese ) == CB_ERR )
                    {
                        SetWindowText( herri1st, "Błąd - brak któregoś argumentu" );
                        break;
                    }
                    SetWindowText( herri1st, "Rozpoczęto obliczanie" );
                    wynik = ob_prost();
                    itoa( wynik, diagL, 10 );
                    SetWindowText( herri1st, strcat( "a * b = ", diagL ) );
                    break;
                }
            case ID_hresp:
                {
                    DestroyAllWindows();
                    hm2dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 2d", WS_CHILD | WS_VISIBLE, 1, 1, 250, 250, hwnd,( HMENU ) ID_hm2dp, hNewInstance, NULL );
                    hm3dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 3d", WS_CHILD | WS_VISIBLE, 1, 252, 250, 250, hwnd,( HMENU ) ID_hm3dp, hNewInstance, NULL );
                    hmram = CreateWindowEx( 0, "BUTTON", "czekam na dane...", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 488, 1, 500, 500, hwnd,( HMENU ) ID_hm3dp, hNewInstance, NULL );
                }
                break;
            }
            break;
        }
       
        /* All other messages (a lot of them) are processed using default procedures */
        default:
        return DefWindowProc( hwnd, Message, wParam, lParam );
    }
    return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
    WNDCLASSEX wc; /* A properties struct of our window */
   
    /* zero out the struct and set the stuff we want to modify */
    memset( & wc, 0, sizeof( wc ) );
    wc.cbSize = sizeof( WNDCLASSEX );
    wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
   
    /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
    wc.hbrBackground =( HBRUSH )( COLOR_WINDOW + 1 );
    wc.lpszClassName = "WindowClass";
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); /* Load a standard icon */
    wc.hIconSm = LoadIcon( NULL, IDI_APPLICATION ); /* use the name "A" to use the project icon */
   
    if( !RegisterClassEx( & wc ) ) {
        MessageBox( NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
        return 0;
    }
   
    hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, "WindowClass", "VaLC II", WS_VISIBLE | WS_SYSMENU,
    CW_USEDEFAULT, /* x */
    CW_USEDEFAULT, /* y */
    1000, /* width */
    600, /* height */
    NULL, NULL, hInstance, NULL );
   
    if( hwnd == NULL ) {
        MessageBox( NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
        return 0;
    }
    hm2dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 2d", WS_CHILD | WS_VISIBLE, 1, 1, 250, 250, hwnd,( HMENU ) ID_hm2dp, hInstance, NULL );
    hm3dp = CreateWindowEx( WS_EX_CLIENTEDGE, "BUTTON", "Figury 3d", WS_CHILD | WS_VISIBLE, 1, 252, 250, 250, hwnd,( HMENU ) ID_hm3dp, hInstance, NULL );
   
    hmram = CreateWindowEx( 0, "BUTTON", "czekam na dane...", WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 488, 1, 500, 500, hwnd,( HMENU ) ID_hm3dp, hInstance, NULL );
   
   
    SendMessage( hm2dp, WM_SETFONT,( WPARAM ) hNormalFont, 0 );
    SendMessage( hm3dp, WM_SETFONT,( WPARAM ) hNormalFont, 0 );
    /*
    This is the heart of our program where all input is processed and
    sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
    this loop will not produce unreasonably high CPU usage
    */
    hNewInstance = hInstance;
    while( GetMessage( & Msg, NULL, 0, 0 ) > 0 ) { /* If no error is received... */
        TranslateMessage( & Msg ); /* Translate key codes to chars if present */
        DispatchMessage( & Msg ); /* Send it to WndProc */
    }
    return Msg.wParam;
}

Wadliwy komunikat zwie się "ID2_hsend1p".
Prosiłbym o pomoc i odpowiedź na pytanie "Jakim cudem to nie działa?".
P-104498
alixir
» 2014-02-12 11:39:47
W funkcji
ob_prost()
 wskaźniki war1 i war2 nie wskazują na żaden adres, stąd funkcja
GetWindowText
 się wysypuje :)
Zmień to np. na:

C/C++
war1 = new char[ GetWindowTextLength( hwymXtx ) + 1 ];
war2 = new char[ GetWindowTextLength( hwymYtx ) + 1 ];
GetWindowText( hwymXtx, war1, GetWindowTextLength( hwymXtx ) + 1 );
GetWindowText( hwymYtx, war2, GetWindowTextLength( hwymYtx ) + 1 );

Z resztą ten sam błąd występuje tutaj:
itoa( wynik, diagL, 10 );

diagL jest niezainicjowanym wskaźnikiem.

W fukcji
ob_prost()
 jest jeszcze kolejny błąd. Mianowicie
switch( je3I )
je3I jest wartością okna wyboru koloru, a nie rodzaju wyniku.
Dodatkowo używasz dzielenia na liczbach int, więc często wynikiem będzie zero, jeśli chcesz go otrzymać w jednostkach większych niż podawanych.
P-104502
AssSoft
Temat założony przez niniejszego użytkownika
» 2014-02-12 18:20:48
Nie wiedziałem o większości z tych błędów, ale jest jeszcze jeden - którego nie wskazałeś, i nie mam pomysłu na czym polega.

Otóż program nawet NIE DOCHODZI do ob_prost, głównie dla tego nie zauważyłem tych błędów.
Program nie dochodzi nawet do SetWindowText (herri1st,"Rozpoczęto wstępne obliczanie");. W ogóle pomija ID2_hsend1p, dodałem nawet komunikat przed "Rozpoczęto wstępne obliczanie", nie pojawiał się.
P-104527
pekfos
» 2014-02-12 18:42:01
C/C++
#define ID2_hsend1p 100121
Za duża wartość.

C/C++
LPSTR je1;
LPSTR je2;
LPSTR je3;
LPSTR je4;
Powinno być LPCSTR.
P-104531
AssSoft
Temat założony przez niniejszego użytkownika
» 2014-02-12 23:15:47
Moje ostatnie drobne pytanie - Jakiego typu najlepiej użyć do obliczania i podawania wyników? mam zamiar rozbudować to i dodać małą ilustracje w ramce ( Właśnie po to był wybór kolorów ) ale daleko nie zajdę jeśli wynikiem będzie prawie zawsze 0.

Z góry dziękuję za pomoc.
P-104567
« 1 »
  Strona 1 z 1