[[oktatas:programozás:cpp|< CPP]]
====== Cpp Windows asztali környezet ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2011
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== Windowsos progi példák =====
#include
char *AppTitle = (char*)"Ablak";
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
int WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInst;
wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_WINDOWFRAME;
wc.lpszMenuName=NULL;
wc.lpszClassName=AppTitle;
if (!RegisterClass(&wc))
return 0;
hwnd = CreateWindow(AppTitle,AppTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,400,300,
NULL,NULL,hInst,NULL);
if (!hwnd)
return 0;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg,NULL,0,0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC dc;
RECT r;
GetClientRect(hwnd,&r);
dc=BeginPaint(hwnd,&ps);
TextOut(dc, 50, 50, "Helló itt vagyok", 16);
DrawText(dc,"Helló Világ",-1,&r,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
Fordítás:
g++ -mwindows main.cpp -o main
===== Nyomógomb eseménykezeléssel =====
#include
#define IDC_MAIN_BUTTON 101
char *AppTitle=(char*) "Ablak";
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
int WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInst;
wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_WINDOWFRAME;
wc.lpszMenuName=NULL;
wc.lpszClassName=AppTitle;
if (!RegisterClass(&wc))
return 0;
hwnd = CreateWindow(AppTitle,AppTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,400,300,
NULL,NULL,hInst,NULL);
if (!hwnd)
return 0;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg,NULL,0,0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CREATE:
{
HWND hWndButton=CreateWindowEx(NULL,
"BUTTON",
"OK",
WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
100,
100,
100,
24,
hwnd,
(HMENU)IDC_MAIN_BUTTON,
GetModuleHandle(NULL),
NULL);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
{
switch(LOWORD(wparam))
{
case IDC_MAIN_BUTTON:
{
MessageBox(NULL,
"Árvíztűrő tükörfúrógép",
"Információ",
MB_ICONINFORMATION);
break;
}
}
break;
}
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
===== Szövegmező tartalmát =====
#include
#define IDC_MAIN_BUTTON 101
#define IDC_MAIN_EDIT 102
char *AppTitle=(char*) "Ablak";
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
HWND hEdit;
int WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInst;
wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_WINDOWFRAME;
wc.lpszMenuName=NULL;
wc.lpszClassName=AppTitle;
if (!RegisterClass(&wc))
return 0;
hwnd = CreateWindow(AppTitle,AppTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,400,300,
NULL,NULL,hInst,NULL);
if (!hwnd)
return 0;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg,NULL,0,0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_CREATE:
{
HWND hWndButton=CreateWindowEx(NULL,
"BUTTON",
"OK",
WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
100,
130,
100,
24,
hwnd,
(HMENU)IDC_MAIN_BUTTON,
GetModuleHandle(NULL),
NULL);
hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
"EDIT",
"",
WS_CHILD|WS_VISIBLE|
ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
50,
10,
200,
100,
hwnd,
(HMENU)IDC_MAIN_EDIT,
GetModuleHandle(NULL),
NULL);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
{
switch(LOWORD(wparam))
{
case IDC_MAIN_BUTTON:
{
char buffer[256];
SendMessage(hEdit,
WM_GETTEXT,
sizeof(buffer)/sizeof(buffer[0]),
reinterpret_cast(buffer));
MessageBox(NULL,
buffer,
"Információ",
MB_ICONINFORMATION);
break;
}
}
break;
}
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
===== Linkek =====
* [[http://msdn.microsoft.com/en-us/library/windows/desktop/ff657751(v=vs.85).aspx]]
* [[http://msdn.microsoft.com/en-us/library/windows/desktop/bb775951(v=vs.85).aspx]] (Gomb stílusok)
* [[http://msdn.microsoft.com/en-us/library/windows/desktop/bb773169(v=vs.85).aspx]] (Komponensek)
* http://www.win32developer.com/tutorial.shtm
* http://www.win32developer.com/tutorial/windows/windows_tutorial_3.shtm
* http://mrbook.org/tutorials/make/ (Makefile)