Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: C++: dialogit

Sivun loppuun

Meitsi [03.03.2004 11:25:45]

#

Lueskelin yhtä winohjelmointikirjaa ja siinä puhuttiin dialogeista. Kirjan koodivinkit olivat kyllä aivan kamalaa purkkaa joten voisiko joku kertoa miten saisi esiin dialogin jossa olisi vain yläpalkki (ei ikonia, tekstinä vaikka: tää o dialogi, ei sulje; pienennä eikä suurenna nappeja), ei vierityspalkkeja, eikä sitä voi resizettaa. Ja sitte siinä ikkunassa olis nappi: sulje ja siinä olisi vielä joku lyhyt tekstinpätkä vaikka: tämä on turhadialogi.

thefox [03.03.2004 16:14:50]

#

Dialogi resurssitiedostossa olisi jotakuinkin tällainen:

IDD_DIALOG1 DIALOG DISCARDABLE  0, 0, 104, 55
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
CAPTION "huono diaalogi"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "turha nappi",IDOK,6,24,88,25
    CTEXT           "hejsan!",IDC_STATIC,7,6,81,14,SS_CENTERIMAGE
END

Sitten ei muuta kun DialogBoxParam:lla (tai DialogBox:lla [joka on makro DialogBoxParam:sta]) dialogi näkyviin.

Meitsi [04.03.2004 17:20:31]

#

käväsin msdnässä kopipastettamassa mutta ei näy dialokia.
tässä koodi

#include <windows.h>
HINSTANCE hThisInstance;
HWND hwndGoto;

int iLine;             // receives line number
BOOL fRelative;        // receives check box status

BOOL CALLBACK GoToProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    BOOL fError;

    switch (message)
    {
        case WM_INITDIALOG:
            CheckDlgButton(hwndDlg, IDCANCEL, fRelative);
            return TRUE;

        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                    MessageBox (NULL, "workki?" , " ", 0);
                    if (fError)
                    {
                        MessageBox (NULL, "virhe" , " ", 0);
                    }
                    else

                    // Notify the owner window to carry out the task.

                    return TRUE;

                case IDCANCEL:
                    DestroyWindow(hwndDlg);
                    hwndGoto = NULL;
                    return TRUE;
            }
    }
    return FALSE;
}

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

{
    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 light-gray as the background of the window */
    wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

    /* Register the window class, if fail 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 */
           "Windows App",         /* Title Text */
           WS_OVERLAPPEDWINDOW, /* 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 */
           );

    /* Make the window visible on the screen */
    ShowWindow(hwnd, nFunsterStil);
    /* 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);
    }

    BOOL bRet;

while ( (bRet = GetMessage(&messages, NULL, 0, 0)) != 0 )
{
    if (bRet == -1 )
    {
        // handle the error and possibly exit
    }
    else if (!IsWindow(hwndGoto) || !IsDialogMessage(hwndGoto, &messages))
    {
        TranslateMessage(&messages);
        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_COMMAND:
        switch (LOWORD(wParam))
        {
            case 12345:
                if (!IsWindow(hwndGoto))
                {
                    hwndGoto = CreateDialog(hThisInstance,
                         MAKEINTRESOURCE(12345),
                         hwnd, (DLGPROC) GoToProc);
                    ShowWindow(hwndGoto, SW_SHOW);
                }
                break;
        }
        return 0L;
           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;
}

ja resu

#include <windows.h>
500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "C:/DEV-C++/Bin/Mainicon.ico"
12345 DIALOG DISCARDABLE  0, 0, 104, 55
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
CAPTION "huono diaalogi"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "turha nappi",IDOK,6,24,88,25
    PUSHBUTTON "nap", IDCANCEL, 7, 30, 88, 25
END

EDIT: Korjailin kodia

thefox [06.03.2004 12:01:02]

#

Globaalille hThisInstance:lle et ainakaan ole asettanut kunnon arvoa.

Meitsi [06.03.2004 12:04:16]

#

öö?? Voisitko hieman selittää kun en ole mikään huippu c-koodaaja

thefox [06.03.2004 12:41:24]

#

Sulla on tuolla tokalla rivillä "HINSTANCE hThisInstance;" mutta et aseta sille missään vaiheessa arvoa. Sitten kuitenkin CreateDialog kutsussa käytät sitä. Eli pistä johonkin kohtaan WinMainia:

::hThisInstance = hThisInstance; // asetetaan globaaliin hThisInstance:en lokaalin hThisInstancen arvo

Meitsi [08.03.2004 16:04:49]

#

ei tuu dialogia.
kaikenlisäks se ohjelma ei sulkeudu vaan se jää tonne prosesseihin ja pitää sulkee käsin. koodi:

#include <windows.h>
HINSTANCE hThisInstance;
HWND hwndGoto;

int iLine;             // receives line number
BOOL fRelative;        // receives check box status

BOOL CALLBACK GoToProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    BOOL fError;

    switch (message)
    {
        case WM_INITDIALOG:
            CheckDlgButton(hwndDlg, IDCANCEL, fRelative);
            return TRUE;

        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                    MessageBox (NULL, "workki?" , " ", 0);
                    if (fError)
                    {
                        MessageBox (NULL, "virhe" , " ", 0);
                    }
                    else

                    // Notify the owner window to carry out the task.

                    return TRUE;

                case IDCANCEL:
                    DestroyWindow(hwndDlg);
                    hwndGoto = NULL;
                    return TRUE;
            }
    }
    return FALSE;
}

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

{
    ::hThisInstance = 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 light-gray as the background of the window */
    wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

    /* Register the window class, if fail 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 */
           "Windows App",         /* Title Text */
           WS_OVERLAPPEDWINDOW, /* 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 */
           );

    /* Make the window visible on the screen */
    ShowWindow(hwnd, nFunsterStil);
    /* 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);
    }

    BOOL bRet;

while ( (bRet = GetMessage(&messages, NULL, 0, 0)) != 0 )
{
    if (bRet == -1 )
    {
        // handle the error and possibly exit
    }
    else if (!IsWindow(hwndGoto) || !IsDialogMessage(hwndGoto, &messages))
    {
        TranslateMessage(&messages);
        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_COMMAND:
        switch (LOWORD(wParam))
        {
            case 12345:
                if (!IsWindow(hwndGoto))
                {
                    hwndGoto = CreateDialog(hThisInstance,
                         MAKEINTRESOURCE(12345),
                         hwnd, (DLGPROC) GoToProc);
                    ShowWindow(hwndGoto, SW_SHOW);
                }
                break;
        }
        return 0L;
           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;
}

thefox [08.03.2004 17:46:41]

#

Tuo pätkä on kyllä melkoisen täynään virheitä. Ensinnäkin, mikä on WM_COMMAND:n tarkoitus WindowProcedure-funktiossa? Nykyiselläänhän tuohon koodinpätkään ei koskaan päästä -> dialogia ei tietenkään näy.

Mikä muuten on tuon fErrorin tarkoitus? Koko koodi näyttää pahasti rip'n'rollatulta. Sellaista mitä ei tajua, ei kannata kopioida.

Tässä nyt kuitenkin edes jotensakin toimiva koodi:

#include <windows.h>
HINSTANCE hThisInstance;
HWND hwndGoto;

int iLine;             // receives line number
BOOL fRelative;        // receives check box status

BOOL CALLBACK GoToProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    BOOL fError;

    switch (message)
    {
        case WM_INITDIALOG:
            CheckDlgButton(hwndDlg, IDCANCEL, fRelative);
            return TRUE;

        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDOK:
                    MessageBox (NULL, "workki?" , " ", 0);
                    if (fError)
                    {
                        MessageBox (NULL, "virhe" , " ", 0);
                    }
                    else

                    // Notify the owner window to carry out the task.

                    return TRUE;

                    return TRUE;

                case IDCANCEL:
                    DestroyWindow(hwndDlg);
                    hwndGoto = NULL;
                    return TRUE;
            }
    }
    return FALSE;
}

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

{
    ::hThisInstance = 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 light-gray as the background of the window */
    wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);

    /* Register the window class, if fail 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 */
           "Windows App",         /* Title Text */
           WS_OVERLAPPEDWINDOW, /* 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 */
           );

    /* Make the window visible on the screen */
    ShowWindow(hwnd, nFunsterStil);
    /* 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);
    }

    BOOL bRet;

    /* 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_CREATE:
//        switch (LOWORD(wParam))
        {
  //          case 12345:
                if (!IsWindow(hwndGoto))
                {
                    hwndGoto = CreateDialog(hThisInstance,
                         MAKEINTRESOURCE(12345),
                         hwnd, (DLGPROC) GoToProc);
                    ShowWindow(hwndGoto, SW_SHOW);
                }
                break;
        }
        return 0L;
           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;
}

Meitsi [08.03.2004 19:30:13]

#

Joo nyt alko toimiin. Kiitti. Joo mä vaan kopipastetin msdnnästä.


Sivun alkuun

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta