📄 mywindows.c
字号:
/*** $Id: mywindows.c,v 1.9 2003/11/22 05:35:17 weiym Exp $**** mywindows.c: implementation file of libmywins library.**** Copyright (c) 2000, 2001, 2002, WEI Yongming**** Current maintainer: WEI Yongming.**** Create date: 2000.xx.xx*//*** 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//*** TODO:*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>#include <errno.h>#include "common.h"#include "minigui.h"#include "gdi.h"#include "window.h"#include "control.h"#include "mywindows.h"void errorWindow(HWND hwnd, char* str, char* title){ char* a = NULL; if (!strstr (str, "%s")) { a = malloc (strlen (str) + 5); strcpy(a, str); strcat(a, ": %s"); str = a; } myMessageBox (hwnd, MB_OK | MB_ICONSTOP, title?title:"Error", str, strerror (errno)); free (a);}int myMessageBox (HWND hwnd, DWORD dwStyle, char* title, char* text, ...){ char * buf = NULL; int rc; if (strchr (text, '%')) { va_list args; int size = 0; int i = 0; va_start(args, text); do { size += 1000; if (buf) free(buf); buf = malloc(size); i = vsnprintf(buf, size, text, args); } while (i == size); va_end(args); } rc = MessageBox (hwnd, buf ? buf : text, title, dwStyle); if (buf) free (buf); return rc;}static DLGTEMPLATE DlgButtons ={ WS_BORDER | WS_CAPTION, WS_EX_NONE, (640 - 418)/2, (480 - 170)/2, 418, 170, NULL, 0, 0, 5, NULL, 0};#define IDC_BASE 100#define IDC_ONE 101#define IDC_TWO 102#define IDC_THREE 103static CTRLDATA CtrlButtons [] ={ { "static", WS_VISIBLE | SS_ICON, 10, 10, 40, 40, IDC_STATIC, "LOGO", 0 }, { "static", WS_VISIBLE | SS_LEFT, 50, 10, 340, 90, IDC_STATIC, NULL, 0 }, { "button", WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_GROUP, 100, 110, 80, 24, IDC_ONE, NULL, 0 }, { "button", WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP, 200, 110, 80, 24, IDC_TWO, NULL, 0 }, { "button", WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP, 300, 110, 80, 24, IDC_THREE, NULL, 0 }};static int ButtonsBoxProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam){ switch (message) { case MSG_INITDIALOG: return 1; break; case MSG_COMMAND: if (wParam == IDC_ONE || wParam == IDC_TWO || wParam == IDC_THREE) EndDialog (hDlg, wParam); break; } return DefaultDialogProc (hDlg, message, wParam, lParam);}int myWinMessage (HWND hwnd, char* title, char* button1, char* text, ...){ char* buf = NULL; int rc; if (strchr (text, '%')) { va_list args; int size = 0; int i = 0; va_start(args, text); do { size += 1000; if (buf) free(buf); buf = malloc(size); i = vsnprintf(buf, size, text, args); } while (i == size); va_end(args); } DlgButtons.caption = title; DlgButtons.controls = CtrlButtons; DlgButtons.controlnr = 3; CtrlButtons [1].caption = buf ? buf : text; CtrlButtons [2].caption = button1; CtrlButtons [2].x = 300; CtrlButtons [0].dwAddData = GetLargeSystemIcon (IDI_APPLICATION); rc = DialogBoxIndirectParam (&DlgButtons, hwnd, ButtonsBoxProc, 0L); if (buf) free (buf); return rc - IDC_BASE;}int myWinChoice (HWND hwnd, char* title, char* button1, char* button2, char* text, ...){ char * buf = NULL; int rc; if (strchr (text, '%')) { va_list args; int size = 0; int i = 0; va_start(args, text); do { size += 1000; if (buf) free(buf); buf = malloc(size); i = vsnprintf(buf, size, text, args); } while (i == size); va_end(args); } DlgButtons.caption = title; DlgButtons.controls = CtrlButtons; DlgButtons.controlnr = 4; CtrlButtons [1].caption = buf ? buf : text; CtrlButtons [2].caption = button1; CtrlButtons [3].caption = button2; CtrlButtons [2].x = 200; CtrlButtons [3].x = 300; CtrlButtons [0].dwAddData = GetLargeSystemIcon (IDI_APPLICATION); rc = DialogBoxIndirectParam (&DlgButtons, hwnd, ButtonsBoxProc, 0L); if (buf) free (buf); return rc - IDC_BASE;}int myWinTernary (HWND hwnd, char* title, char* button1, char* button2, char* button3, char* text, ...){ char * buf = NULL; int rc; if (strchr (text, '%')) { va_list args; int size = 0; int i = 0; va_start(args, text); do { size += 1000; if (buf) free(buf); buf = malloc(size); i = vsnprintf(buf, size, text, args); } while (i == size); va_end(args); } DlgButtons.caption = title; DlgButtons.controls = CtrlButtons; DlgButtons.controlnr = 5; CtrlButtons [1].caption = buf ? buf : text; CtrlButtons [2].caption = button1; CtrlButtons [3].caption = button2; CtrlButtons [4].caption = button3; CtrlButtons [2].x = 100; CtrlButtons [3].x = 200; CtrlButtons [4].x = 300; CtrlButtons [0].dwAddData = GetLargeSystemIcon (IDI_APPLICATION); rc = DialogBoxIndirectParam (&DlgButtons, hwnd, ButtonsBoxProc, 0L); if (buf) free (buf); return rc - IDC_BASE;}static int StatusWinProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam){ switch (message) { case MSG_PAINT: { HDC hdc; char* text; RECT rc; hdc = BeginPaint (hWnd); text = (char*) GetWindowAdditionalData (hWnd); if (text) { GetClientRect (hWnd, &rc); SetBkColor (hdc, COLOR_lightgray); TextOut (hdc, 5, (rc.bottom - GetSysCharHeight())>>1, text); } EndPaint (hWnd, hdc); return 0; } } return DefaultMainWinProc(hWnd, message, wParam, lParam);}HWND createStatusWin (HWND hParentWnd, int width, int height, char* title, char* text, ...){ HWND hwnd; char* buf = NULL; MAINWINCREATE CreateInfo; if (strchr (text, '%')) { va_list args; int size = 0; int i = 0; va_start(args, text); do { size += 1000; if (buf) free(buf); buf = malloc(size); i = vsnprintf(buf, size, text, args); } while (i == size); va_end(args); } width = ClientWidthToWindowWidth (WS_CAPTION | WS_BORDER, width); height= ClientHeightToWindowHeight (WS_CAPTION | WS_BORDER, height, FALSE); CreateInfo.dwStyle = WS_ABSSCRPOS | WS_CAPTION | WS_BORDER | WS_VISIBLE; CreateInfo.dwExStyle = WS_EX_NONE; CreateInfo.spCaption = title; CreateInfo.hMenu = 0; CreateInfo.hCursor = GetSystemCursor (IDC_WAIT); CreateInfo.hIcon = 0; CreateInfo.MainWindowProc = StatusWinProc; CreateInfo.lx = (GetGDCapability (HDC_SCREEN, GDCAP_MAXX) - width) >> 1; CreateInfo.ty = (GetGDCapability (HDC_SCREEN, GDCAP_MAXY) - height) >> 1; CreateInfo.rx = CreateInfo.lx + width; CreateInfo.by = CreateInfo.ty + height; CreateInfo.iBkColor = COLOR_lightgray; CreateInfo.dwAddData = buf ? (DWORD) buf : (DWORD) text; CreateInfo.hHosting = hParentWnd; hwnd = CreateMainWindow (&CreateInfo); if (hwnd == HWND_INVALID) return hwnd; SetWindowAdditionalData2 (hwnd, buf ? (DWORD) buf : 0); UpdateWindow (hwnd, TRUE); return hwnd;}void destroyStatusWin (HWND hwnd){ char* buf; DestroyMainWindow (hwnd); MainWindowThreadCleanup (hwnd); buf = (char*)GetWindowAdditionalData2 (hwnd); if (buf) free (buf);}#define _MARGIN 2#define _ID_TIMER 100static int ToolTipWinProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam){ switch (message) {#ifdef _TIMER_UNIT_10MS case MSG_CREATE: { int timeout = (int)GetWindowAdditionalData (hWnd); if (timeout >= 10) SetTimer (hWnd, _ID_TIMER, timeout / 10); break; } case MSG_TIMER: KillTimer (hWnd, _ID_TIMER); ShowWindow (hWnd, SW_HIDE); break;#endif case MSG_PAINT: { HDC hdc; const char* text; hdc = BeginPaint (hWnd); text = GetWindowCaption (hWnd); SetBkMode (hdc, BM_TRANSPARENT); SelectFont (hdc, GetSystemFont (SYSLOGFONT_WCHAR_DEF)); TabbedTextOut (hdc, _MARGIN, _MARGIN, text); EndPaint (hWnd, hdc); return 0; } case MSG_DESTROY: { KillTimer (hWnd, _ID_TIMER); return 0; } case MSG_SETTEXT: { int timeout = (int)GetWindowAdditionalData (hWnd); if (timeout >= 10) { KillTimer (hWnd, _ID_TIMER); SetTimer (hWnd, _ID_TIMER, timeout / 10); } break; } } return DefaultMainWinProc(hWnd, message, wParam, lParam);}HWND createToolTipWin (HWND hParentWnd, int x, int y, int timeout_ms, const char* text, ...){ HWND hwnd; char* buf = NULL; MAINWINCREATE CreateInfo; SIZE text_size; int width, height; if (strchr (text, '%')) { va_list args; int size = 0; int i = 0; va_start(args, text); do { size += 1000; if (buf) free(buf); buf = malloc(size); i = vsnprintf(buf, size, text, args); } while (i == size); va_end(args); } SelectFont (HDC_SCREEN, GetSystemFont (SYSLOGFONT_WCHAR_DEF)); GetTabbedTextExtent (HDC_SCREEN, buf ? buf : text, -1, &text_size); text_size.cx += _MARGIN << 1; text_size.cy = GetSysCharHeight () + (_MARGIN << 1); width = ClientWidthToWindowWidth (WS_THINFRAME, text_size.cx); height= ClientHeightToWindowHeight (WS_THINFRAME, text_size.cy, FALSE); if (x + width > g_rcScr.right) x = g_rcScr.right - width; if (y + height > g_rcScr.bottom) y = g_rcScr.bottom - height; CreateInfo.dwStyle = WS_ABSSCRPOS | WS_THINFRAME | WS_VISIBLE; CreateInfo.dwExStyle = WS_EX_TOPMOST | WS_EX_TOOLWINDOW; CreateInfo.spCaption = buf ? buf : text; CreateInfo.hMenu = 0; CreateInfo.hCursor = GetSystemCursor (IDC_ARROW); CreateInfo.hIcon = 0; CreateInfo.MainWindowProc = ToolTipWinProc; CreateInfo.lx = x; CreateInfo.ty = y; CreateInfo.rx = CreateInfo.lx + width; CreateInfo.by = CreateInfo.ty + height; CreateInfo.iBkColor = COLOR_yellow; CreateInfo.dwAddData = (DWORD) timeout_ms; CreateInfo.hHosting = hParentWnd; hwnd = CreateMainWindow (&CreateInfo); if (buf) free (buf); return hwnd;}void resetToolTipWin (HWND hwnd, int x, int y, const char* text, ...){ char* buf = NULL; SIZE text_size; int width, height; if (strchr (text, '%')) { va_list args; int size = 0; int i = 0; va_start(args, text); do { size += 1000; if (buf) free(buf); buf = malloc(size); i = vsnprintf(buf, size, text, args); } while (i == size); va_end(args); } SelectFont (HDC_SCREEN, GetSystemFont (SYSLOGFONT_WCHAR_DEF)); GetTabbedTextExtent (HDC_SCREEN, buf ? buf : text, -1, &text_size); text_size.cx += _MARGIN << 1; text_size.cy = GetSysCharHeight () + (_MARGIN << 1); width = ClientWidthToWindowWidth (WS_THINFRAME, text_size.cx); height= ClientHeightToWindowHeight (WS_THINFRAME, text_size.cy, FALSE); SetWindowCaption (hwnd, buf ? buf : text);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -