📄 twl.cpp
字号:
// TWL.CPP/* * Tiny, Terminal or Toy Windows Library * Steve Donovan, Miningtek 1998 * based on SWL, 1995.*/
//#define WIN32_LEAN_AND_MEAN#define NO_STRICT#include <windows.h>#ifdef _WIN32//# include <commctrl.h>//# define COMMCTRL# define LOW_WORD LOWORD#else# define LOW_WORD WORD# define GWL_USERDATA 0#endif#include <string.h>#include <stdlib.h>
#include <ctype.h>#include "twl.h"#define MAX_CMD_LINE 120#define N_CMD_LINE 20
// *hack 1.2.8 make the window class distinct from YAWL;
// in general, it should be unique per process.#define EW_CLASSNAME "EVNTWNDCLSS0"#define POP_WNDCLSS "WNDCLSS"static HINSTANCE hInst;static int CmdShow;static HANDLE hAccel=0,hModeless=0;char obuff[BUFSIZE];static char buff[MAX_CMD_LINE];typedef unsigned char byte;// Miscelaneous functions!!long RGBF(float r, float g, float b)//-------------------------------------{ return RGB(byte(255*r),byte(255*g),byte(255*b)); }long RGBI(int r, int g, int b){ return RGB(r,g,b); }int exec(pchar s, int mode, bool do_wait)//--------------------------------------{ // return WinExec(s,mode); PROCESS_INFORMATION pi; STARTUPINFO si; TWin *lastw = TWin::get_active_window(); ZeroMemory(&si,sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = mode; BOOL bRet = CreateProcess(NULL, s, NULL,NULL, // no security! FALSE, // inherited handles CREATE_NEW_CONSOLE, // creation flags NULL,NULL, &si, &pi); if (!bRet) return 0; if (do_wait) { WaitForSingleObject(pi.hProcess,1000); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } //process_id = pi.dwProcessId; lastw->set_focus(); delete lastw; return 1;}long current_time(){ return GetTickCount(); }bool get_key_state(int keycode){ return GetKeyState(keycode) < 0; }int message(char *format,bool was_warning)//----------------------------------------{ //char *Args; int ret; // Args = (char *) &format + sizeof format; // vsprintf(obuff,format,Args); // ret = MessageBox(NULL,obuff,"Message",MB_ICONEXCLAMATION | MB_OKCANCEL); ret = MessageBox(NULL,format,"Message",MB_ICONEXCLAMATION | MB_OKCANCEL); if (ret == IDCANCEL) return 0; else return 1; }/// TDC ///////////////TDC::TDC(){ m_hdc = m_pen = m_font = m_brush = NULL; m_twin = NULL;}TDC::~TDC(){ }void TDC::get(TWin *pw)//---------------------{ if(!pw) pw = m_twin; m_hdc = GetDC(pw->handle());}void TDC::release(TWin *pw) //--------------------------{ if(!pw) pw = m_twin; ReleaseDC(pw->handle(),m_hdc); }void TDC::kill()//--------------{ DeleteDC(m_hdc); }Handle TDC::select(Handle obj)//---------------------------{ return SelectObject(m_hdc,obj); }void TDC::select_stock(int val)//----------------------------{ select(GetStockObject(val)); }void TDC::xor_pen()//-----------------{ SetROP2(m_hdc, GetROP2(m_hdc)==R2_XORPEN ? R2_COPYPEN : R2_XORPEN); }void TDC::to_logical(Point *pt, int sz){ DPtoLP(m_hdc,(LPPOINT)pt,sz); }void TDC::to_device(Point *pt, int sz){ LPtoDP(m_hdc,(LPPOINT)pt,sz); }void TDC::set_colour(long colour){ COLORREF rgb = (COLORREF)colour; get(); SetTextColor(m_hdc,rgb); if(m_pen) DeleteObject(m_pen); m_pen = CreatePen(PS_SOLID,0,rgb); select(m_pen); release();}void TDC::set_back_colour(long colour){ COLORREF rgb = (COLORREF)colour; SetBkColor(m_hdc,rgb); if(m_brush) DeleteObject(m_brush); m_brush = CreateSolidBrush(rgb); select(m_brush);}// this changes both the _pen_ and the _text_ colourvoid TDC::set_colour(float r, float g, float b)//----------------------------{ set_colour(RGBF(r,g,b));}void TDC::set_back_colour(float r, float g, float b){ set_back_colour(RGBF(r,g,b));}void TDC::set_text_colour(long colour){ SetTextColor(m_hdc,colour); } void TDC::set_text_align(int flags, bool update_cp)//-------------------------------------------------{ get(); SetTextAlign(m_hdc, flags | (update_cp ? TA_UPDATECP : 0)); release();}void TDC::get_text_extent(pchar text, int& w, int& h, TFont *font)//----------------------------------------------------------------{ SIZE sz; HFONT oldfont; get(); if (font) oldfont = select(*font); #ifdef _WIN32 GetTextExtentPoint32(m_hdc,text,lstrlen(text),&sz); #else DWORD res = GetTextExtent(m_hdc,text,lstrlen(text)); sz.cx = LOWORD(res); sz.cy = HIWORD(res); #endif if (font) select(oldfont); release(); w = sz.cx; h = sz.cy;}// wrappers around common graphics callsvoid TDC::draw_text(pchar msg)//---------------------------------------------------{ TextOut(m_hdc,0,0,msg,lstrlen(msg)); }void TDC::text_out(int x, int y, char *buff, int sz){ TextOut(m_hdc,x,y,buff,sz);}void TDC::move_to(int x, int y)//----------------------------{ MoveToEx(m_hdc,x,y,NULL); }void TDC::line_to(int x, int y)//-----------------------------{ LineTo(m_hdc,x,y/*,NULL*/); }void TDC::rectangle(const Rect& rt)//---------------------------------{ Rectangle(m_hdc, rt.left,rt.top,rt.right,rt.bottom); }void TDC::ellipse(const Rect& rt)//---------------------------------{ Ellipse(m_hdc, rt.left,rt.top,rt.right,rt.bottom); }void TDC::invert_rect(const Rect& rt)//-----------------------------------{ InvertRect(m_hdc,(RECT *)&rt); }TClientDC::TClientDC(TEventWindow *ew){ set_hdc(ew->get_dc()->get_hdc()); set_twin(ew); get();}TClientDC::~TClientDC(){ release();}//// TGDIObjvoid TGDIObj::destroy()//---------------{ if (m_hand) DeleteObject(m_hand); m_hand = NULL; } //// TFont ////////#define PLF ((LOGFONT *)m_pfont)TFont::TFont(){ m_pfont = (void *) new LOGFONT;}TFont::~TFont(){ delete (LOGFONT *) m_pfont;}void TFont::set(pchar spec, int sz, int ftype)//-------------------------------------------{ LOGFONT&lf = *(LOGFONT *)m_pfont; // define an alias... int wt = FW_NORMAL; lf.lfHeight = sz; if (ftype & BOLD) wt = FW_BOLD; lf.lfWeight = wt; lf.lfItalic = (ftype & ITALIC) ? TRUE: FALSE; lf.lfOutPrecision = OUT_DEFAULT_PRECIS; lf.lfCharSet = ANSI_CHARSET; lf.lfQuality = PROOF_QUALITY; lf.lfEscapement = 0; lf.lfOrientation = 0; lf.lfUnderline = 0; lf.lfStrikeOut = 0; lstrcpy(lf.lfFaceName,spec); create();}/// copy constructorTFont& TFont::operator = (const TFont& f)//--------------------------------------{ memcpy(m_pfont,f.m_pfont,sizeof(LOGFONT)); create(); return *this;}void TFont::create()//------------------{ destroy(); m_hand = CreateFontIndirect((LOGFONT *)m_pfont);}void TFont::set_name(pchar name)//-------------------------------{ lstrcpy(PLF->lfFaceName,name); create(); }void TFont::set_size(int pts)//----------------------------{ PLF->lfHeight = pts; create(); }void TFont::set_bold()//-----------------------{ PLF->lfWeight = FW_BOLD; create(); }void TFont::set_italic()//-----------------------{ PLF->lfItalic = TRUE; create(); }////// TWin ///////////static void check_error(){ LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL );// Process any inserts in lpMsgBuf.// ...(?)// Display the string.MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );// Free the buffer.LocalFree( lpMsgBuf );}TWin::TWin(TWin *parent, pchar winclss, pchar text, int id, dword styleEx)//------------------------------------------------------------------------{ DWORD style = WS_CHILD | WS_VISIBLE | styleEx; HWND hwndChild = CreateWindowEx(WS_EX_LEFT,winclss,text,style, 0,0,CW_USEDEFAULT,CW_USEDEFAULT, parent->m_hwnd, (HMENU)id, hInst,NULL); set(hwndChild); check_error();} void TWin::update()//-----------------{ UpdateWindow(m_hwnd);}static inline bool as_bool(int val){ return val ? true : false; }static inline bool as_bool(void *ptr){ return ptr ? true : false; }bool TWin::is_visible(){ return as_bool(IsWindowVisible(m_hwnd));} void TWin::invalidate(Rect *lprt){ InvalidateRect(m_hwnd,(LPRECT)lprt,TRUE); }void TWin::get_rect(Rect &rt)//---------------------------{ GetWindowRect(m_hwnd,(LPRECT)&rt);}void TWin::get_client_rect(Rect &rt){ GetClientRect(m_hwnd,(LPRECT)&rt);}Point TWin::to_screen(Point pt){ ClientToScreen(m_hwnd,(LPPOINT)&pt); return pt; }Point TWin::to_client(Point pt){ ScreenToClient(m_hwnd,(LPPOINT)&pt); return pt; }int TWin::width()//---------------{ Rect rt; get_rect(rt); return rt.right - rt.left;}int TWin::height()//----------------{ Rect rt; get_rect(rt); return rt.bottom - rt.top;}void TWin::set_text(pchar str)//---------------------------{ SetWindowText(m_hwnd, str); }pchar TWin::get_text(pchar str, int sz)//-------------------------------------{ GetWindowText(m_hwnd,str,sz); return str; }// These guys work with the specified _child_ of the windowvoid TWin::set_text(int id, pchar str)//-------------------------------------{ SetDlgItemText(m_hwnd,id,str); }void TWin::set_int(int id, int val)//-------------------------------------{ SetDlgItemInt(m_hwnd,id,val,TRUE); }pchar TWin::get_text(int id, pchar str, int sz)//--------------------------------------------{ GetDlgItemText(m_hwnd,id,str,sz); return str; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -