現在時刻とかIPアドレスとかメモリ使用率とか。その辺の情報を一瞬だけ見たいって人用のツールです。始めはフリーソフトに入れようと思ったんですけど、たいしたものじゃないのでソースを公開します。
以下のファイルを用意(新規作成)して、WS2_32.LIBをプロジェクトへ追加してください。
実行したらこんな感じに出ます。一部ぼかしをかけてますが実際にはかかりませんよ。
iniファイルの値を変えることによって、フォントサイズや色、表示位置などを変えることが出来ます。ただエラーチェックをあまりやっていないので、チェック処理を追加するなりして対応してください。
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include "GetInfo.h"
#define INI_FILE_PATH "DispInfo.ini"
/* グローバル変数 */
//表示位置
int DisplayX;
int DisplayY;
//文字の高さ
int FontHeight;
//書体名
char FontFace[80];
//文字色
int FontColorR;
int FontColorG;
int FontColorB;
//文字の影の色
int FontShadowR;
int FontShadowG;
int FontShadowB;
/* 関数プロトタイプ */
int LoadIni();
HFONT SetFont();
int WriteText(HDC hdc, char * buf);
//====================================================================================================//
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
//-clearがきた場合は画面再描画
if (strcmp(lpCmdLine, "-clear") == 0) {
InvalidateRect(0, NULL, TRUE);
} else {
//iniファイルより値取得
LoadIni();
//デスクトップのデバイスコンテキストハンドル取得
HDC hdc = GetDC(0);
SetBkMode(hdc, TRANSPARENT);
HFONT hFont, hOldFont;
hFont = SetFont();
hOldFont = (HFONT)SelectObject(hdc, hFont);
char buf[256];
//ホスト名、IPアドレス取得
ZeroMemory(buf, 256);
GetHostInfo(buf);
WriteText(hdc, buf);
//現在時刻取得
ZeroMemory(buf, 256);
GetTimeInfo(buf);
DisplayY = DisplayY + FontHeight + 5;
WriteText(hdc, buf);
//メモリ情報取得
ZeroMemory(buf, 256);
GetMemoryInfo(buf);
DisplayY = DisplayY + FontHeight + 5;
WriteText(hdc, buf);
//仮想メモリ情報取得
ZeroMemory(buf, 256);
GetSwapMemoryInfo(buf);
DisplayY = DisplayY + FontHeight + 5;
WriteText(hdc, buf);
SelectObject(hdc, hOldFont);
DeleteObject(hFont);
//デスクトップのデバイスコンテキスト開放
ReleaseDC(0, hdc);
}
return 0;
}
//フォントの設定
HFONT SetFont() {
HFONT hFont;
hFont = CreateFont(FontHeight, //フォント高さ
0, //文字幅
0, //テキストの角度
0, //ベースラインとx軸との角度
FW_HEAVY, //フォントの重さ(太さ)
FALSE, //イタリック体
FALSE, //アンダーライン
FALSE, //打ち消し線
SHIFTJIS_CHARSET, //文字セット
OUT_DEFAULT_PRECIS, //出力精度
CLIP_DEFAULT_PRECIS, //クリッピング精度
PROOF_QUALITY, //出力品質
FIXED_PITCH | FF_MODERN, //ピッチとファミリー
FontFace); //書体名
return hFont;
}
//テキストの書き出し
int WriteText(HDC hdc, char * buf) {
SetTextColor(hdc, RGB(FontShadowR, FontShadowG, FontShadowB));
TextOut(hdc, DisplayX+3, DisplayY+3, buf, strlen(buf));
SetTextColor(hdc, RGB(FontColorR, FontColorG, FontColorB));
TextOut(hdc, DisplayX, DisplayY, buf, strlen(buf));
return 0;
}
//INIファイルを読む
int LoadIni() {
char path[500];ZeroMemory(path, 500);
GetCurrentDirectory(500, path);
sprintf(path, "%s\\%s", path, INI_FILE_PATH);
char DisplayXchar[5];ZeroMemory(DisplayXchar,5);
GetPrivateProfileString("DISP", "DisplayX", 0, DisplayXchar, 5, path);
DisplayX = atoi(DisplayXchar);
char DisplayYchar[5];ZeroMemory(DisplayYchar,5);
GetPrivateProfileString("DISP", "DisplayY", 0, DisplayYchar, 5, path);
DisplayY = atoi(DisplayYchar);
char FontHeightchar[3];ZeroMemory(FontHeightchar,3);
GetPrivateProfileString("DISP", "FontHeight", "15", FontHeightchar, 3, path);
FontHeight = atoi(FontHeightchar);
ZeroMemory(FontFace, 80);
GetPrivateProfileString("DISP", "FontFace", "MS 明朝", FontFace, 80, path);
char FontColorRchar[4];ZeroMemory(FontColorRchar,4);
GetPrivateProfileString("DISP", "FontColorR", 0, FontColorRchar, 4, path);
FontColorR = atoi(FontColorRchar);
char FontColorGchar[4];ZeroMemory(FontColorGchar,4);
GetPrivateProfileString("DISP", "FontColorG", 0, FontColorGchar, 4, path);
FontColorG = atoi(FontColorGchar);
char FontColorBchar[4];ZeroMemory(FontColorBchar,4);
GetPrivateProfileString("DISP", "FontColorB", 0, FontColorBchar, 4, path);
FontColorB = atoi(FontColorBchar);
char FontShadowRchar[4];ZeroMemory(FontShadowRchar,4);
GetPrivateProfileString("DISP", "FontShadowR", 0, FontShadowRchar, 4, path);
FontShadowR = atoi(FontShadowRchar);
char FontShadowGchar[4];ZeroMemory(FontShadowGchar,4);
GetPrivateProfileString("DISP", "FontShadowG", 0, FontShadowGchar, 4, path);
FontShadowG = atoi(FontShadowGchar);
char FontShadowBchar[4];ZeroMemory(FontShadowBchar,4);
GetPrivateProfileString("DISP", "FontShadowB", 0, FontShadowBchar, 4, path);
FontShadowB = atoi(FontShadowBchar);
return 0;
}
#ifdef __GETINFO_H__ #define __GETINFO_H__ int GetHostInfo(char * str); int GetTimeInfo(char * str); int GetMemoryInfo(char * str); int GetSwapMemoryInfo(char * str); #endif__GETINFO_H__
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include "GetInfo.h"
//曜日
const char *DayOfWeek[7] = {"日", "月", "火", "水", "木", "金", "土"};
//ホスト情報取得
int GetHostInfo(char * str) {
//コンピュータ名取得
char computername[256];
unsigned long length = 256;
ZeroMemory(computername, 256);
GetComputerName(computername, &length);
//ユーザー名取得
char username[256];
ZeroMemory(username, 256);
GetUserName(username, &length);
//IPアドレス取得
WSAData wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData);
struct hostent *phe = gethostbyname(computername);
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
WSACleanup();
//値作成
sprintf(str, "%s %s %s", computername, username, inet_ntoa(addr));
return 0;
}
//現在時刻を取得strに値を入力
int GetTimeInfo(char * str) {
SYSTEMTIME st;
GetLocalTime(&st);
sprintf(str,"%04d/%02d/%02d(%s) %02d:%02d:%02d.%d",
st.wYear, st.wMonth, st.wDay, DayOfWeek[st.wDayOfWeek],
st.wHour, st.wMinute, st.wMinute, st.wMilliseconds);
return 0;
}
//メモリ情報取得
int GetMemoryInfo(char * str) {
MEMORYSTATUS m;
GlobalMemoryStatus(&m);
sprintf(str,
"メモリ使用率%d% %dMB/%dMB",
m.dwMemoryLoad,
(m.dwTotalPhys - m.dwAvailPhys) / (1024*1024),
m.dwTotalPhys / (1024*1024));
return 0;
}
//仮想メモリ情報取得
int GetSwapMemoryInfo(char * str) {
MEMORYSTATUS m;
GlobalMemoryStatus(&m);
double max = m.dwTotalPageFile;
double now = max - m.dwAvailPageFile;
sprintf(str,
"仮想メモリ使用率%d% %dMB/%dMB",
(long)(now * 100 / max),
(long)now / (1024*1024),
(long)max / (1024*1024));
return 0;
}
[DISP] DisplayX=500 DisplayY=10 FontHeight=30 FontFace=MS ゴシック FontColorR=200 FontColorG=255 FontColorB=255 FontShadowR=100 FontShadowG=100 FontShadowB=150