3 回答
TA貢獻1898條經驗 獲得超8個贊
一般使用API函數生成SDK窗體的順序是這樣的
首先包含<windows.h>
再定義一個Win32窗體程序的消息處理函數
如:LRESULT CALLBACK WinSunProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
在int WINAPI WinMain()主函數中
1.設計窗體類
定義一個WIN32的窗體類結構 WNDCLASSEXA并填充數據
2.注冊窗體類
將設計好的WIN32窗體結構類進行注冊
可以使用RegisterClassExA函數
3.創建窗體獲取窗體句柄
使用CreatWindow函數創建窗體 并返回窗體的句柄
4.更新窗體
創建一個消息循環截獲相關消息GetMessage
并把消息推送給窗體TranslateMessage()DispatchMessage()
接下來就是消息處理函數的認為了
可以使用默認 DefWindowProc()處理
TA貢獻2065條經驗 獲得超14個贊
生產窗體可以使用CreateWindowEx函數。
函數功能:該函數創建一個具有擴展風格的層疊式窗口、彈出式窗口或子窗口,其他與CreateWindow函數相同。
函數原型:
CreateWindowEx函數創建一個層疊的,自動彈出的(pop-up)或是一個子窗口通過擴展格式。另外這個函數的作用與CreateWindow函數的作用相同。要獲得更多的關于創建窗口的信息和關于CreateWindowEx函數參數的詳細描述。參見CreateWindow
HWND CreateWindowEx(
DWOR DdwExStyle, //窗口的擴展風格
LPCTSTR lpClassName, //指向注冊類名的指針
LPCTSTR lpWindowName, //指向窗口名稱的指針
DWORD dwStyle, //窗口風格
int x, //窗口的水平位置
int y, //窗口的垂直位置
int nWidth, //窗口的寬度
int nHeight, //窗口的高度
HWND hWndParent, //父窗口的句柄
HMENU hMenu, //菜單的句柄或是子窗口的標識符
HINSTANCE hInstance, //應用程序實例的句柄
LPVOID lpParam //指向窗口的創建數據
);例程:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 include<windows.h>#include<stdio.h>LRESULTCALLBACK WinDouProc(HWNDhwnd,// handle to windowUINTuMsg,// message identifierWPARAMwParam,// first message parameterLPARAMlParam// second message parameter);classCWnd{public:CWnd(){m_hWnd = NULL;}BOOLCreateEx(DWORDdwExStyle,// extended window styleLPCTSTRlpClassName,// pointer to registered class nameLPCTSTRlpWindowName,// pointer to window nameDWORDdwStyle,// window styleintx,// horizontal position of windowinty,// vertical position of windowintnWidth,// window widthintnHeight,// window heightHWNDhWndParent,// handle to parent or owner windowHMENUhMenu,// handle to menu or child-window identifierHANDLEhInstance,// handle to application instanceLPVOIDlpParam// pointer to window-creation data);BOOLShowWindow(intnCmdShow );BOOLUpdateWindow();public:HWNDm_hWnd;};BOOLCWnd::CreateEx(DWORDdwExStyle,// extended window styleLPCTSTRlpClassName,// pointer to registered class nameLPCTSTRlpWindowName,// pointer to window nameDWORDdwStyle,// window styleintx,// horizontal position of windowinty,// vertical position of windowintnWidth,// window widthintnHeight,// window heightHWNDhWndParent,// handle to parent or owner windowHMENUhMenu,// handle to menu or child-window identifierHANDLEhInstance,// handle to application instanceLPVOIDlpParam// pointer to window-creation data){m_hWnd = ::CreateWindowEx (dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,(HINSTANCE)hInstance,lpParam);if(m_hWnd != NULL)returnTRUE;elsereturnFALSE;}BOOLCWnd::ShowWindow(intnCmdShow){return::ShowWindow(m_hWnd,nCmdShow);}BOOLCWnd::UpdateWindow(){return::UpdateWindow(m_hWnd);}intWINAPI WinMain(HINSTANCEhInstance,// handle to current instanceHINSTANCEhPrevInstance,// handle to previous instanceLPSTRlpCmdLine,// pointer to command lineintnCmdShow// show state of window){WNDCLASS wndclass;//先設計窗口類wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);wndclass.hCursor = LoadCursor(NULL,IDC_HELP);wndclass.hIcon = LoadIcon(NULL,IDI_WARNING);wndclass.hInstance = hInstance;wndclass.lpfnWndProc = WinDouProc;wndclass.lpszClassName ="Magic_Maggie";wndclass.lpszMenuName = 0;wndclass.style = CS_VREDRAW | CS_HREDRAW;//某一個變量原油幾個變量去掉一個特征,可以用取反(~)后再進行與(&)//例如:style上去掉CS_NOCLOSE,可以style&~CS_NOCLOSE;RegisterClass(&wndclass);///注意先建立再注冊昂CWnd wnd;wnd.CreateEx(NULL,"Magic_Maggie","DouDou",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);wnd.ShowWindow(SW_SHOWNORMAL);wnd.UpdateWindow();MSG msg;//消息循環while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg);DispatchMessage(&msg);//觸發WinDouProc}return0;}LRESULTCALLBACK WinDouProc(HWNDhwnd,// handle to windowUINTuMsg,// message identifierWPARAMwParam,// first message parameterLPARAMlParam// second message parameter){switch(uMsg){caseWM_LBUTTONDOWN:MessageBox(hwnd,"您按下了鼠標左鍵昂","豆豆的程序",MB_OK);HDChdc;hdc = GetDC(hwnd);//The GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the device context.TextOut(hdc,0,0,"感謝您對豆豆程序的支持昂",strlen("感謝您對豆豆程序的支持昂"));ReleaseDC(hwnd,hdc);break;caseWM_CHAR:charszChar[20];sprintf(szChar,"Char is %d",wParam);MessageBox(hwnd,szChar,"豆豆的程序",MB_OK);break;caseWM_PAINT:PAINTSTRUCT ps;HDChDc;hDc = BeginPaint(hwnd,&ps);TextOut(hDc,0,0,"這個是重繪滴哦",strlen("這個是重繪滴哦"));EndPaint(hwnd,&ps);break;caseWM_CLOSE://這個case與下邊的destroy這個case不要弄錯了,否則窗口不出現,但任務管理器中運行if(IDYES == MessageBox(hwnd,"您真的要退出么?","豆豆的程序",MB_YESNO)){DestroyWindow(hwnd);}break;caseWM_DESTROY:PostQuitMessage(0);//////////////////////////////////////////?????????????????????break;default:returnDefWindowProc(hwnd,uMsg,wParam,lParam);// 別忘記了return}return0;}
- 3 回答
- 0 關注
- 669 瀏覽
添加回答
舉報
