win32apiの豆知識
Posted: 2011年1月27日(木) 23:51
知っていると便利な知識や面白い関数などなどを書き込むトピックです。
win32apiに関係のあるものなら何でもOKです。是非書き込んでください^^
win32apiに関係のあるものなら何でもOKです。是非書き込んでください^^
//ボタンを描く
//ボタンの絵を描いているだけなので、クリックしても何も起きません(当たり前ですが)
void DrawButton(HDC hdc, int x, int y, int width, int height)
{
RECT rect = { x, y, x + width, y + height };
//ボタンコントロールを描くにはuTypeにDFC_BUTTONを指定します。
//他にはDFC_CAPTION(タイトルバー)、DFC_SCROLL(スクロールバー)などがあります。
//uTypeがDFC_BUTTONの場合、DFCS_BUTTONPUSHを指定すると、プッシュボタンが描画されます。
//他にはDFCS_BUTTONCHECK(チェックボックス)、DFCS_BUTTONRADIO(ラジオボタン)などがあります。
DrawFrameControl(hdc, &rect, DFC_BUTTON, DFCS_BUTTONPUSH);
}
#include<stdio.h>
#include<windows.h>
int main()
{
char szBuff[512];
DWORD nSize = sizeof(szBuff) / sizeof(szBuff[0]);
BOOL nRet;
nRet = GetUserName(szBuff,&nSize);
if(nRet == FALSE){
strncpy(szBuff,"error",sizeof(szBuff));
}
printf("このパソコンのユーザー名は%sです\n",szBuff);
printf("文字数は%dです\n",nSize);
return 0;
}
#include <windows.h>
#include <tchar.h>
//card.dll内の関数
typedef BOOL (WINAPI *pfcdtInit)(int FAR *pdxCard, int FAR *pdyCard);
typedef BOOL (WINAPI *pfcdtDraw)(HDC hdc, int x, int y, int card, int type, COLORREF color);
typedef void (WINAPI *pfcdtTerm)(void);
pfcdtInit cdtInit;
pfcdtDraw cdtDraw;
pfcdtTerm cdtTerm;
//cdtDrawで使うと便利なマクロ
//指定した番号、スートからカードを決める
#define CARD(n, k) ((n) * 4 + (k))
//スート(絵柄マーク)
#define SUIT_CLUBS (0) //クラブ
#define SUIT_DIAMONDS (1) //ダイアモンド
#define SUIT_HEARTS (2) //ハート
#define SUIT_SPADES (3) //スペード
//カードの模様
#define B_CROSSHATCH (53)
#define B_PLAID (54)
#define B_WEAVE (55)
#define B_ROBOT (56)
#define B_ROSES (57)
#define B_IVYBLACK (58)
#define B_IVYBLUE (59)
#define B_FISHCYAN (60)
#define B_FISHBLUE (61)
#define B_SHELL (62)
#define B_CASTLE (63)
#define B_BEACH (64)
#define B_CARDHAND (65)
#define B_UNUSED (66)
#define B_X (67)
#define B_O (68)
//カードの描画タイプ
#define D_FACES (0) //表(数字が描かれている面)
#define D_BACKS (1) //裏(模様が描かれている面)
#define D_INVERT (2) //カードを消す(消す色はcolorで指定)
//ウィンドウプロシージャ
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
HDC hdc;
PAINTSTRUCT ps;
static HMODULE hDll;
static int CardWidth, CardHeight;
switch(msg)
{
//ウィンドウ生成
case WM_CREATE:
//cards.dll読み込み
hDll = LoadLibrary(_T("cards.dll"));
if(hDll == NULL) return -1;
cdtInit = (pfcdtInit)GetProcAddress(hDll, "cdtInit");
cdtDraw = (pfcdtDraw)GetProcAddress(hDll, "cdtDraw");
cdtTerm = (pfcdtTerm)GetProcAddress(hDll, "cdtTerm");
//cards.dll初期化
cdtInit(&CardWidth, &CardHeight);
return 0;
//描画処理
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//カードの描画
for(int i = SUIT_CLUBS; i <= SUIT_SPADES; i ++)
{
for(int j = 0; j < 13; j ++)
{
cdtDraw(hdc, j * CardWidth, (i - SUIT_CLUBS) * CardHeight, CARD(j, i), D_FACES, RGB(255, 255, 255));
}
}
//カードの模様の描画
for(int i = B_CROSSHATCH; i <= B_O; i ++)
{
cdtDraw(hdc, (i - B_CROSSHATCH) * CardWidth / 2, 4 * CardHeight, i, D_BACKS, RGB(255, 255, 255));
}
EndPaint(hWnd, &ps);
break;
//ウィンドウを閉じる
case WM_CLOSE:
cdtTerm();
DestroyWindow(hWnd);
break;
//終了
case WM_DESTROY:
FreeLibrary(hDll);
DestroyWindow(hWnd);
PostQuitMessage(0);
break;
//その他
default:
return DefWindowProc(hWnd, msg, wp, lp);
}
return 0;
}
//ウィンドウクラスの登録
ATOM InitApp(HINSTANCE hInst)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
wc.hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = _T("cardsdll");
wc.hIconSm = (HICON) LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
return RegisterClassEx(&wc);
}
//ウィンドウの生成
BOOL InitInstance(HINSTANCE hInst, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow(_T("cardsdll"), _T("DLL"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if(!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//WinMain関数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
BOOL bRet;
//初期化処理
if(!InitApp(hInstance)) return FALSE;
if(!InitInstance(hInstance, nShowCmd)) return FALSE;
//メッセージループ
while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if(bRet == -1)
{
MessageBox(NULL, TEXT("GetMessage Error"), TEXT("ERROR"), MB_OK | MB_ICONHAND);
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
BOOL WINAPI cdtDrawExt(HDC hdc, int x, int y, int w, int h, int card, int type, COLORREF color);Cir さんが書きました:これらの関数は「cards.dll」内にあるので、LoadLibrary関数、GetProcAddress関数を使用して関数のアドレスを所得し、呼び出してください。
#ifndef ISLE_TRACE_H
#define ISLE_TRACE_H
#ifdef _DEBUG
#include <windows.h>
#include <stdarg.h>
static void TRACE(LPCTSTR format, ...)
{
TCHAR str[1024+1];
va_list args;
va_start(args, format);
wvsprintf(str, format, args);
OutputDebugString(str);
va_end(args);
}
#else
#define TRACE(...)
#endif
#endif
そのようなものはありませんでしたが、必要もありませんでした。shiro4ao さんが書きました:実際に呼び出す際には、Dwmapi.hとDwmapi.libが必要です。
#include <windows.h>
LPCSTR szWindowClass = "SAMPLE_CLASS";
LPCSTR szWindowTitle = "Sample Application";
LPCSTR szHello = "Hello, Windows C Programming !";
typedef struct _MARGINS {
int cxLeftWidth;
int cxRightWidth;
int cyTopHeight;
int cyBottomHeight;
} MARGINS, *PMARGINS;
typedef HRESULT (WINAPI *pDwmExtendFrameIntoClientArea)
(HWND hWnd,
const MARGINS *pMarInset
);
pDwmExtendFrameIntoClientArea _DwmExtendFrameIntoClientArea;
HMODULE hdll;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rt;
switch (message) {
case WM_CREATE:
{
MARGINS m={-1};
_DwmExtendFrameIntoClientArea(hWnd,&m);
}
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
FillRect(hdc,&rt,GetStockObject(BLACK_BRUSH));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
WNDCLASSEX wcex;
HWND hWnd;
hdll=LoadLibrary("Dwmapi.dll");
if(hdll==NULL) {
MessageBox(hWnd,"dllが読み込めません。","エラー",MB_OK | MB_ICONWARNING | MB_TOPMOST);
return 1;
} else {
_DwmExtendFrameIntoClientArea=(pDwmExtendFrameIntoClientArea)
GetProcAddress(hdll,"DwmExtendFrameIntoClientArea");
if(_DwmExtendFrameIntoClientArea==NULL) {
FreeLibrary(hdll);
MessageBox(hWnd,"関数が読み込めません。","エラー",MB_OK | MB_ICONWARNING | MB_TOPMOST);
return 1;
}
}
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
hWnd = CreateWindow(szWindowClass,
szWindowTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
FreeLibrary(hdll);
return msg.wParam;
}