Tartalomjegyzék

< CPP

Cpp Windows asztali környezet

Windowsos progi példák

main.cpp
#include <windows.h>
 
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

main.cpp
#include <windows.h>
 
#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

main.cpp
#include <windows.h>
 
#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<LPARAM>(buffer));
					MessageBox(NULL,
					           buffer,
					           "Információ",
					           MB_ICONINFORMATION);
					break;
				}
 
			}
			break;
		}
		default:
			return DefWindowProc(hwnd, msg, wparam, lparam);
	}
	return 0;
}

Linkek