티스토리 뷰

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;

}


두 개의 에디트 박스에 숫자를 입력받고 이것을 기준으로 값을 계산하는 프로그램이다.


댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30