【討論】這段代碼很有趣:Windows藍屏代碼
這個是在學習windows程序設計時,偶然發(fā)現(xiàn)的。程序運行后,在滾動條上右擊,在彈出的快捷菜單中選擇“滾動至此”,接下來程序會提示3次的"藍屏死機!請先保存好數(shù)據(jù)!",依次點3次確定后,windows?xp就藍屏重啟了。
程序本身沒有用到特殊的api。程序也很容易理解。
藍屏的原因是在處理滾動條消息時時,MessgeBox函數(shù)造成的。但是直接在滾動條的點擊并不會藍屏,具體什么原因我也不知道。估計是系統(tǒng)的bug。
VC?6.0?下編譯通過,以下是代碼。
復制內容到剪貼板代碼:
///////////////////////////////////////////////////////////////////////////?#include?<windows.h>
LRESULT?CALLBACK?WndProc?(HWND,?UINT,?WPARAM,?LPARAM)?;
int?WINAPI?WinMain?(HINSTANCE?hInstance,?HINSTANCE?hPrevInstance,
????????????????????PSTR?szCmdLine,?int?iCmdShow)
{
????????static?TCHAR?szAppName[]?=?TEXT?("MyWindow")?;
????????HWND?????????????hwnd?;
????????MSG?????????????msg?;
????????WNDCLASS?????wndclass?;
????????wndclass.style????????????????=?CS_HREDRAW?|?CS_VREDRAW?;
????????wndclass.lpfnWndProc????????=?WndProc?;
????????wndclass.cbClsExtra????????=?0?;
????????wndclass.cbWndExtra????????=?0?;
????????wndclass.hInstance????????=?hInstance?;
????????wndclass.hIcon????????????????=?LoadIcon?(NULL,?IDI_APPLICATION)?;
????????wndclass.hCursor????????=?LoadCursor?(NULL,?IDC_ARROW)?;
????????wndclass.hbrBackground????????=?(HBRUSH)?GetStockObject?(WHITE_BRUSH)?;
????????wndclass.lpszMenuName????????=?NULL?;
????????wndclass.lpszClassName????????=?szAppName?;
????????if(!RegisterClass?(&wndclass))
????????{
?????????????MessageBox?(NULL,?TEXT?("此程序必須運行在NT下!"),?szAppName,?MB_ICONERROR)?;
?????????????return?0;
????????}
????????hwnd?=?CreateWindow?(szAppName,
?????????????????????TEXT?("藍屏死機"),?????????????
?????????????????????????????WS_OVERLAPPEDWINDOW,
?????????????????????????????CW_USEDEFAULT,
?????????????????????????????CW_USEDEFAULT,
?????????????????????????????300,
?????????????????????????????210,
?????????????????????????????NULL,
?????????????????????????????NULL,
?????????????????????????????hInstance,
?????????????????????????????NULL)?;
????????ShowWindow?(hwnd,?iCmdShow)?;
????????UpdateWindow?(hwnd)?;
????????
????????while?(GetMessage?(&msg,?NULL,?0,?0))
????????{
?????????????TranslateMessage?(&msg)?;
?????????????DispatchMessage?(&msg)?;
????????}
????????return?msg.wParam?;
}
LRESULT?CALLBACK?WndProc?(HWND?hwnd,?UINT?message,?WPARAM?wParam,?LPARAM?lParam)
{
??????HDC???????????hdc?;
??????PAINTSTRUCT??ps?;
??????RECT???????????rect?;
??????static?HINSTANCE?hInstance;
??????static?HWND?hScrollBar;
??????switch?(message)
??????{
????????case?WM_CREATE:
?????????????hInstance?=?(HINSTANCE)?GetWindowLong?(hwnd,?GWL_HINSTANCE)?;?//取得實例句柄
?????????????hScrollBar?=?CreateWindow?(TEXT?("scrollbar"),?NULL,
??????????????????????????????????????????????WS_CHILD?|?WS_VISIBLE?|?WS_TABSTOP?|?SBS_VERT,????
??????????????????????????????????????????30,?30,?100,?100,?
??????????????????????????????????????????hwnd,?(HMENU)?1,?hInstance,?NULL)?;?//創(chuàng)建垂直滾動條
?????????????return?0?;
?????????????
????????case?WM_SIZE?:
?????????????MoveWindow?(hScrollBar,?30,?30,?LOWORD?(lParam)?/?2,?HIWORD?(lParam)-50,?TRUE)?;
?????????????return?0;
?????????????
????????case?WM_VSCROLL?:
?????????????MessageBox(NULL,?TEXT("藍屏死機!請先保存好數(shù)據(jù)!"),TEXT("請先保存好數(shù)據(jù)"),?MB_OK);?//藍屏的原因就是加上了這句。去掉這句就不會藍屏了。
?????????????Sleep(500);
?????????????return?0;
????????case?WM_PAINT:
?????????????hdc?=?BeginPaint?(hwnd,?&ps)?;
?????????????GetClientRect?(hwnd,?&rect)?;
?????????????DrawText?(hdc,?TEXT?("Windows?XP下的藍屏,死機程序!"),?-1,?&rect,?DT_SINGLELINE?|?DT_CENTER);
?????????????EndPaint?(hwnd,?&ps)?;
?????????????return?0?;
????????case?WM_DESTROY:
?????????????PostQuitMessage?(0)?;
?????????????return?0?;
????????}
????????return?DefWindowProc?(hwnd,?message,?wParam,?lParam)?;
}