📄 autorun.c
字号:
#define APPNAME "AutoRun"
// Windows Header Files:
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
// Local Header Files
#include "AUTORUN.h"
/////////////////////////////////////////////////////////////////////////
// Global Variables:
HINSTANCE hInst; // current instance
char szAppName[] = APPNAME; // The name of this application
char szTitle[] = APPNAME": Sample"; // The title bar text
char szHelpFileName[] = APPNAME".HLP";
/////////////////////////////////////////////////////////////////////////
// Foward declarations of functions included in this code module:
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CenterWindow (HWND, HWND);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
// The following functions were added to Generic for this AutoRun sample app:
int OnCreate (HWND hWnd);
BOOL LaunchApplication(void);
/************************************************************************\
* FUNCTION: WinMain
\************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HANDLE hAccelTable;
if (!hPrevInstance) { // Will always be TRUE on Windows 95
// Perform instance initialization:
if (!InitApplication(hInstance)) {
return (FALSE);
}
}
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow)) {
return (FALSE);
}
hAccelTable = LoadAccelerators (hInstance, szAppName);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (msg.wParam);
lpCmdLine; // This will prevent 'unused formal parameter' warnings
}
/************************************************************************\
* FUNCTION: InitApplication
\************************************************************************/
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wc;
HWND hwnd;
// Win32 will always set hPrevInstance to NULL, so lets check
// things a little closer. This is because we only want a single
// version of this app to run at a time
hwnd = FindWindow (szAppName, NULL);
if (hwnd) {
// We found another version of ourself. Lets defer to it:
if (IsIconic(hwnd)) {
ShowWindow(hwnd, SW_RESTORE);
}
SetForegroundWindow (GetLastActivePopup (hwnd));
// If this app actually had any functionality, we would
// also want to communicate any action that our 'twin'
// should now perform based on how the user tried to
// execute us.
return FALSE;
}
// Fill in window class structure with parameters that describe
// the main window.
wc.lpszClassName = szAppName;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.lpszMenuName = NULL;
wc.hIcon = LoadIcon (hInstance, szAppName);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
return RegisterClass(&wc);
}
/************************************************************************\
* FUNCTION: InitInstance
\************************************************************************/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
// Lets replace the default window title, with the name of the
// actual application we want to launch...
LoadString (hInst, ID_APPNAME, szTitle, sizeof(szTitle));
hWnd = CreateWindow(szAppName, szTitle,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
0, 0,
0, 0,
NULL, NULL, hInstance, NULL);
if (!hWnd) {
return (FALSE);
}
// Set the small icon
#if !defined(WM_SETICON)
#define WM_SETICON 0x0080
#endif
SendMessage(hWnd, WM_SETICON, FALSE, (LPARAM)LoadIcon(hInst, "SMALL"));
ShowWindow(hWnd, nCmdShow);
return UpdateWindow(hWnd);
}
/************************************************************************\
* FUNCTION: WndProc
\************************************************************************/
HPALETTE hpal;
HBITMAP hbmAutoRun;
BITMAP bmAutoRun;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
UINT uMappedColors; // Used in WM_QUERYNEWPALETTE
RECT rect; // Used in WM_PAINT
HDC hdcSrc; // Used in WM_PAINT
HBITMAP hbmOld; // Used in WM_PAINT
switch (message) {
case WM_CREATE:
return OnCreate (hWnd);
case WM_PALETTECHANGED:
if ((HWND)wParam == hWnd) {
break;
}
// fall through to WM_QUERYNEWPALETTE
case WM_QUERYNEWPALETTE:
hdc = GetDC(hWnd);
if (hpal) {
SelectPalette(hdc, hpal, FALSE);
}
uMappedColors = RealizePalette(hdc);
ReleaseDC(hWnd,hdc);
if (uMappedColors>0) {
InvalidateRect(hWnd,NULL,TRUE);
return TRUE;
} else {
return FALSE;
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam); // Remember, these are...
wmEvent = HIWORD(wParam); // ...different for Win32!
switch (wmId) {
case IDM_CONTINUE:
if (!LaunchApplication()) {
// Failed to launch your application, you need
// to decide what to do in this case and add code
// to do that here. You can use GetLastError() to
// determine the cause of this failure.
} else {
PostMessage (hWnd, WM_CLOSE, 0, 0);
}
break;
case IDM_EXIT:
DestroyWindow (hWnd);
break;
case IDM_ABOUT:
DialogBox(hInst, "AboutBox", hWnd, (DLGPROC)About);
break;
case IDM_HELPCONTENTS:
WinHelp (hWnd, szHelpFileName, HELP_CONTENTS,(DWORD)0);
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
break;
case WM_PAINT:
hdc = BeginPaint (hWnd, &ps);
SelectPalette(hdc, hpal, FALSE);
RealizePalette(hdc);
GetClientRect (hWnd, &rect);
hdcSrc = CreateCompatibleDC (hdc);
hbmOld = SelectObject (hdcSrc, hbmAutoRun);
SelectPalette(hdcSrc, hpal, FALSE);
RealizePalette(hdcSrc);
if (!BitBlt (hdc, 0, 0, bmAutoRun.bmWidth, bmAutoRun.bmHeight, hdcSrc, 0, 0, SRCCOPY)) {
MessageBeep(0);
}
SelectObject (hdcSrc, hbmOld);
DeleteDC (hdcSrc);
EndPaint (hWnd, &ps);
break;
case WM_DESTROY:
// Tell WinHelp we don't need it any more...
WinHelp (hWnd, APPNAME".HLP", HELP_QUIT,(DWORD)0);
DeleteObject (hpal);
DeleteObject (hbmAutoRun);
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
/************************************************************************\
* FUNCTION: CenterWindow
\************************************************************************/
// This is a 'utility' function I find usefull. It will center one
// window over another. It also makes sure that the placement is within
// the 'working area', meaning that it is both within the display limits
// of the screen, -and- not obscured by the tray or other frameing
// elements of the desktop.
BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
{
RECT rChild, rParent, rWorkArea = {0,0,0,0};
int wChild, hChild, wParent, hParent;
int wScreen, hScreen, xScreen, yScreen, xNew, yNew;
BOOL bResult;
// Get the Height and Width of the child window
GetWindowRect (hwndChild, &rChild);
wChild = rChild.right - rChild.left;
hChild = rChild.bottom - rChild.top;
// Get the Height and Width of the parent window
GetWindowRect (hwndParent, &rParent);
wParent = rParent.right - rParent.left;
hParent = rParent.bottom - rParent.top;
// Get the limits of the 'workarea'
#if !defined(SPI_GETWORKAREA)
#define SPI_GETWORKAREA 48
#endif
bResult = SystemParametersInfo(
SPI_GETWORKAREA, // system parameter to query or set
sizeof(RECT), // depends on action to be taken
&rWorkArea, // depends on action to be taken
0);
wScreen = rWorkArea.right - rWorkArea.left;
hScreen = rWorkArea.bottom - rWorkArea.top;
xScreen = rWorkArea.left;
yScreen = rWorkArea.top;
// On Windows NT, the above metrics aren't valid (yet), so they all return
// '0'. Lets deal with that situation properly:
if (wScreen==0 && hScreen==0) {
wScreen = GetSystemMetrics(SM_CXSCREEN);
hScreen = GetSystemMetrics(SM_CYSCREEN);
xScreen = 0; // These values should already be '0', but just in case
yScreen = 0;
}
// Calculate new X position, then adjust for screen
xNew = rParent.left + ((wParent - wChild) /2);
if (xNew < xScreen) {
xNew = xScreen;
} else if ((xNew+wChild) > wScreen) {
xNew = (xScreen + wScreen) - wChild;
}
// Calculate new Y position, then adjust for screen
yNew = rParent.top + ((hParent - hChild) /2);
if (yNew < yScreen) {
yNew = yScreen;
} else if ((yNew+hChild) > hScreen) {
yNew = (yScreen + hScreen) - hChild;
}
// Set it, and return
return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
/************************************************************************\
* FUNCTION: About
\************************************************************************/
// To allow us better flexibility over the contents of the 'About' box,
// lets go the extra mile and pull out values from the 'Version' resource.
// This means that by properly keeping that information up-to-date, it
// will automatically keep your about box information current. Remember,
// that the 'version' strings can also be seen from the 'Properties'
// page of FileManager on Windows NT, or the Explorer on Windows 95.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -