Skocz do zawartości

Windows GDI


kt1117

Rekomendowane odpowiedzi

Mam taki kod:

 #include  <windows.h>
HWND uchwytdookna;               //uchwyt okna 
HDC hdc;
HPEN hpenZoltePioro;
HPEN hpenStarePioro;
HBRUSH pedzel=CreateSolidBrush(RGB (255,90,45));   //Procedura zdarzeniowa
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{    
switch (message)
     {        
          case WM_DESTROY:            
              PostQuitMessage (0);            
                          break;        
                 case WM_PAINT:        
                      hdc=BeginPaint(uchwytdookna,0);        
                      hpenZoltePioro = CreatePen(PS_SOLID, 50, RGB(0, 0, 0));    
                  hpenStarePioro = (HPEN) SelectObject(hdc, hpenZoltePioro);    
                  MoveToEx(hdc,0,0,0);        
                      LineTo (hdc,50,80);//czemu nie ma linii    
                  EndPaint(uchwytdookna,0);    
                  break;    
             default:        
                       return DefWindowProc (hwnd, message, wParam, lParam);    
         }     
return 0; 
} 
  //Klasa okna 
char szClassName[ ] = "CodeBlocksWindowsApp"; 
int WINAPI WinMain (HINSTANCE hThisInstance,    
                HINSTANCE hPrevInstance,    
                LPSTR lpszArgument,    
                int nCmdShow) 
            {    
                    MSG messages;
                WNDCLASSEX wincl;    
                    wincl.hInstance = hThisInstance;
                wincl.lpszClassName = szClassName;
                wincl.lpfnWndProc = WindowProcedure;
                wincl.style = CS_DBLCLKS;
                wincl.cbSize = sizeof (WNDCLASSEX);
                wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);    
                    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);    
                    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
                    wincl.lpszMenuName = NULL;    
                    wincl.cbClsExtra = 0;    
                    wincl.cbWndExtra = 0;    
                    wincl.hbrBackground = (HBRUSH) pedzel;     
                            if (!RegisterClassEx (&wincl))    

                                        return 0;     
  uchwytdookna = CreateWindowEx (    
                                              0,    
                                             szClassName,    
                                             "Tytuł",    
                                              WS_OVERLAPPEDWINDOW,    
                                                  CW_USEDEFAULT,        
                                                      CW_USEDEFAULT,    
                                              544,    
                                              375,        
                                                  HWND_DESKTOP,    
                                              NULL,
                                          hThisInstance,
                                          NULL    
                                            );        
ShowWindow (uchwytdookna, nCmdShow);    
  
while (GetMessage (&messages, NULL, 0, 0))
     {        
          TranslateMessage(&messages);    
      DispatchMessage(&messages);
     }     
  return messages.wParam;
}

i nie wywala błędu, lecz nie rysuje linii, dlaczego? (Dopiero uczę się GDI )

Odnośnik do komentarza
Udostępnij na innych stronach

Dziwi mnie ta linijka

 
#include HWND

 

Porównaj swój kod z kodem z tutoriala (MegaTutoral z Gamedev)

#include <string>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>


// nazwa klasy okna
std::string g_strKlasaOkna = "od0dogk_Window";

// dane okna
HDC g_hdcOkno;       // uchwyt kontekstu urządzenia okna


//------------------- procedura zdarzeniowa okna------------------------

LRESULT CALLBACK WindowEventProc(HWND hWnd, UINT uMsg,
                                WPARAM wParam, LPARAM lParam)
{
  switch (uMsg)
  {
        case WM_LBUTTONDOWN:
              // przejmujemy myszkę
              SetCapture (hWnd);

              // przesuwamy pióro (służące do rysowania po oknie)
              // w punkt kliknięcia
              MoveToEx (g_hdcOkno,
                        GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
                        NULL);

              // zwracamy zero
              return 0;

        case WM_MOUSEMOVE:
              // jeżeli nasze okno posiada myszkę
              if (GetCapture() == hWnd)
                    // rysujemy linie od poprzedniego do aktualnego
                    // miejsca kursora myszki
                    LineTo (g_hdcOkno,
                            GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));

              // zwracamy zero
              return 0;

        case WM_LBUTTONUP:
              // oddajemy władzę nad myszką do systemu
              ReleaseCapture();
              return 0;

        //-------------------------------------------------------------

        case WM_DESTROY:
              // kończymy program
              PostQuitMessage (0);
              return 0;
  }

  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


//----------------------- funkcja WinMain()----------------------------

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
  /* rejestrujemy klasę okna */

  WNDCLASSEX KlasaOkna;

  // wypełniamy strukturę WNDCLASSEX
  // (pomijamy z tego większość pól)
  KlasaOkna.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  KlasaOkna.style = CS_OWNDC;  // własny kontekst urządzenia okna

  // rejestrujemy klasę okna
  RegisterClassEx (&KlasaOkna);


  /* tworzymy okno */

  // tworzymy okno funkcją CreateWindowEx
  // (znana czynność, więc pomijamy (uchwyt trafia do hOkno))

  // pobieramy uchwyt do kontekstu urządzenia obszaru klienta okna
  g_hdcOkno = GetDC(hOkno);

  // pokazujemy nasze okno
  ShowWindow (hOkno, nCmdShow);


  /* pętla komunikatów */

  // (w zwyczajowej formie, darujemy ją sobie)

  // zwracamy kod wyjścia
  return static_cast<int>(msgKomunikat.wParam);
}

Odnośnik do komentarza
Udostępnij na innych stronach

Źle skopiowałem kod(Rozsypał się po wklejeniu na linijki), zaraz dam Edita.

E:

Naprawiłem i działa, zaraz dam edita i powiem jak.

E2:

Odnoszę się do dobrego uchwytu, kodu nie wkleje, bo nie chce mi się go potem układać.

E3:

Jeszcze ostatnie pytanie. Co zrobić, żeby w tle nie było okienka konsoli?

Odnośnik do komentarza
Udostępnij na innych stronach

Dzięki, działa. Ach - zapomniałem o jeszcze jednym, mianowicie, jak używam pliku .rc o treści:

200 MENU 
{
    POPUP "Nowy"
    {
    }
    POPUP "Zapisz"
    {
    }
    POPUP "Otwórz"
    {
    }
    POPUP ""
    {
    }
    POPUP "Importuj"
    {
        MENUITEM "Tekst", IDM_TEKST1
        POPUP "Obraz"
        {
            MENUITEM "JPEG", IDM_JPEG1
            MENUITEM "Bitmapa", IDM_BITMAPA1
        }
    }
    POPUP ""
    {
    }
    POPUP "Koniec"
    {
    }
}

w programie o kodzie:

#include <windows.h>
#include "res.rc"

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
             /* This is the handle for our window */
           /* 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 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 0;

    /* The class is registered, let's create the program*/
    HMENU hMenu = LoadMenu( hThisInstance, MAKEINTRESOURCE( 200) );
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template 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 */
           hMenu,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* 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);
    }

    /* 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_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;
}

to C::B wywala błąd:

w\ghhjggjgjgj\res.rc|16|error: expected unqualified-id before numeric constant|
||=== Build finished: 1 errors, 0 warnings ===|

Jak temu zaradzić?

E:Dobra już wiem, wystarczyło tylko usunąć linijkę #include "res.rc", teraz mam następny problem, a mianowicie mam trzy pliki:

-res.rc:

#include <windows.h>

IDD_PASEK DIALOGEX 0, 0, 98, 52
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION
EXSTYLE WS_EX_TOOLWINDOW
CAPTION "Mój pasek narzędzi"
FONT 8, "MS Sans Serif"
{
     PUSHBUTTON "&Pierwszy przycisk", IDC_PRZYC1, 7, 7, 84, 14
     PUSHBUTTON "&Drugi przycisk", IDC_PRZYC2, 7, 31, 84, 14
     PUSHBUTTON "&Trzeci przycisk", IDC_PRZYC3, 7, 31, 84, 14
}

-pasek.h:

#define IDD_PASEK  200
#define IDC_PRZYC1 201
#define IDC_PRZYC1 202
#define IDC_PRZYC1 203

oraz

- main.cpp:

#include <string>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>

HWND hPrzyc;
HWND hwnd;               /* This is the handle for our window */
    MSG messages;
    MSG msg;         /* Here messages to the application are saved */
    WNDCLASSEX wincl;
    HWND hPasek;
    HWND dial;
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DlgProc( HWND, UINT, WPARAM, LPARAM );

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nFunsterStil)
{
           /* 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 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 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template 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);
    hPrzyc = CreateWindowEx( 0, "BUTTON", "Dialog", WS_CHILD | WS_VISIBLE,
    5, 5, 50, 25, hwnd, NULL, hThisInstance, NULL );


    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
       if( !IsDialogMessage( hPasek, & msg ) )
         {
              TranslateMessage( & msg );
              DispatchMessage( & msg );
         }


    }

    /* 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:
        {
            // Tutaj wywołujemy nasz dialog
             CreateDialog(GetModuleHandle( NULL ), MAKEINTRESOURCE(200), hwnd,DlgProc );

        }
        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;
}
BOOL CALLBACK DlgProc( HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam )
{
    switch( Msg )
    {
    case WM_INITDIALOG:
        {

        }
        break;
    case WM_COMMAND:
        {// reakcja na przyciski

        }
        break;
        default: return FALSE;
    }
    return TRUE;
}

.

Problem tkwi w tym, iż nie kompiluje się, i wyskakuje błąd:

E:\Pulpit\DOKUME~1\PROJEK~1\GHHJGG~1\res.rc|9|syntax error|

, gdy wywale te "pushbuttony" (chodzi mi o te trzy linijki zaczynające się od słowa "pushbutton")

kompiluje się, lecz nie wyświetla niemodalnego dialogu, a okno jest "nieaktywne", tzn. nie reaguje nawet na zamknięcie krzyżykiem, ani alt+F4.

Odnośnik do komentarza
Udostępnij na innych stronach

Jeśli chcesz dodać odpowiedź, zaloguj się lub zarejestruj nowe konto

Jedynie zarejestrowani użytkownicy mogą komentować zawartość tej strony.

Zarejestruj nowe konto

Załóż nowe konto. To bardzo proste!

Zarejestruj się

Zaloguj się

Posiadasz już konto? Zaloguj się poniżej.

Zaloguj się
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...