📄 core_system.cpp
字号:
/**************************************************
Windows主程序模块(借鉴adam的程序)
Author: LittleFish QQ:93663886
E-Mail: kittyfish_1981@yahoo.com.cn
Blog: http://blog.csdn.net/kittyfish
真诚的期待你提出改良方法或程序BUG
**************************************************/
#include "Core_System.h"
cApplication* g_pApplication = NULL;
BOOL cApplication::Init()
{
return TRUE;
}
BOOL cApplication::Shutdown()
{
return TRUE;
}
cApplication::cApplication()
{
// Save instance handle
g_pApplication = this;
//m_pDirectDraw = NULL;
m_bUseThread = FALSE;
// Get the instance handle
m_hInst = GetModuleHandle(NULL);
// Set a default window class and caption
_tcscpy(m_Class, _T("AppClass"));
_tcscpy(m_Caption, _T("Application Caption"));
// Set default window style, position, width, height
m_Style = WS_OVERLAPPEDWINDOW;
//m_Style &= ~WS_CAPTION;
m_XPos = -1;
m_YPos = -1;
m_Width = 200;
m_Height = 200;
m_rcWnd.left = m_rcWnd.top = 0;
m_rcWnd.right = m_rcWnd.bottom = 200;
// Set default WNDCLASSEX structure
m_wcex.cbSize = sizeof(WNDCLASSEX);
m_wcex.style = CS_HREDRAW | CS_VREDRAW;;
m_wcex.lpfnWndProc = AppWindowProc;
m_wcex.cbClsExtra = 0;
m_wcex.cbWndExtra = 0;
m_wcex.hInstance = m_hInst;
m_wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
m_wcex.hbrBackground = NULL;
m_wcex.lpszMenuName = NULL;
m_wcex.lpszClassName = m_Class;
m_wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
}
HWND cApplication::GethWnd()
{
return m_hWnd;
}
HINSTANCE cApplication::GethInst()
{
return m_hInst;
}
BOOL cApplication::Create()
{
if(!RegisterClassEx(&m_wcex))
return FALSE;
// Create the Main Window
m_hWnd = CreateWindow(m_Class, m_Caption,
m_Style,
m_XPos, m_YPos,
m_Width, m_Height,
NULL, NULL, m_hInst, NULL);
if(!m_hWnd)
return FALSE;
return TRUE;
}
BOOL cApplication::Run()
{
MSG Msg;
// Register window class
if(!RegisterClassEx(&m_wcex))
return FALSE;
// Create the Main Window
m_hWnd = CreateWindow(m_Class, m_Caption,
m_Style,
m_XPos, m_YPos,
m_Width, m_Height,
NULL, NULL, m_hInst, NULL);
if(!m_hWnd)
return FALSE;
// Show and update the window
ShowWindow(m_hWnd, SW_NORMAL);
UpdateWindow(m_hWnd);
// Make sure client area is correct size
Resize(m_Width, m_Height);
// Initialize COM
CoInitialize(NULL);
if(Init() == TRUE)
{
//这种适合线程
if( m_bUseThread )
{
BOOL bRet;
while( (bRet = GetMessage( &Msg, NULL, 0, 0 )) != 0 )
{
if( bRet == -1 )
break;
if ( Msg.message == WM_QUIT )
{
break;
}
else
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
//Frame();//Frame in a thread, not here
}
}
else
{
//这种适合非线程
//// Enter the message pump
ZeroMemory(&Msg, sizeof(MSG));
while(Msg.message != WM_QUIT)
{
// Handle Windows messages (if any)
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
else
{
// Do per-frame processing, break on FALSE return value
if(Frame() == FALSE)
break;
}
}
}
}
Shutdown();
// Shutdown COM
CoUninitialize();
// Unregister the window class
UnregisterClass(m_Class, m_hInst);
return TRUE;
}
BOOL cApplication::Error(BOOL Fatal, TCHAR *Text, ...)
{
TCHAR CaptionText[12];
TCHAR ErrorText[2048];
va_list valist;
// Build the message box caption based on fatal flag
if(Fatal == FALSE)
_tcscpy(CaptionText, _T("Error"));
else
_tcscpy(CaptionText, _T("Fatal Error"));
// Build variable text buffer
va_start(valist, Text);
_vstprintf(ErrorText, Text, valist);
//_vstprintf()
va_end(valist);
// Display the message box
MessageBox(NULL, ErrorText, CaptionText, MB_OK | MB_ICONEXCLAMATION);
// Post a quit message if error was fatal
if(Fatal == TRUE)
PostQuitMessage(0);
return TRUE;
}
BOOL cApplication::Move(long XPos, long YPos)
{
RECT ClientRect;
GetClientRect(m_hWnd, &ClientRect);
MoveWindow(m_hWnd, XPos, YPos, ClientRect.right, ClientRect.bottom, TRUE);
return TRUE;
}
BOOL cApplication::Resize(long Width, long Height)
{
RECT WndRect, ClientRect;
long WndWidth, WndHeight;
GetWindowRect(m_hWnd, &WndRect);
GetClientRect(m_hWnd, &ClientRect);
WndWidth = (WndRect.right - (ClientRect.right - Width)) - WndRect.left;
WndHeight = (WndRect.bottom - (ClientRect.bottom - Height)) - WndRect.top;
MoveWindow(m_hWnd, WndRect.left, WndRect.top, WndWidth, WndHeight, TRUE);
return TRUE;
}
BOOL cApplication::SetCaption( const TCHAR* chCaption )
{
if( !chCaption )
return FALSE;
_tcscpy( m_Caption, chCaption );
return TRUE;
}
void cApplication::SetPos( long xPos, long yPos )
{
m_XPos = xPos;
m_YPos = yPos;
}
void cApplication::SetSize( long width, long height )
{
m_Width = width;
m_Height = height;
if( m_XPos == -1 && m_YPos == -1 )
{
m_XPos = ( GetSystemMetrics(SM_CXSCREEN) - m_Width )>>1;
m_YPos = ( GetSystemMetrics(SM_CYSCREEN) - m_Height )>>1;
}
}
BOOL cApplication::SetFullScreen()
{
LONG style=::GetWindowLong( m_hWnd, GWL_STYLE );
style &= ~WS_CAPTION;
SetWindowLong( m_hWnd, GWL_STYLE, style );
MoveWindow( m_hWnd,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),TRUE);
return TRUE;
}
BOOL cApplication::ExitFullScreen()
{
LONG style=::GetWindowLong( m_hWnd,GWL_STYLE );
style |= WS_CAPTION;
SetWindowLong( m_hWnd, GWL_STYLE, style );
MoveWindow( m_hWnd,0,0,m_Width, m_Height, TRUE );
return TRUE;
}
BOOL cApplication::ShowMouse(BOOL Show)
{
ShowCursor(Show);
return TRUE;
}
LRESULT CALLBACK cApplication::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch( uMsg )
{
case WM_MOVE:
GetClientRect(m_hWnd,&m_rcWnd);
ClientToScreen(m_hWnd,(LPPOINT)&m_rcWnd.left);
ClientToScreen(m_hWnd,(LPPOINT)&m_rcWnd.right);
return 1;
case WM_CHAR:
OnChar();
break;
default: return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
// The message procedure - empty except for destroy message
LRESULT CALLBACK AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
SendMessage( g_pApplication->GethWnd(), WM_QUIT, 0, 0 );
return 0;
default: return g_pApplication->MsgProc(hWnd, uMsg, wParam, lParam);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -