[C++] Program w oknie - kilka prostych pytań

0

W Dev-C++ utworzyłem projekt jako windows application i dostałem taki kod:

#include <windows.h>

/*  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 Windows's default color 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 */
           "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);
    }

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

Efekt:
user image

  1. Jak dodać górne menu (plik, edycja, widok itp.)?
  2. Jak wstawić tekst w środku okna?
  3. Jak wstawić obraz np. images/logo.png?
  4. Jak zrobić ikonę programu (w sensie dodać, zrobić wiem że się da w Gimpie ;) )?
0

Poczytać na www.winapi.org

0

Ok, dzięki, chętnie poczytam ;-)

Mam jeszcze jedno pytanie:

class file {
      public:
             char actual_filename;
             
             void file_get_contents();
             void file_put_contents();
             void filesize();
}

file::file_get_contents(filename)
{
      //
}

file::file_put_contents(filename,content)
{
      //
}

file::filesize(filename)
{
      //
}
10 `file file::file_get_contents' is not a static member of `class file'
10 `filename' was not declared in this scope
11 expected `,' or `;' before '{' token
15 expected constructor, destructor, or type conversion before '(' token
15 expected `,' or `;' before '(' token
20 expected constructor, destructor, or type conversion before '(' token
20 expected `,' or `;' before '(' token

Wszędzie, gdzie czytałem, klasy są deklarowane właśnie w ten sposób. Dlaczego w devie pojawiają się te błędy?

0

Poczytaj sobie najpierw o programowaniu obiektowym w C++ bo widze ze nie wiesz jak poprawnie to zapisac...

  1. Średnik za klasą
  2. typ zwracany przed nazwa metody
  3. W deklaracji nie masz parametrow, a w definicji juz chcesz je miec - cos jest nie tak ...
  4. Dane maja byc prywatne - odwolujesz sie do nich poprzez metody - inaczej mowiac Hermetyzacja
0

Zapoznałem się z tą stroną. Jest problem z menu:
http://www.winapi.org/index.php?option=com_content&task=view&id=48&Itemid=26 - nie mam "insert" :-[
Mam Visual Studio 2008 Express Edition
Autor niestety nie podał innego sposobu, tylko przez to menu, którego u mnie nie ma...
Jak wstawić menu do tego kodu:

#include <windows.h>

static TCHAR lpszAppName[] = TEXT("API Windows");

LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		case WM_CREATE:
		break;

		case WM_DESTROY:
			PostQuitMessage(0);
		break;

		case WM_KEYDOWN:
		break;

		default:
			return (DefWindowProc(hWnd, uMsg, wParam, lParam));
	}
	return(0L);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{ 

	MSG msg;
	WNDCLASS wndclass;
	HWND hWnd; 

	wndclass.style = CS_HREDRAW | CS_VREDRAW; 
	wndclass.lpfnWndProc = MainWndProc; 
	wndclass.cbClsExtra = 0; 
	wndclass.cbWndExtra = 0; 
	wndclass.hInstance = hInstance; 
	wndclass.hIcon = NULL; 
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
	wndclass.lpszMenuName = NULL; 
	wndclass.lpszClassName = lpszAppName; 

	if(RegisterClass(&wndclass) == 0)    
	{
		return FALSE;  
	}

	hWnd = CreateWindow(lpszAppName, lpszAppName, 
	WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
	CW_USEDEFAULT, CW_USEDEFAULT,
	CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 
	
	if(hWnd == NULL)
	{
		return FALSE;  
	}

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
} 
0

Olej to WinAPI, zainstaluj QtCreatora, albo jakąś inną zabawkę związaną z okienkami (WxWidgets na przykład).

0

Stwórz sobie okno dialogowe i umieść w zasobach programu. Wygodniej niż tworzyć okno dynamicznie, dodatkowo nie musisz deklarować klasy okna, więc kod krótszy.

1 użytkowników online, w tym zalogowanych: 0, gości: 1