今天学了Win32 汇编当中的事件.就跟着例子写了个Win32的小程序.把代码记下来.做为留念.
#include "counter.h"
#include "resource.h"
static BYTE dwOptionPause=0; //标志位,是否暂停.1 非暂停.
static BYTE dwOptionStop=1; //非开始.
static int i=1;
HWND hWndDlg;
HANDLE hEvent;
HANDLE hThread;
DWORD WINAPI counter(LPVOID lParam)
{
while (i)
{
SetDlgItemInt(hWndDlg,IDC_EDIT1,i,true);
i++;
WaitForSingleObject(hEvent,INFINITE);
}
return 0;
}
INT_PTR CALLBACK WindowProc(HWND hWnd,UINT uMsg,WPARAM wParam ,LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
{
if (dwOptionStop)
{
SetEvent(hEvent);//设置为有信号状态.
hThread = CreateThread(NULL,0,counter,NULL,0,NULL);//创建线程.
EnableWindow(GetDlgItem(hWndDlg,IDCANCEL),true);
SetDlgItemText(hWnd,IDOK,_T("停止"));
dwOptionStop=0;
}
else
{
SetDlgItemText(hWnd,IDOK,_T("计数"));
EnableWindow(GetDlgItem(hWndDlg,IDCANCEL),false);//禁用.
CloseHandle(hThread);
ResetEvent(hEvent);
dwOptionStop=1;
}
}
break;
case IDCANCEL:
{
if (dwOptionPause)//处于暂停状态.
{
SetEvent(hEvent);//置为有信号状态.
dwOptionPause =0;
}
else
{
ResetEvent(hEvent);//置为无信号状态.
dwOptionPause=1;
}
}
break;
default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
}
break;
case WM_INITDIALOG:
{
hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);//创建事件对象.
EnableWindow(GetDlgItem(hWnd,IDCANCEL),false);
hWndDlg = hWnd;
}
break;
default:
DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
LPCWSTR lpTemplate = MAKEINTRESOURCE(IDD_DIALOG_MAIN);
return DialogBoxParam(hInstance,lpTemplate,NULL,WindowProc,NULL);
}