WinAPI: Khi click vào textbox hay button... thì tự tắt!

Mọi người ơi, có thể xem giúp mình đoạn code sau không, mình lập trình giải phương trình bậc 3 nhưng chả hiểu sao cứ click vào textbox để nhập số thì chương trình lại tự tắt :frowning:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif

#include <tchar.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "Window App";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	PSTR lpszArgument, int nFunsterStil)
{
	HWND hwnd;
	MSG msg;
	WNDCLASSEX wincl;

	wincl.hInstance = hInstance;
	wincl.lpszClassName = szClassName;
	wincl.lpfnWndProc = WndProc;
	wincl.style = CS_DBLCLKS;
	wincl.cbSize = sizeof(WNDCLASSEX);

	wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
	wincl.lpszMenuName = NULL;
	wincl.cbClsExtra = 0;
	wincl.cbWndExtra = 0;

	wincl.hbrBackground = CreateSolidBrush(RGB(0, 220, 0));

	if (!RegisterClassEx(&wincl))
		return 0;

	hwnd = CreateWindowEx(
		0,
		szClassName,
		"Calculator",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		500,
		300,
		HWND_DESKTOP,
		NULL,
		hInstance,
		NULL
	);

	ShowWindow(hwnd, nFunsterStil);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

#define button1 1
#define button2 2
#define text5 7
#define text4 6
#define text1 3
#define text2 4
#define text3 5

HWND bttinh, btreset, texta, textb, textc, textd, textkq, thongbao;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static wchar_t *notice;

	switch (msg)
	{
	case WM_CREATE:
	{
		bttinh = CreateWindow(TEXT("button"), TEXT("Tinh"),
			WS_VISIBLE | WS_CHILD, 370, 35, 70, 40, hwnd, (HMENU)button1, NULL, NULL);

		btreset = CreateWindow(TEXT("button"), TEXT("Lam lai"),
			WS_VISIBLE | WS_CHILD, 370, 90, 70, 40, hwnd, (HMENU)button2, NULL, NULL);

		texta = CreateWindow(TEXT("edit"), TEXT(""),
			WS_VISIBLE | WS_CHILD, 60, 35, 35, 25, hwnd, (HMENU)text1, NULL, NULL);

		textb = CreateWindow(TEXT("edit"), TEXT(""),
			WS_VISIBLE | WS_CHILD, 135, 35, 35, 25, hwnd, (HMENU)text2, NULL, NULL);

		textc = CreateWindow(TEXT("edit"), TEXT(""),
			WS_VISIBLE | WS_CHILD, 210, 35, 35, 25, hwnd, (HMENU)text3, NULL, NULL);
		textd = CreateWindow(TEXT("edit"), TEXT(""),
			WS_VISIBLE | WS_CHILD, 295, 35, 35, 25, hwnd, (HMENU)text3, NULL, NULL);
		textkq = CreateWindow(TEXT("edit"), TEXT(""),
			WS_VISIBLE | WS_CHILD, 60, 70, 270, 100, hwnd, (HMENU)text3, NULL, NULL);
		thongbao= CreateWindowW(L"STATIC", notice, 
		WS_CHILD | WS_VISIBLE | SS_LEFT, 60, 70, 200, 100, hwnd, (HMENU)1, NULL, NULL);
		break;
	}
	case WM_COMMAND:
	{
		char s_a[50] = "0", s_b[50] = "0", s_c[50] = "0", s_d[50] = "0", s_nghiem1[50] = "0", s_nghiem2[50] = "0", s_nghiem3[50] = "0";
		float a, b, c, d, delta, nghiem1, nghiem2, nghiem3;
		SendMessage((HWND)texta, (UINT)EM_GETLINE, (WPARAM)1, (LPARAM)&s_a);
		SendMessage((HWND)textb, (UINT)EM_GETLINE, (WPARAM)1, (LPARAM)&s_b);
		SendMessage((HWND)textb, (UINT)EM_GETLINE, (WPARAM)1, (LPARAM)&s_c);
		SendMessage((HWND)textb, (UINT)EM_GETLINE, (WPARAM)1, (LPARAM)&s_d);

		a = atof(s_a);
		b = atof(s_b);
		c = atof(s_c);
		d = atof(s_d);
		if (LOWORD(wParam) == button1){
			if (a == 0)
			{
				delta = pow(c, 2) - 4 * b*d;
				if (delta == 0)
				{
					nghiem1 = -c / b;
				};
				if (delta > 0)
				{
					wchar_t*notice = L"aaaaaaaaaaaa";
					nghiem1 = (-c + sqrt(delta)) / (2 * b);
					nghiem2 = (-c - sqrt(delta)) / (2 * b);
				};
				if (delta < 0)
				{
					nghiem1 = (-c + sqrt(delta)) / (2 * b);
					nghiem2 = (-c - sqrt(delta)) / (2 * b);
				};
				sprintf_s(s_nghiem1, "%f", nghiem1);
				sprintf_s(s_nghiem2, "%f", nghiem2);
				SendMessage((HWND)textkq, (UINT)WM_SETTEXT, (WPARAM)1, (LPARAM)&nghiem1);
				SendMessage((HWND)textkq, (UINT)WM_SETTEXT, (WPARAM)1, (LPARAM)&nghiem2);

			}
		}

		if (LOWORD(wParam) == button2)
		{
			ZeroMemory(s_a, sizeof(s_a));
			ZeroMemory(s_b, sizeof(s_b));
			ZeroMemory(s_c, sizeof(s_c));
			ZeroMemory(s_d, sizeof(s_d));
			ZeroMemory(s_nghiem1, sizeof(s_nghiem1));
			ZeroMemory(s_nghiem2, sizeof(s_nghiem2));
			ZeroMemory(s_nghiem3, sizeof(s_nghiem3));
			InvalidateRect(hwnd, NULL, TRUE);
			UpdateWindow(hwnd);
		}

	}

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}
1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?