rufus/usbdos.c

150 lines
3.6 KiB
C
Raw Normal View History

/*
* USBDOS: USB DOS bootable stick creation utility
* Copyright (c) 2011 Pete Batard <pete@akeo.ie>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <commctrl.h>
#include "msapi_utf8.h"
#include "resource.h"
#include "usbdos.h"
/*
* Globals
*/
HINSTANCE main_instance;
/*
* Main dialog callback
*/
static INT_PTR CALLBACK main_callback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_DEVICECHANGE:
return (INT_PTR)TRUE;
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
switch(LOWORD(wParam)) {
case IDC_CLOSE:
PostQuitMessage(0);
break;
default:
return (INT_PTR)FALSE;
}
return (INT_PTR)TRUE;
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return (INT_PTR)FALSE;
}
/*
* Center a dialog with regards to the main application Window or the desktop
*/
void CenterDialog(HWND hDialog)
{
POINT Point;
HWND hParent;
RECT DialogRect;
RECT ParentRect;
int nWidth;
int nHeight;
// Get the size of the dialog box.
GetWindowRect(hDialog, &DialogRect);
// Get the parent
hParent = GetParent(hDialog);
if (hParent == NULL) {
hParent = GetDesktopWindow();
}
GetClientRect(hParent, &ParentRect);
// Calculate the height and width of the current dialog
nWidth = DialogRect.right - DialogRect.left;
nHeight = DialogRect.bottom - DialogRect.top;
// Find the center point and convert to screen coordinates.
Point.x = (ParentRect.right - ParentRect.left) / 2;
Point.y = (ParentRect.bottom - ParentRect.top) / 2;
ClientToScreen(hParent, &Point);
// Calculate the new x, y starting point.
Point.x -= nWidth / 2;
Point.y -= nHeight / 2 + 35;
// Move the window.
MoveWindow(hDialog, Point.x, Point.y, nWidth, nHeight, FALSE);
}
/*
* Application Entrypoint
*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HANDLE mutex = NULL;
HWND hDlg = NULL;
MSG msg;
// Prevent 2 applications from running at the same time
mutex = CreateMutexA(NULL, TRUE, "Global/USBDOS");
if ((mutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS))
{
MessageBoxA(NULL, "Another USBDOS application is running.\n"
"Please close the first application before running another one.",
"Other instance detected", MB_ICONSTOP);
return 0;
}
// Save instance of the application for further reference
main_instance = hInstance;
// Initialize COM for folder selection
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
// Create the main Window
if ( (hDlg = CreateDialogA(hInstance, MAKEINTRESOURCEA(IDD_DIALOG), NULL, main_callback)) == NULL ) {
MessageBoxA(NULL, "Could not create Window", "DialogBox failure", MB_ICONSTOP);
goto out;
}
CenterDialog(hDlg);
ShowWindow(hDlg, SW_SHOWNORMAL);
UpdateWindow(hDlg);
// Do our own event processing
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
out:
CloseHandle(mutex);
return 0;
}