Created
December 14, 2012 20:23
-
-
Save anonymous/4288369 to your computer and use it in GitHub Desktop.
Sample Win32 project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
// Enable visual styles (XP and later) | |
#pragma comment(linker,"\"/manifestdependency:type='win32' \ | |
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ | |
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") | |
const int windowWidth = 500; | |
const int windowHeight = 90; | |
const int ID_BUTTON = 1; | |
HWND g_TextField; | |
LRESULT CALLBACK windowListener(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) | |
{ | |
switch (msg) | |
{ | |
case WM_CLOSE: | |
DestroyWindow(hwnd); | |
break; | |
case WM_DESTROY: | |
PostQuitMessage(0); | |
break; | |
case WM_COMMAND: | |
// When the button is pressed, retrieve the text from the text field and show it | |
if (LOWORD(wParam) == ID_BUTTON) { | |
LPCSTR buffer[100]; | |
SendMessage(g_TextField, WM_GETTEXT, sizeof(buffer), (LPARAM) buffer); | |
MessageBox(0, (LPCSTR) buffer, "Your message", MB_ICONINFORMATION); | |
} | |
break; | |
default: | |
return DefWindowProc(hwnd, msg, wParam, lParam); | |
} | |
} | |
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) | |
{ | |
// Create simple new class for the window | |
WNDCLASS windowClass; | |
memset(&windowClass, 0, sizeof(windowClass)); | |
windowClass.lpfnWndProc = windowListener; | |
windowClass.hInstance = hInstance; | |
windowClass.lpszClassName = "SimpleWindow"; | |
RegisterClass(&windowClass); | |
// Calculate the position of the window to center it | |
RECT screenRect; | |
GetWindowRect(GetDesktopWindow(), &screenRect); | |
int x = screenRect.right / 2 - windowWidth / 2; | |
int y = screenRect.bottom / 2 - windowHeight / 2; | |
// Create a visible non-resizable window | |
DWORD style = WS_OVERLAPPEDWINDOW - (WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME); | |
HWND window = CreateWindow("SimpleWindow", "Simple Window", style | WS_VISIBLE, x, y, windowWidth, windowHeight, NULL, NULL, hInstance, NULL); | |
// Create a text field | |
g_TextField = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE, 10, 10, windowWidth - 194, windowHeight - 56, window, NULL, NULL, NULL); | |
SendMessage(g_TextField, WM_SETTEXT, NULL, (LPARAM) "Hello, world!"); | |
HFONT textFont = CreateFont(26, 0, 0, 0, 0, FALSE, FALSE, FALSE, 0, 0, 0, 0, 0, "Arial"); | |
SendMessage(g_TextField, WM_SETFONT, (WPARAM) textFont, TRUE); | |
// Create a button | |
HWND button = CreateWindow("BUTTON", "Show message", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, windowWidth - 174, 10, 150, windowHeight - 56, window, (HMENU) ID_BUTTON, NULL, NULL); | |
HFONT buttonFont = CreateFont(16, 0, 0, 0, 700, FALSE, FALSE, FALSE, 0, 0, 0, 0, 0, "Arial"); | |
SendMessage(button, WM_SETFONT, (WPARAM) buttonFont, TRUE); | |
// Handle events | |
MSG msg; | |
while (GetMessage(&msg, NULL, 0, 0)) { | |
TranslateMessage(&msg); | |
DispatchMessage(&msg); | |
} | |
// Clean up | |
DeleteObject(buttonFont); | |
DeleteObject(textFont); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment