目录:
- MessageBox() 和 PlaySound()
- 获得窗口 或屏幕大小
- 获得字体大小
- 输出文字
- 屏蔽和显示控制台窗口
1、 MessageBox() 和 PlaySound()
MessageBox() 参数说明 :link here
PlaySound() 参数说明 :link here
// test for function MessageBox() and PlaySound()
// The program play the music file "c:\Users\Administrator\Desktop\Victory.wav" #include<windows.h>
#include<stdio.h>
#pragma comment(lib,"winmm.lib") // 告诉连接器与这个库连接,因为我们要播放多媒体声音
#pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"") int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nShowCmd)
{
system("title music player");
system("color 0b"); PlaySound("SystemStart",NULL, SND_ALIAS|SND_ASYNC);
printf("startup!\n"); MessageBox(NULL,TEXT("即将播放音乐"),TEXT("提示"),MB_OK|MB_ICONINFORMATION);
int rtn;
do{
printf("victory begin playing!\n");
PlaySound("c:\\Users\\Administrator\\Desktop\\Victory.wav", NULL, SND_FILENAME | SND_ASYNC);
rtn=MessageBox(NULL,TEXT("播放中,按重试重新播放,按取消结束播放"),TEXT("this is title"),MB_RETRYCANCEL |MB_ICONINFORMATION);
if(rtn==IDCANCEL)
{
PlaySound(NULL,NULL,NULL);
printf("music has been stop!\n");
break;
}
}while(rtn== IDRETRY); MessageBox(NULL,TEXT("播放程序已结束"),TEXT("提示"),MB_ICONINFORMATION);
return ;
}
MessageBox() PlaySound()
遗留问题—— 如何自定义MessageBox里的按钮?(可能思路:了解MessageBox的实现原理,以及相关的窗口原理,了解相应API ,所以可以先把Window编程相关知识看完,然后就是提高英语的阅读能力,使自己能够读懂MSDN上的document 和 stackFlow 等论坛上的专业解答)。。。。。。
2、获得窗口大小
窗口大小:
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
窗口矩形:
GetClientRect (hwnd, &rect) ;
屏幕:
cxScreen=GetSystemMetrics ( SM_CXSCREEN) ;
cyScreen=GetSystemMetrics ( SM_CYSCREEN) ;
3、获得字体大小
TEXTMETRIC tm ;
hdc = GetDC (hwnd) ; SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ;
cyChar = tm.tmHeight + tm.tmExternalLeading ;
或
cxChar = LOWORD (GetDialogBaseUnits ()) ;
cyChar = HIWORD (GetDialogBaseUnits ()) ;
4、输出文字
DrawText (hdc, TEXT ("Hello, Windows 98!"), -, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
或
//字体
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
//字的背景模式
SetBkMode (hdc, TRANSPARENT) ; TextOut (hdc, * cxChar, cyChar, szTop, lstrlen (szTop)) ;
5、屏蔽和显示控制台窗口
屏蔽控制台应用程序窗口 :link here
如何在Windows Application中应用printf函数? :link here