Windows框架
#include<Windows.h>
#include<tchar.h>
#include"resource.h"
//全局變量
LPSTR g_MainFrame = "主框架";
LPSTR ?g_ClientFrame = "客戶區(qū)框架";
LPSTR g_ChildFrame[] = { "子框架1","子框架2" };
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
//函數(shù)執(zhí)行的入口函數(shù)
int WINAPI WinMain(
?? ?_In_ ? ? ? ? HINSTANCE hInstance,
?? ?_In_opt_ ?HINSTANCE hPrevInstance,
?? ?_In_ ? ? ? ? LPSTR lpCmdLine,
?? ?_In_ ? ? ? ? int nShowCmd)
{
?? ?//1.初始化需要注冊的窗口信息
?? ?WNDCLASS MainFrame = { 0 };
?? ?MainFrame.hbrBackground = (HBRUSH)::GetStockObject(GRAY_BRUSH);//背景
?? ?MainFrame.hCursor = ::LoadCursor(NULL, IDC_ARROW);
?? ?MainFrame.hInstance = hInstance;
?? ?MainFrame.lpfnWndProc = WndProc;
?? ?MainFrame.lpszClassName = g_MainFrame;
?? ?MainFrame.lpszMenuName = MAKEINTRESOURCE(IDR_MainFrame);//定位資源,通過MAKEINTRESOURCE轉(zhuǎn)換的id, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//也可以是資源名稱的字符串。
?? ?MainFrame.style = CS_VREDRAW | CS_HREDRAW;
?? ?//2.注冊MainFrame窗口
?? ?if (!RegisterClass(&MainFrame))
?? ?{
?? ??? ?MessageBox(NULL, _T("主框架注冊失敗!"), _T("Error"), MB_OK | MB_ICONERROR);//_T() 需用用到tchar.h頭文件
?? ?}
?? ?//3.創(chuàng)建窗口?? ?
?? ??? ?HWND mainframe_hwnd;
?? ??? ?mainframe_hwnd= CreateWindow(
?? ??? ??? ?g_MainFrame,
?? ??? ??? ?g_ClientFrame,
?? ??? ??? ?WS_OVERLAPPEDWINDOW,
?? ??? ??? ?CW_USEDEFAULT,?
?? ??? ??? ?CW_USEDEFAULT,?
?? ??? ??? ?CW_USEDEFAULT,?
?? ??? ??? ?CW_USEDEFAULT,
?? ??? ??? ?NULL,?
?? ??? ??? ?NULL,?
?? ??? ??? ?hInstance,
?? ??? ??? ?NULL);
?? ??? ?if(!mainframe_hwnd)
?? ??? ??? ?MessageBox(NULL, _T("主框架創(chuàng)建失敗!"), _T("Error"), MB_OK | MB_ICONERROR);
?? ?//4.顯示窗口
?? ??? ?ShowWindow(mainframe_hwnd, nShowCmd);
?? ?//5.刷新窗口
?? ??? ?UpdateWindow(mainframe_hwnd);
?? ?//6.信息捕獲
?? ??? ?MSG mainframe_msg;
?? ??? ?while (::GetMessage(&mainframe_msg, NULL, 0, 0))
?? ??? ?{
?? ??? ??? ?::TranslateMessage(&mainframe_msg);//TranslateMessage是用來把虛擬鍵消息轉(zhuǎn)換為字符消息。
?? ??? ??? ?::DispatchMessage(&mainframe_msg);//消息調(diào)度
?? ??? ?}
?? ??? ?return mainframe_msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
?? ?//處理消息
?? ?switch (message)
?? ?{
?? ??? ?/*1.關(guān)不窗口時,會收到該消息,PostQuitMessage()像系統(tǒng)表明終止當(dāng)前線程,沒有這個函數(shù)的話,窗口不會關(guān)閉*/
?? ??? ?case WM_DESTROY:
?? ??? ??? ?PostQuitMessage(0);//用于相應(yīng)關(guān)閉消息,否則線程不會關(guān)閉,導(dǎo)致系統(tǒng)阻塞
?? ??? ?return 0;
?? ?}
?? ?//將不需要處理的消息傳遞給系統(tǒng)作默認(rèn)處理
?? ?return DefWindowProc(hwnd, message, wParam, lParam);
}
?
總結(jié)
- 上一篇: FTP协议、电子邮件系统与Telnet远
- 下一篇: 操作系统——文件基本概念