⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winmain.c

📁 最新IAR6.4软集成开发环境及破解文件
💻 C
📖 第 1 页 / 共 4 页
字号:
      s++;
    }
    if (*s == LOG_COLOR_RED) {
      SetTextColor(hdc, RGB(255, 0, 0));
      s++;
    }
    Len = 0;
    while (*(s + Len) && (*(s + Len) != '\n') && (*(s + Len) != LOG_COLOR_RED))
      Len++;
    DrawText(hdc, s, Len, &rt, DT_LEFT);
    GetTextExtentPoint32(hdc, s, Len, &SegSize);
    rt.left += SegSize.cx;
    SetTextColor(hdc, RGB(0, 0, 0));
    s += Len;
  }
}

/*********************************************************************
*
*       _LOG_Clear
*/
static void _LOG_Clear(void) {
  _LOG_acBuffer[0] = 0;
}

/*********************************************************************
*
*       _LOG_DelFirstLine
*/
static void _LOG_DelFirstLine(void) {
  int i;
  for (i = 0; _LOG_acBuffer[i]; i++) {
    if (_LOG_acBuffer[i]=='\n') {
      strcpy(&_LOG_acBuffer[0], &_LOG_acBuffer[i + 1]);
      return;
    }
  }
}

/*********************************************************************
*
*       _LOG_AddPlain
*/
static void _LOG_AddPlain(const char*s) {
  while (strlen(_LOG_acBuffer) + strlen(s) > sizeof(_LOG_acBuffer)) {
    _LOG_DelFirstLine();
  }
  strcpy(&_LOG_acBuffer[strlen(_LOG_acBuffer)], s);
  if (_LOG_hWnd) {
    InvalidateRect(_LOG_hWnd, NULL, TRUE);
  }
}

/*********************************************************************
*
*       _LOG_Add
*/
static void __cdecl _LOG_Add(const char *format ,... ) {
  char ac[200];
  va_list arglist;
  va_start(arglist, format);
  sprintf (ac, format
           , va_arg(arglist,int)
           , va_arg(arglist,int)
           , va_arg(arglist,int)
           , va_arg(arglist,int)
           , va_arg(arglist,int));
  _LOG_AddPlain(ac);
};

/*********************************************************************
*
*       _LOG_AddRed
*/
static void _LOG_AddRed(void) {
  char ac[200];
  ac[0] = LOG_COLOR_RED;
  ac[1] = 0;
  _LOG_Add(&ac[0]);
}

/*********************************************************************
*
*       _WndProcLog
*/
static LRESULT CALLBACK _WndProcLog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  PAINTSTRUCT ps;
  switch (message) {
    case WM_PAINT:
      {
        HDC hdc = BeginPaint(hWnd, &ps);
        _LOG_Paint(hWnd, hdc);
      }
      EndPaint(hWnd, &ps);
      break;
    case WM_DESTROY: _LOG_hWnd = 0; break;
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

/*********************************************************************
*
*       _LOG_Init
*/
static void _LOG_Init(HINSTANCE hInst, HICON hIcon) {
  static char IsInitialized=0;
  if (!IsInitialized) {
    WNDCLASSEX wcex;
    IsInitialized =1;
    memset (&wcex, 0, sizeof(wcex));
    wcex.cbSize       = sizeof(WNDCLASSEX); 
    wcex.hInstance    = hInst;
    wcex.style        = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc  = (WNDPROC)_WndProcLog;
    wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName= acClassNameLog;
    wcex.hIcon        = hIcon;
    wcex.hCursor      = LoadCursor(NULL, IDC_ARROW);
    RegisterClassEx(&wcex);
  }
}

/*********************************************************************
*
*       _LOG_Create
*/
static void _LOG_Create(HINSTANCE hInst, HWND hWndParent) {
  if (_LOG_hWnd !=0)  // Create only one instance of this window
    return;
  _LOG_Init(hInst,0);
  _LOG_hWnd = CreateWindow(acClassNameLog, "Log",
                           WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME 
                           | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CHILD | WS_CLIPSIBLINGS
                           | WS_VISIBLE | WS_VSCROLL,
                           _LOG_x0, _LOG_y0, _LOG_xsize, _LOG_ysize,
                           hWndParent, NULL, hInst, NULL);
}

/*********************************************************************
*
*       Clipboard support
*/

static void _OnCopy(int LayerIndex) {
  HGLOBAL hMemClipboard;
  BITMAPINFOHEADER bmiHeader = {0};
  int XSize = _GetXSizePhysEx(LayerIndex);
  int YSize = _GetYSizePhysEx(LayerIndex);
  int BPP   = LCD_GetBitsPerPixel_L0Ex(LayerIndex);
  int x,y, i,Size;
  int BytesPerLine = ((BPP > 8) ? (2 * XSize + 2) : (XSize + 3)) & ~3;
  int NumColors = (BPP > 8) ? 0 : (1 << BPP);
  union {
    U8*               u8;
    U16*              u16;
    COLORREF*         ColorRef;
    BITMAPINFOHEADER* BitmapInfoHeader;
  } p;
  Size = sizeof(BITMAPINFOHEADER) + NumColors * 4 + YSize * BytesPerLine;
  hMemClipboard = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE , Size);  // Note that GlobalFree is called automatically by windows
  p.u8      = GlobalLock(hMemClipboard);
  bmiHeader.biClrUsed = NumColors;
  bmiHeader.biBitCount = (BPP  <= 8) ? 8 : 16;
  bmiHeader.biHeight = YSize;
  bmiHeader.biPlanes = 1;
  bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmiHeader.biWidth = XSize;
  /* Fill in bitmap info header */
  *p.BitmapInfoHeader++ = bmiHeader;
  /* Fill in color table */
  for (i = 0; i < NumColors; i++) {
    COLORREF Color;
    Color = LCDSIM_Index2Color(i, LayerIndex);
    Color = ((Color>>16)&255) | (Color & 0xff00) | ((Color & 0xff) <<16);
    *p.ColorRef++ = Color;
  }
  /* Fill in the pixels */
  for (y = YSize-1; y >= 0; y--) {
    for (x = 0; x < XSize; x++) {
      if (BPP <= 8) {
        *p.u8++ = LCDSIM_GetPixelIndex(x, y, LayerIndex);
      } else {
        int Color = LCDSIM_GetPixelColor(x, y, LayerIndex);
        int r = ((Color >> 16) * 31 + 127) / 255;
        int g = (((Color >> 8) & 255) * 31 + 127) / 255;
        int b = ((Color & 255) * 31 + 127) / 255;
        *p.u16++ = (r << 10) | (g << 5) | b;   // 16 bpp Bitmaps in windows are 555: rrrrrgggggbbbbb
      }
    }
    /* Align pointer to next U32 */
    if (BytesPerLine &3)
      p.u8 += 3- (BytesPerLine &3);
  }
  /* Copy to clipboard */
  OpenClipboard(NULL);
  EmptyClipboard();
  SetClipboardData(CF_DIB, hMemClipboard);
  CloseClipboard();
  GlobalUnlock(hMemClipboard);
}
  
/*********************************************************************
*
*       Static functions: Thread management
*
**********************************************************************
*/
/*********************************************************************
*
*       _Thread
*/
static DWORD __stdcall _Thread(void* Parameter) {
#ifdef WATCOM
  main();
#else
  MainTask();
#endif
  _ghThread =0;
  SIM_Log("\nApplication terminated.");
  return 0;
}

/*********************************************************************
*
*       _THREAD_IsRunning
*/
static char _THREAD_IsRunning(void) {
  return _ghThread ? 1 : 0;
}

/*********************************************************************
*
*       _THREAD_IsSuspended
*/
static char _THREAD_IsSuspended(void) {
  return _SuspendCount ? 1 : 0;
}

/*********************************************************************
*
*       _THREAD_KillAll
*/
static void _THREAD_KillAll(void) {
// Try to get the thread to terminate by itself
  int i = 0;
  _CmdKill = 1;
  while (_ghThread && i++ < 20)
    Sleep(10);
  if (_ghThread)
    TerminateThread(_ghThread, 0);
  _ghThread = 0;
  _CmdKill = 0;
}

/*********************************************************************
*
*       _THREAD_Sleep
*/
static void _THREAD_Sleep(int ms) {
  while (ms > 0) {
    if (_CmdKill) {
      // Temporarily increase threads priority in order to make
      // sure it is not interrupted before exit
      SetThreadPriority(_ghThread, THREAD_PRIORITY_HIGHEST);
      _ghThread = 0;
      ExitThread(0);
    }
    Sleep(10); ms -= 10;
  }
}

/*********************************************************************
*
*       _THREAD_StartApplication
*/
static void _THREAD_StartApplication(void) {
  DWORD ThreadId;
  _LOG_Clear();
  SIM_Log("Application started");
  if (_ghThread)
    _THREAD_KillAll();
  _ghThread = CreateThread(NULL, 0, _Thread, NULL, 0, &ThreadId);
  _SuspendCount=0;
}

/*********************************************************************
*
*       _THREAD_StopApplication
*/
static void _THREAD_StopApplication(void) {
  int i;
  SIM_Log("\nApplication suspended");
  if (_SuspendCount) {
    _MessageBox1("Application paused already ...");
    return;
  }
  if (!_ghThread) {
    _MessageBox1("Application is not running...");
    return;
  }
  _SuspendCount = SuspendThread(_ghThread) + 1;
  for (i = 0; i < _NumTask; i++) {
    SuspendThread(_ahThread[i]);
  }
}

/*********************************************************************
*
*       _THREAD_ContinueApplication
*/
static void _THREAD_ContinueApplication(void) {
  int i;
  SIM_Log("\nApplication continued");
  if (!_SuspendCount) {
    _MessageBox1("Application is not stopped...");
    return;
  }
  if (!_ghThread) {
    _MessageBox1("Application is not running...");
    return;
  }
  _SuspendCount = ResumeThread(_ghThread) - 1;
  for (i = 0; i < _NumTask; i++) {
    ResumeThread(_ahThread[i]);
  }
}
 
/*********************************************************************
*
*       _CreateTask
*/
static unsigned long __stdcall _CreateTask(LPVOID lpParameter) {
  ((TaskMain*)lpParameter)();
  return 0;
}

/*********************************************************************
*
*       Static functions: Logging
*
**********************************************************************
*/
/*********************************************************************
*
*       _LogTime
*/
static void _LogTime(void) {
  char ac[80];
  sprintf(ac,"\n%d:   ",SIM_GetTime());
  _LOG_Add(ac);
}

/*********************************************************************
*
*       _SendToErrorFile
*/
static void _SendToErrorFile(const char* s) {
  DWORD NumBytesWritten;
  if (_hFileError == 0) {
    _hFileError = CreateFile("SimError.log", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS,  FILE_ATTRIBUTE_NORMAL, NULL);
  }
  WriteFile(_hFileError, "\r\n", 2, &NumBytesWritten, NULL);
  WriteFile(_hFileError, s, strlen(s), &NumBytesWritten, NULL);
}

/*********************************************************************
*
*       _RegisterClasses
*/
static void _RegisterClasses(void){
  WNDCLASSEX wcex;
  HICON hIcon = LoadIcon(_hInst, (LPCTSTR)IDR_MAINFRAME);
  memset (&wcex, 0, sizeof(wcex));
  wcex.cbSize       = sizeof(WNDCLASSEX); 
  wcex.hInstance    = _hInst;
  //
  // Register main window
  //
  wcex.style        = CS_HREDRAW | CS_VREDRAW;
  wcex.lpfnWndProc  = (WNDPROC)_WndProcMain;
  wcex.hIcon        = LoadIcon(_hInst, (LPCTSTR)IDR_MAINFRAME);
  wcex.hCursor      = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground= (HBRUSH)(COLOR_APPWORKSPACE+1);
  wcex.lpszMenuName = (LPCSTR)IDC_SIMULATION;
  wcex.lpszClassName= acClassNameMain;
  RegisterClassEx(&wcex);
  //
  // Register Device window
  //
  wcex.lpfnWndProc  = (WNDPROC)_WndProcDevice;
  wcex.hbrBackground= (HBRUSH)GetStockObject(BLACK_BRUSH);
  wcex.lpszMenuName = NULL;
  wcex.lpszClassName= acClassNameDevice;
  RegisterClassEx(&wcex);
  //
  // Register LCD Info window
  //
  wcex.style        = CS_HREDRAW | CS_VREDRAW;
  wcex.lpfnWndProc  = (WNDPROC)_WndProcLCDInfo;
  wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
  wcex.lpszClassName= acClassNameLCDInfo;
  RegisterClassEx(&wcex);//
  //
  // Register Hardkey window
  //
  wcex.style        = 0;
  wcex.lpfnWndProc  = (WNDPROC)_WndProcHardkey;
  wcex.lpszClassName= acClassNameHardkey;
  RegisterClassEx(&wcex);
  //
  // Register LCD window
  //
  wcex.lpfnWndProc  = (WNDPROC)_WndProcLCD;
  wcex.lpszClassName= acClassNameLCD;
  RegisterClassEx(&wcex);//
  //
  // Register LOG window
  //
  _LOG_Init(_hInst, hIcon);
}
 
/*********************************************************************
*
*       _WndProcLCD
*/
static LRESULT CALLBACK _WndProcLCD(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  int xPos = (signed short)LOWORD(lParam) / _MagX;  // horizontal position of cursor 
  int yPos = (signed short)HIWORD(lParam) / _MagY;  // vertical position of cursor 
  unsigned int LayerIndex = GetWindowLong(hWnd, GWL_USERDATA);
  switch (message) {
  case WM_CREATE:
    SetTimer(hWnd, 0, 20, NULL);
    break;
  case WM_TIMER:
    if (LayerIndex == 0) {
      LCDSIM_CheckMouseState();
    }
    if (LayerIndex < countof(_aModifyCnt)) {
      int NewCnt = LCDSIM_GetModifyCnt(LayerIndex);
      if (_aModifyCnt[LayerIndex] != NewCnt) {
        if (InvalidateRect(hWnd, NULL, FALSE)) {
          _aModifyCnt[LayerIndex] = NewCnt;            // invalidation successfull
        }
      }
    }
    if (_ghThread == NULL)
      PostQuitMessage(0);
    break;
  case WM_PAINT:
    LCDSIM_Paint(hWnd);
    break;
  // Handle mouse events
  case WM_RBUTTONDOWN:
    { POINT Point;
      Point.x = (signed short)LOWORD(lParam);
      Point.y = (signed short)HIWORD(lParam);
      ClientToScreen(hWnd, &Point);
      TrackPopupMenu(_hMenuPopup, TPM_RIGHTBUTTON, Point.x, Point.y, 0, GetParent(hWnd), NULL);
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -