windows代码

// ICONDEMO.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "ICONDEMO.h"
// 此代码模块中包含的函数的前向声明:
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
//TCHAR szAppName[] = TEXT("PopPad2");
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
 _In_opt_ HINSTANCE hPrevInstance,
 _In_ LPWSTR    lpCmdLine,
 _In_ int       nCmdShow)
{
 static TCHAR szAppName[] = TEXT("HexCalc");
 HWND hwnd;
 MSG  msg;
 WNDCLASS wndclass;
 wndclass.style = CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc = WndProc;
 wndclass.cbClsExtra = 0;
 wndclass.cbWndExtra = DLGWINDOWEXTRA;
 wndclass.hInstance = hInstance;
 wndclass.hIcon = LoadIcon(hInstance, szAppName);
 wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW);
 wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
 wndclass.lpszMenuName = NULL;
 wndclass.lpszClassName = szAppName;
 if (!RegisterClass(&wndclass))
 {
  MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
  return 0;
 }
 hwnd = CreateDialog(hInstance, szAppName, 0, NULL);;
 ShowWindow(hwnd, nCmdShow);
    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
   TranslateMessage(&msg);
   DispatchMessage(&msg);    
    }
 return  msg.wParam;
}
void ShowNumber(HWND hwnd, UINT iNumber)
{
 TCHAR szBuffer[20];
 wsprintf(szBuffer, TEXT("%X"), iNumber);
 SetDlgItemText(hwnd, VK_ESCAPE, szBuffer);
}
DWORD CalcIt(UINT iFirstNum, int iOperation, UINT iNum)
{
 switch (iOperation)
 {
 case '=': return iNum;
 case '+': return iFirstNum + iNum;
 case '-': return iFirstNum - iNum;
 case '*': return iFirstNum *  iNum;
 case '&': return iFirstNum &  iNum;
 case '|': return iFirstNum | iNum;
 case '^': return iFirstNum ^  iNum;
 case '<': return iFirstNum << iNum;
 case '>': return iFirstNum >> iNum;
 case '/': return iNum ? iFirstNum / iNum : MAXDWORD;
 case '%': return iNum ? iFirstNum % iNum : MAXDWORD;
 default:return 0;
 }
}
//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的:    处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 static BOOL bNewNumber = TRUE;
 static int iOperation = '=';
 static UINT iNumber, iFirstNum;
 HWND hButton;
    switch (message)
    {
 case WM_KEYDOWN:
  if (wParam != VK_LEFT)
   break;
  wParam = VK_BACK;
 case WM_CHAR:
  if ((wParam = (WPARAM)CharUpper((TCHAR*)wParam)) == VK_RETURN)
   wParam = '=';
  if (hButton = GetDlgItem(hwnd, wParam))
  {
   SendMessage(hButton, BM_SETSTATE, 1, 0);
   Sleep(100);
   SendMessage(hButton, BM_SETSTATE, 0, 0);
  }
  else
  {
   MessageBeep(0);
   break;
  }
 case WM_COMMAND:
  SetFocus(hwnd);
  if (LOWORD(wParam) == VK_BACK)
  {
   ShowNumber(hwnd, iNumber /= 16);
  }
  else if (LOWORD(wParam) == VK_ESCAPE)
  {
   ShowNumber(hwnd, iNumber = 0);
  }
  else if (isxdigit(LOWORD(wParam)))
  {
   if (bNewNumber)
   {
    iFirstNum = iNumber;
    iNumber = 0;
   }
   bNewNumber = FALSE;
   if (iNumber <= MAXDWORD >> 4)
   {
    ShowNumber(hwnd, iNumber = 16 * iNumber + wParam - (isdigit(wParam) ? '0' : 'A' - 10));
   }
   else
   {
    MessageBeep(0);
   }
  }
  else
  {
   if (!bNewNumber)
   {
    ShowNumber(hwnd, iNumber = CalcIt(iFirstNum, iOperation, iNumber));
   }
   bNewNumber = TRUE;
   iOperation = LOWORD(wParam);
  }
  return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
  return 0; 
    }
 return DefWindowProc(hwnd, message, wParam, lParam);
}
posted on 2020-05-17 10:45  201610007  阅读(211)  评论(0编辑  收藏  举报