티스토리 뷰
Win API를 통한 사칙연산기
메뉴바 |
다이얼로그 |
전체 소스 |
#include <windows.h>
#include <stdio.h> #include "resource.h" LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam); BOOL CALLBACK Dlg_Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam); HINSTANCE hInst; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS WndClass; hInst = hInstance; WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInstance; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU); WndClass.lpszClassName = "Example2"; RegisterClass(&WndClass); hwnd = CreateWindow("Example2", "Example2", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { switch (iMsg) { case WM_CREATE: break; case WM_COMMAND: switch (LOWORD(wParam)) { case ID_DIALOG: DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, Dlg_Proc); break; } break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hwnd, iMsg, wParam, lParam); } BOOL CALLBACK Dlg_Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam) { static char str[50] = "0"; static double a = NULL, b = NULL, result = 0; switch (iMsg) { case WM_INITDIALOG: //SetDlgItemText(hDlg, IDC_EDIT_INPUT_1, "0"); //SetDlgItemText(hDlg, IDC_EDIT_INPUT_2, "0"); SetDlgItemText(hDlg, IDC_EDIT_OUTPUT, str); return 1; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_BUTTON_ADD: GetDlgItemText(hDlg, IDC_EDIT_INPUT_1, str, sizeof(str)); a = atof(str); GetDlgItemText(hDlg, IDC_EDIT_INPUT_2, str, sizeof(str)); b = atof(str); result = a + b; sprintf_s(str, "%lf", result); SetDlgItemText(hDlg, IDC_EDIT_OUTPUT, str); break; case IDC_BUTTON_SUB: GetDlgItemText(hDlg, IDC_EDIT_INPUT_1, str, sizeof(str)); a = atof(str); GetDlgItemText(hDlg, IDC_EDIT_INPUT_2, str, sizeof(str)); b = atof(str); result = a - b; sprintf_s(str, "%lf", result); SetDlgItemText(hDlg, IDC_EDIT_OUTPUT, str); break; case IDC_BUTTON_MUL: GetDlgItemText(hDlg, IDC_EDIT_INPUT_1, str, sizeof(str)); a = atof(str); GetDlgItemText(hDlg, IDC_EDIT_INPUT_2, str, sizeof(str)); b = atof(str); result = a * b; sprintf_s(str, "%lf", result); SetDlgItemText(hDlg, IDC_EDIT_OUTPUT, str); break; case IDC_BUTTON_DIV: GetDlgItemText(hDlg, IDC_EDIT_INPUT_1, str, sizeof(str)); a = atof(str); GetDlgItemText(hDlg, IDC_EDIT_INPUT_2, str, sizeof(str)); b = atof(str); if (b == 0) b = 1; result = a / b; sprintf_s(str, "%lf", result); SetDlgItemText(hDlg, IDC_EDIT_OUTPUT, str); break; case IDC_EXIT: EndDialog(hDlg, 0); break; case IDCANCEL: EndDialog(hDlg, 0); break; } break; } return 0; } |
두 개의 에디트 박스에 숫자를 입력받고 이것을 기준으로 값을 계산하는 프로그램이다.
'Programming > C' 카테고리의 다른 글
dumpcode.h (0) | 2016.07.05 |
---|---|
fork와 pipe와 dup2 함수를 통한 표준 입력 (2) | 2016.06.23 |
GNU gcc의 특수한 C언어 문법 - Designated Initializers (0) | 2016.06.23 |
WinAPI 콤보박스(ComboBox)와 에디트컨트롤(Edit Control) 예제 (0) | 2015.12.07 |
Win API를 이용한 계산기 제작(1) (4) | 2015.12.07 |