welcome.c

来自「一个类似windows」· C语言 代码 · 共 826 行 · 第 1/2 页

C
826
字号
/*
 *  ReactOS applications
 *  Copyright (C) 2001, 2002, 2003 ReactOS Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/* $Id: welcome.c 21325 2006-03-17 22:07:04Z fireball $
 *
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS welcome/autorun application
 * FILE:        subsys/system/welcome/welcome.c
 * PROGRAMMERS: Eric Kohl (ekohl@rz-online.de)
 *              Casper S. Hornstrup (chorns@users.sourceforge.net)
 *
 * NOTE:
 *   This utility can be customized by modifying the resources.
 *   Please do NOT change the source code in order to customize this
 *   utility but change the resources!
 */

#include "../../../include/reactos/version.h"
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <tchar.h>

#include "resource.h"



#define LIGHT_BLUE 0x00F7EFD6
#define DARK_BLUE  0x008C7B6B

#define TITLE_WIDTH  480
#define TITLE_HEIGHT  93


/* GLOBALS ******************************************************************/

TCHAR szFrameClass [] = TEXT("WelcomeWindowClass");
TCHAR szAppTitle [80];

HINSTANCE hInstance;

HWND hwndMain = 0;
HWND hwndDefaultTopic = 0;

HDC hdcMem = 0;

int nTopic = -1;
int nDefaultTopic = -1;

ULONG ulInnerWidth = TITLE_WIDTH;
ULONG ulInnerHeight = (TITLE_WIDTH * 3) / 4;
ULONG ulTitleHeight = TITLE_HEIGHT + 3;

HBITMAP hTitleBitmap = 0;
HBITMAP hDefaultTopicBitmap = 0;
HBITMAP hTopicBitmap[10];
HWND hwndTopicButton[10];
HWND hwndCloseButton;
HWND hwndCheckButton;

HFONT hfontTopicButton;
HFONT hfontTopicTitle;
HFONT hfontTopicDescription;
HFONT hfontCheckButton;

HBRUSH hbrLightBlue;
HBRUSH hbrDarkBlue;
HBRUSH hbrRightPanel;

RECT rcTitlePanel;
RECT rcLeftPanel;
RECT rcRightPanel;

WNDPROC fnOldBtn;


INT_PTR CALLBACK
MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


/* FUNCTIONS ****************************************************************/

int WINAPI
WinMain(HINSTANCE hInst,
	HINSTANCE hPrevInstance,
	LPSTR lpszCmdLine,
	int nCmdShow)
{
  WNDCLASSEX wndclass;
  MSG msg;
  int xPos;
  int yPos;
  int xWidth;
  int yHeight;
  RECT rcWindow;
  HICON hMainIcon;
  HMENU hSystemMenu;
  DWORD dwStyle = WS_OVERLAPPED | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
                  WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
  BITMAP BitmapInfo;

  hInstance = hInst;

  /* Load icons */
  hMainIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_MAIN));

  /* Register the window class */
  wndclass.style = CS_HREDRAW | CS_VREDRAW;
  wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hInstance = hInstance;
  wndclass.hIcon = hMainIcon;
  wndclass.hCursor = LoadCursor (NULL, MAKEINTRESOURCE(IDC_ARROW));
  wndclass.hbrBackground = 0;
  wndclass.lpszMenuName = NULL;
  wndclass.lpszClassName = szFrameClass;

  wndclass.cbSize = sizeof(WNDCLASSEX);
  wndclass.hIconSm = 0;

  RegisterClassEx(&wndclass);

  hTitleBitmap = LoadBitmap (hInstance, MAKEINTRESOURCE(IDB_TITLEBITMAP));
  if (hTitleBitmap != NULL)
    {
      GetObject(hTitleBitmap, sizeof(BITMAP), &BitmapInfo);
      ulInnerWidth = BitmapInfo.bmWidth;
      ulInnerHeight = (ulInnerWidth * 3) / 4;
      ulTitleHeight = BitmapInfo.bmHeight + 3;
      DeleteObject(hTitleBitmap);
    }
  ulInnerHeight -= GetSystemMetrics(SM_CYCAPTION);

  rcWindow.top = 0;
  rcWindow.bottom = ulInnerHeight - 1;
  rcWindow.left = 0;
  rcWindow.right = ulInnerWidth - 1;

  AdjustWindowRect(&rcWindow,
		    dwStyle,
		    FALSE);
  xWidth = rcWindow.right - rcWindow.left;
  yHeight = rcWindow.bottom - rcWindow.top;

  xPos = (GetSystemMetrics(SM_CXSCREEN) - xWidth) / 2;
  yPos = (GetSystemMetrics(SM_CYSCREEN) - yHeight) / 2;

  rcTitlePanel.top = 0;
  rcTitlePanel.bottom = ulTitleHeight;
  rcTitlePanel.left = 0;
  rcTitlePanel.right = ulInnerWidth - 1;

  rcLeftPanel.top = rcTitlePanel.bottom;
  rcLeftPanel.bottom = ulInnerHeight - 1;
  rcLeftPanel.left = 0;
  rcLeftPanel.right = ulInnerWidth / 3;

  rcRightPanel.top = rcLeftPanel.top;
  rcRightPanel.bottom = rcLeftPanel.bottom;
  rcRightPanel.left = rcLeftPanel.right;
  rcRightPanel.right = ulInnerWidth - 1;

  if (!LoadString(hInstance, (UINT)MAKEINTRESOURCE(IDS_APPTITLE), szAppTitle, 80))
    _tcscpy(szAppTitle, TEXT("ReactOS Welcome"));

  /* Create main window */
  hwndMain = CreateWindow(szFrameClass,
			  szAppTitle,
			  dwStyle,
			  xPos,
			  yPos,
			  xWidth,
			  yHeight,
			  0,
			  0,
			  hInstance,
			  NULL);

  hSystemMenu = GetSystemMenu(hwndMain, FALSE);
  if(hSystemMenu)
  {
    RemoveMenu(hSystemMenu, SC_SIZE, MF_BYCOMMAND);
    RemoveMenu(hSystemMenu, SC_MAXIMIZE, MF_BYCOMMAND);
  }

  ShowWindow(hwndMain, nCmdShow);
  UpdateWindow(hwndMain);

  while (GetMessage(&msg, NULL, 0, 0) != FALSE)
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }

  return(msg.wParam);
}


INT_PTR CALLBACK
ButtonSubclassWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  LONG i;

  if (uMsg == WM_MOUSEMOVE)
    {
      i = GetWindowLong(hWnd, GWL_ID);
      if (nTopic != i)
	{
	  nTopic = i;
	  SetFocus(hWnd);
	  InvalidateRect(hwndMain, &rcRightPanel, TRUE);
	}
    }

  return(CallWindowProc(fnOldBtn, hWnd, uMsg, wParam, lParam));
}


static BOOL
RunApplication(int nTopic)
{
  PROCESS_INFORMATION ProcessInfo;
  STARTUPINFO StartupInfo;
  TCHAR AppName[256];
  TCHAR CurrentDir[256];
  int nLength;

  InvalidateRect(hwndMain, NULL, TRUE);

  GetCurrentDirectory(256, CurrentDir);

  nLength = LoadString(hInstance, IDS_TOPICACTION0 + nTopic, AppName, 256);
  if (nLength == 0)
    return TRUE;

  if (!_tcsicmp(AppName, TEXT("<exit>")))
    return FALSE;

  if (_tcsicmp(AppName, TEXT("explorer.exe")) == 0)
    {
      _tcscat(AppName, TEXT(" "));
      _tcscat(AppName, CurrentDir);
    }

  memset(&StartupInfo, 0, sizeof(STARTUPINFO));
  StartupInfo.cb = sizeof(STARTUPINFO);
  StartupInfo.lpTitle = TEXT("Test");
  StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow = SW_SHOWNORMAL;

  CreateProcess(NULL, AppName, NULL, NULL, FALSE, CREATE_NEW_CONSOLE,NULL,
		CurrentDir,
		&StartupInfo,
		&ProcessInfo);

  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);

  return TRUE;
}


static VOID
SubclassButton(HWND hWnd)
{
  fnOldBtn = (WNDPROC)SetWindowLongPtr(hWnd, GWL_WNDPROC, (DWORD_PTR)ButtonSubclassWndProc);
}


static DWORD
GetButtonHeight(HDC hDC,
		HFONT hFont,
		LPCTSTR szText,
		DWORD dwWidth)
{
  HFONT hOldFont;
  RECT rect;

  rect.left = 0;
  rect.right = dwWidth - 20;
  rect.top = 0;
  rect.bottom = 25;

  hOldFont = SelectObject(hDC, hFont);
  DrawText(hDC, szText, -1, &rect, DT_TOP | DT_CALCRECT | DT_WORDBREAK);
  SelectObject(hDC, hOldFont);

  return(rect.bottom-rect.top + 14);
}


static LRESULT
OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
  TCHAR szText[80];
  int i,nLength;
  HDC ScreenDC;
  DWORD dwTop;
  DWORD dwHeight = 0;

  hbrLightBlue = CreateSolidBrush(LIGHT_BLUE);
  hbrDarkBlue = CreateSolidBrush(DARK_BLUE);
  hbrRightPanel = CreateSolidBrush(0x00FFFFFF);

  /* Topic title font */
  hfontTopicTitle = CreateFont(-18,0,0,0,FW_NORMAL,
			       FALSE,FALSE,FALSE,ANSI_CHARSET,
			       OUT_DEFAULT_PRECIS,
			       CLIP_DEFAULT_PRECIS,
			       DEFAULT_QUALITY,
			       FF_DONTCARE,
			       TEXT("Arial"));

  /* Topic description font */
  hfontTopicDescription = CreateFont(-11,0,0,0,FW_THIN,
				     FALSE,FALSE,FALSE,ANSI_CHARSET,
				     OUT_DEFAULT_PRECIS,
				     CLIP_DEFAULT_PRECIS,
				     DEFAULT_QUALITY,
				     FF_DONTCARE,
				     TEXT("Arial"));

  /* Topic button font */
  hfontTopicButton = CreateFont(-11,0,0,0,FW_BOLD,
				FALSE,FALSE,FALSE,ANSI_CHARSET,
				OUT_DEFAULT_PRECIS,
				CLIP_DEFAULT_PRECIS,
				DEFAULT_QUALITY,
				FF_DONTCARE,
				TEXT("Arial"));

  /* Load title bitmap */
  if (hTitleBitmap != 0)
    hTitleBitmap = LoadBitmap (hInstance, MAKEINTRESOURCE(IDB_TITLEBITMAP));

  /* Load topic bitmaps */
  hDefaultTopicBitmap = LoadBitmap (hInstance, MAKEINTRESOURCE(IDB_DEFAULTTOPICBITMAP));
  for (i=0;i < 10; i++)
    {
      hTopicBitmap[i] = LoadBitmap (hInstance, MAKEINTRESOURCE(IDB_TOPICBITMAP0+i));
    }

  ScreenDC = GetWindowDC(hWnd);
  hdcMem = CreateCompatibleDC (ScreenDC);
  ReleaseDC(hWnd, ScreenDC);

  /* load and create buttons */
  dwTop = rcLeftPanel.top;
  for (i = 0; i < 10; i++)
    {
      nLength = LoadString(hInstance, IDS_TOPICBUTTON0 + i, szText, 80);
      if (nLength > 0)
	{
	  dwHeight = GetButtonHeight(hdcMem,
				     hfontTopicButton,
				     szText,
				     rcLeftPanel.right - rcLeftPanel.left);

	  hwndTopicButton[i] = CreateWindow(TEXT("BUTTON"),
					    szText,
					    WS_CHILDWINDOW | WS_VISIBLE | WS_TABSTOP | BS_MULTILINE | BS_OWNERDRAW,
					    rcLeftPanel.left,
					    dwTop,
					    rcLeftPanel.right - rcLeftPanel.left,
					    dwHeight,
					    hWnd,
					    (HMENU)i,
					    hInstance,
					    NULL);
	  hwndDefaultTopic = hwndTopicButton[i];
	  nDefaultTopic = i;
	  SubclassButton(hwndTopicButton[i]);
	  SendMessage(hwndTopicButton[i], WM_SETFONT, (WPARAM)hfontTopicButton, MAKELPARAM(TRUE,0));
	}
      else
	{
	  hwndTopicButton[i] = 0;
	}

      dwTop += dwHeight;
    }

  /* Create exit button */
  nLength = LoadString(hInstance, IDS_CLOSETEXT, szText, 80);
  if (nLength > 0)
    {
      hwndCloseButton = CreateWindow(TEXT("BUTTON"),
				     szText,
				     WS_VISIBLE | WS_CHILD | BS_FLAT,
				     rcRightPanel.right - 10 - 57,
				     rcRightPanel.bottom - 10 - 21,
				     57,
				     21,
				     hWnd,
				     (HMENU)IDC_CLOSEBUTTON,
				     hInstance,
				     NULL);

⌨️ 快捷键说明

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