Pereiti prie turinio

C++ Win32 "Sveikas pasauli"


Svečias Aliarth

Rekomenduojami pranešimai

Svečias Aliarth

Sveiki. Ziuriu cia niekas nekalba apie C++ aplikacijas, tai sakau imesiu viena lengviausiu jos pavizdi, gal kas susidomesite. :)

 

Si C++ koda irasant ir sukompiliuojant:

#include <windows.h>
#include <sstream>

using namespace std;


// ==========================================================================================
//                                        LOAD DATA
// ==========================================================================================
// ============================================================
//                        LOAD BITMAP
// ============================================================
HBITMAP load_bitmap(string file_url)
{
return (HBITMAP)LoadImage(NULL, file_url.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}
// ============================================================
//                         LOAD ICON
// ============================================================
HICON load_icon(string file_url)
{
return (HICON)LoadImage(NULL, file_url.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE|LR_DEFAULTSIZE|LR_SHARED);
}
// ============================================================
// ==========================================================================================


// ==========================================================================================
//                                       CREATE PROGRAM
// ==========================================================================================
// ============================================================
//                     CREATE PROGRAM CLASS
// ============================================================
bool create_program_class(string class_name = "", WNDPROC class_procedure = NULL, HINSTANCE class_hinstance = NULL, string class_icon_url = "", string class_cursor_url = "", int class_background_color = 0, string class_background_image = "")
{
WNDCLASSEX wincl;                                    // Data structure for the windowclass
ZeroMemory(&wincl, sizeof(WNDCLASSEX));

/* The Window structure */
wincl.hInstance = class_hinstance;
wincl.lpszClassName = class_name.c_str();
wincl.lpfnWndProc = class_procedure;         // This function is called by windows
wincl.style = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;      // Catch double-clicks
wincl.cbSize = sizeof(WNDCLASSEX);

HICON window_icon = !class_icon_url.empty()?load_icon(class_icon_url):LoadIcon(NULL, IDC_CROSS);
wincl.hIcon = window_icon;
wincl.hIconSm = window_icon;

HCURSOR window_cursor = !class_cursor_url.empty()?load_icon(class_cursor_url):LoadCursor(NULL, IDC_ARROW);
wincl.hCursor = window_cursor;

HBRUSH window_background = !class_background_image.empty()?CreatePatternBrush(load_bitmap(class_background_image)):(HBRUSH)CreateSolidBrush(class_background_color);
wincl.hbrBackground = window_background;

wincl.lpszMenuName = NULL;                 // No menu
wincl.cbClsExtra = 0;                      // No extra bytes after the window class
wincl.cbWndExtra = 0;                      // structure or the window instance

return RegisterClassEx(&wincl)?true:false;
}
// ============================================================
//                   CREATE PROGRAM WINDOW
// ============================================================
HWND create_program_window(string window_name = "", HINSTANCE window_hinstance = NULL, int window_x = CW_USEDEFAULT, int window_y = CW_USEDEFAULT, int window_width = 400, int window_height = 400, HMENU window_menu = NULL)
{
HWND window_hwnd = CreateWindowEx(
0,                          // Extended possibilites for variation
window_name.c_str(),        // Classname
window_name.c_str(),        // Title Text

WS_OVERLAPPEDWINDOW,        // default window

window_x,                   // Windows decides the position
window_y,                   // where the window ends up on the screen

window_width,               // The programs width
window_height,              // and height in pixels

HWND_DESKTOP,               // The window is a child-window to desktop
window_menu,                // Program menu
window_hinstance,          // Program Instance handler
NULL                        // No Window Creation data
);

ShowWindow(window_hwnd, SW_SHOW);
UpdateWindow(window_hwnd);

return window_hwnd;
}
// ============================================================
//                       CREATE PROGRAM
// ============================================================
HWND create_program(string program_name, WNDPROC program_procedure, HINSTANCE program_hinstance)
{
HWND program_hwnd = NULL;

if (create_program_class(program_name, program_procedure, program_hinstance))
{
program_hwnd = create_program_window(program_name);
}

return program_hwnd;
}
// ============================================================
// ==========================================================================================


// ==========================================================================================
//                                      CREATE FROMS
// ==========================================================================================
// ============================================================
//                     CREATE FROM INPUT
// ============================================================
HWND create_form_input(HWND parent_window_hwnd = NULL, string window_text = "", HINSTANCE window_hinstance = NULL, int window_x = 0, int window_y = 0, int window_width = 200, int window_height = 40)
{
// Jeigu nieko nekeiciama nustatyti parent_window_hwnd lango dydi (per visa ekrana)
if (window_x == 0 && window_y == 0 && window_width == 200 && window_height == 40)
{
RECT parent_window_rect;
 if (GetClientRect(parent_window_hwnd, &parent_window_rect))
 {
 window_x = parent_window_rect.left;
 window_y = parent_window_rect.top;
 window_width = parent_window_rect.right - parent_window_rect.left;
 window_height = parent_window_rect.bottom - parent_window_rect.top;
 }
}

HWND window_hwnd = CreateWindowEx(
0,                          // Extended possibilites for variation
"EDIT",                     // Classname
window_text.c_str(),        // Title Text

WS_CHILD|WS_VISIBLE|ES_LEFT|ES_MULTILINE|WS_VSCROLL|ES_AUTOHSCROLL,     // window styles

window_x,                   // Windows decides the position
window_y,                   // where the window ends up on the screen

window_width,               // The programs width
window_height,              // and height in pixels

parent_window_hwnd,         // The window is a child-window to desktop
NULL,                // Program menu
window_hinstance,          // Program Instance handler
NULL                        // No Window Creation data
);

ShowWindow(window_hwnd, SW_SHOW);
UpdateWindow(window_hwnd);

return window_hwnd;
}
// ============================================================
// ==========================================================================================





// ==========================================================================================
//                                         PROGRAM
// ==========================================================================================
bool EXIT = false;
HINSTANCE program_hinstance;
HWND program_hwnd;

// ============================================================
//                       PROGRAM PROCEDURE
// ============================================================
LRESULT CALLBACK program_procedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
EXIT = true;
break;
}

return DefWindowProc (hWnd, message, wParam, lParam);
}
// ============================================================
//                       PROGRAM WINAPI
// ============================================================
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
program_hwnd = create_program("Programa", program_procedure, program_hinstance);

string input_text = "Sveikas C++ Win32 Pasauli. ;D";
HWND form_input_hwnd = create_form_input(program_hwnd, input_text, program_hinstance);

MSG messages;
ZeroMemory(&messages, sizeof(MSG));
while (!EXIT)
{
 if (PeekMessage(&messages, NULL, 0, 0, PM_REMOVE)){TranslateMessage(&messages);DispatchMessage(&messages); continue;}

// Ciklas, kuri vygdo iki kol programa bus isjunkta arba gaus => EXIT = TRUE

Sleep(30);
}

PostQuitMessage(0);

return messages.wParam;
}
// ==========================================================================================


 

Bus sukurta si paprasta programa:

http://aliarth.lt/7223.png

Redagavo Aliarth
Nuoroda į pranešimą
Dalintis kituose puslapiuose
Svečias Aliarth

Tikrai ne lengviausias pavizdys, tiek kodo paprastam langui sukurti... :) mokymosi tikslais gal ir nieko, o šeip nepraktiška. Pasiimk kokį qt framework, tą pati langą sukursi 10x greičiau + multi OS :)

Tiek uzema, kad viskas ten butu kuo aiskiau bei kokybiskiau. Na bet jei jau nori trumpo kodo, tai stai pats trumpiausias sio "Sveikas C++ Win32 pasauli" kodas: <_<

 

#include <windows.h>
#include <sstream>

using namespace std;


// ==========================================================================================
//                                         PROGRAM
// ==========================================================================================
bool EXIT = false;

// ============================================================
//                       PROGRAM PROCEDURE
// ============================================================
LRESULT CALLBACK program_procedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
EXIT = true;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}
// ============================================================
//                       PROGRAM WINAPI
// ============================================================
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
WNDCLASSEX wincl;
ZeroMemory(&wincl, sizeof(WNDCLASSEX));

wincl.hInstance = hThisInstance;
wincl.lpszClassName = "Programa";
wincl.lpfnWndProc = program_procedure;
wincl.style = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.hIcon = LoadIcon(NULL, IDC_CROSS);
wincl.hIconSm = LoadIcon(NULL, IDC_CROSS);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.hbrBackground = (HBRUSH)CreateSolidBrush(0);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
if (!RegisterClassEx(&wincl)){EXIT = true;}

HWND hwnd = CreateWindowEx(0, "Programa", "Programa", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0, 0, 400, 400, HWND_DESKTOP, NULL, hThisInstance, NULL);

RECT rect; GetClientRect(hwnd, &rect);
HWND window_hwnd = CreateWindowEx(0, "EDIT", "Sveikas Win32 pasauli.", WS_CHILD|WS_VISIBLE|ES_LEFT|ES_MULTILINE|WS_VSCROLL|ES_AUTOHSCROLL, 0, 0, rect.right, rect.bottom, hwnd, NULL, hThisInstance, NULL );

MSG messages;
while (!EXIT)
{
 if (PeekMessage(&messages, NULL, 0, 0, PM_REMOVE)){TranslateMessage(&messages);DispatchMessage(&messages); continue;}
}

PostQuitMessage(0);
return messages.wParam;
}
// ==========================================================================================

Redagavo Aliarth
Nuoroda į pranešimą
Dalintis kituose puslapiuose
Svečias Aliarth

aš GARANTUOJU, kad tu pats nė 50% šito kodo nesupranti :D

Ant C++ kuriu programu API. O po to zaidima ant OpenGL ir DirectX esu numates sukurti kai turesiu visas reikiamas programas susikures. Suprantu tiek kiek man reikia, bei ta koda kur rasiau suprantu visu 100% antraip neaisku viskas ten butu, tik i prieki per mazai esu dar pasistumejas...

 

Stai mano pasistumejymas ant C++ Win32 ir C++ OpenGL:

http://aliarth.lt/5224.png

Redagavo Aliarth
Nuoroda į pranešimą
Dalintis kituose puslapiuose

Prisijunkite prie diskusijos

Jūs galite rašyti dabar, o registruotis vėliau. Jeigu turite paskyrą, prisijunkite dabar, kad rašytumėte iš savo paskyros.

Svečias
Parašykite atsakymą...

×   Įdėta kaip raiškusis tekstas.   Atkurti formatavimą

  Only 75 emoji are allowed.

×   Nuorodos turinys įdėtas automatiškai.   Rodyti kaip įprastą nuorodą

×   Jūsų anksčiau įrašytas turinys buvo atkurtas.   Išvalyti redaktorių

×   You cannot paste images directly. Upload or insert images from URL.

Įkraunama...
  • Dabar naršo   0 narių

    Nei vienas registruotas narys šiuo metu nežiūri šio puslapio.

×
×
  • Pasirinkite naujai kuriamo turinio tipą...