目录:
- MessageBox() 和 PlaySound()
- 获得窗口 或屏幕大小
- 获得字体大小
- 输出文字
- 屏蔽和显示控制台窗口
1、 MessageBox() 和 PlaySound()
MessageBox() 参数说明 :link here
PlaySound() 参数说明 :link here
1// test for function MessageBox() and PlaySound() 2 // The program play the music file "c:UsersAdministratorDesktopVictory.wav" 3 4 #include<windows.h> 5 #include<stdio.h> 6 #pragma comment(lib,"winmm.lib") // 告诉连接器与这个库连接,因为我们要播放多媒体声音 7 #pragma comment( linker, "/subsystem:"console" /entry:"WinMainCRTStartup"") 8 9 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nShowCmd) 10 { 11 system("title music player"); 12 system("color 0b"); 13 14 PlaySound("SystemStart",NULL, SND_ALIAS|SND_ASYNC); 15 printf("startup! "); 16 17 MessageBox(NULL,TEXT("即将播放音乐"),TEXT("提示"),MB_OK|MB_ICONINFORMATION); 18 int rtn; 19 do{ 20 printf("victory begin playing! "); 21 PlaySound("c:\Users\Administrator\Desktop\Victory.wav", NULL, SND_FILENAME | SND_ASYNC); 22 rtn=MessageBox(NULL,TEXT("播放中,按重试重新播放,按取消结束播放"),TEXT("this is title"),MB_RETRYCANCEL |MB_ICONINFORMATION); 23 if(rtn==IDCANCEL) 24 { 25 PlaySound(NULL,NULL,NULL); 26 printf("music has been stop! "); 27 break; 28 } 29 }while(rtn== IDRETRY); 30 31 MessageBox(NULL,TEXT("播放程序已结束"),TEXT("提示"),MB_ICONINFORMATION); 32 return 0; 33 }
遗留问题—— 如何自定义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!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
或
//字体 SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ; //字的背景模式 SetBkMode (hdc, TRANSPARENT) ; TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop)) ;
5、屏蔽和显示控制台窗口
屏蔽控制台应用程序窗口 :link here
如何在Windows Application中应用printf函数? :link here