效果图:
// Grab_red_envelope.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <stdio.h>
#include <windows.h>
HWND Hwnd_ExeHwnd;
HANDLE hHandleArr[3];
DWORD money_total=0;
DWORD money_1=0;
DWORD money_2=0;
DWORD money_3=0;
//CRITICAL_SECTION cs; //临界区
HANDLE g_mutex; //互斥体
TCHAR szbuffer1[10];
TCHAR szbuffer2[10];
void init_edit()
{
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),"0");
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_1),"0");
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_2),"0");
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_3),"0");
}
DWORD WINAPI edit1_pro(LPVOID lpParameter)
{
while(TRUE)
{
//EnterCriticalSection(&cs);
WaitForSingleObject(g_mutex,-1);
if (money_total==0)
{
//LeaveCriticalSection(&cs);
ReleaseMutex(g_mutex);
break;
}
if (money_total<50)
{
money_1 +=money_total;
money_total=0;
}else
{
money_total -=50;
money_1 +=50;
}
memset(szbuffer1,0,10);
memset(szbuffer2,0,10);
sprintf(szbuffer1,"%d",money_1);
sprintf(szbuffer2,"%d",money_total);
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_1),szbuffer1);
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer2);
// LeaveCriticalSection(&cs);
ReleaseMutex(g_mutex);
Sleep(30);
}
return 0;
}
DWORD WINAPI edit2_pro(LPVOID lpParameter)
{
while(TRUE)
{
//EnterCriticalSection(&cs);
WaitForSingleObject(g_mutex,-1);
if (money_total==0)
{
// LeaveCriticalSection(&cs);
ReleaseMutex(g_mutex);
break;
}
if (money_total<50)
{
money_2 +=money_total;
money_total=0;
}else
{
money_total -=50;
money_2 +=50;
}
//此处进行操作编辑框.
memset(szbuffer1,0,10);
memset(szbuffer2,0,10);
sprintf(szbuffer1,"%d",money_2);
sprintf(szbuffer2,"%d",money_total);
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_2),szbuffer1);
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer2);
//LeaveCriticalSection(&cs);
ReleaseMutex(g_mutex);
Sleep(30);
}
return 0;
}
DWORD WINAPI edit3_pro(LPVOID lpParameter)
{
while(TRUE)
{
//EnterCriticalSection(&cs);
WaitForSingleObject(g_mutex,-1);
if (money_total<=0)
{
// LeaveCriticalSection(&cs);
ReleaseMutex(g_mutex);
break;
}
if (money_total<50)
{
money_3 +=money_total; //本人犯错误,把这里写成money1了。
money_total=0;
}else
{
money_total -=50;
money_3 +=50;
}
//此处进行操作编辑框.
memset(szbuffer1,0,10);
memset(szbuffer2,0,10);
sprintf(szbuffer1,"%d",money_3);
sprintf(szbuffer2,"%d",money_total);
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_3),szbuffer1);
SetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer2);
//LeaveCriticalSection(&cs);
ReleaseMutex(g_mutex);
Sleep(30);
}
return 0;
}
DWORD WINAPI edit_main_pro(LPVOID lpParameter)
{
//InitializeCriticalSection(&cs);
g_mutex=CreateMutex(NULL, FALSE, "XYZ");
memset(szbuffer1,0,10);
GetWindowText(GetDlgItem(Hwnd_ExeHwnd,IDC_EDIT_MAIN),szbuffer1,10);
sscanf(szbuffer1,"%d",&money_total);//
if (money_total <=0)
{
MessageBox(0,"红包总数不能为0",0,0);
return 0;
}
hHandleArr[0]=CreateThread(NULL,0,edit1_pro,NULL,0,NULL);
hHandleArr[1]=CreateThread(NULL,0,edit2_pro,NULL,0,NULL);
hHandleArr[2]=CreateThread(NULL,0,edit3_pro,NULL,0,NULL);
WaitForMultipleObjects(3,hHandleArr,FALSE,-1);//等待3个线程,只要有一个结束,就消息已通知。-1代表永远等待。
CloseHandle(hHandleArr[0]);
CloseHandle(hHandleArr[1]);
CloseHandle(hHandleArr[2]);
return 0;
}
BOOL CALLBACK MAIN_PROC(
HWND hwndDlg, // handle to dialog box
UINT uMsg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
Hwnd_ExeHwnd=hwndDlg;
switch(uMsg)
{
case WM_CLOSE:
EndDialog(hwndDlg,0);
break;
case WM_INITDIALOG :
init_edit();
break;
case WM_COMMAND :
switch(LOWORD(wParam))
{
case IDC_BUTTON_START:
HANDLE handle_main=CreateThread(NULL,0,edit_main_pro,NULL,0,NULL);
CloseHandle(handle_main);
break;
}
break;
}
return FALSE ;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DialogBox(hInstance,MAKEINTRESOURCE (IDD_DIALOG_MAIN),NULL,MAIN_PROC);
return 0;
}