3 回答

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
一般使用API函數(shù)生成SDK窗體的順序是這樣的
首先包含<windows.h>
再定義一個(gè)Win32窗體程序的消息處理函數(shù)
如:LRESULT CALLBACK WinSunProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
在int WINAPI WinMain()主函數(shù)中
1.設(shè)計(jì)窗體類(lèi)
定義一個(gè)WIN32的窗體類(lèi)結(jié)構(gòu) WNDCLASSEXA并填充數(shù)據(jù)
2.注冊(cè)窗體類(lèi)
將設(shè)計(jì)好的WIN32窗體結(jié)構(gòu)類(lèi)進(jìn)行注冊(cè)
可以使用RegisterClassExA函數(shù)
3.創(chuàng)建窗體獲取窗體句柄
使用CreatWindow函數(shù)創(chuàng)建窗體 并返回窗體的句柄
4.更新窗體
創(chuàng)建一個(gè)消息循環(huán)截獲相關(guān)消息GetMessage
并把消息推送給窗體TranslateMessage()DispatchMessage()
接下來(lái)就是消息處理函數(shù)的認(rèn)為了
可以使用默認(rèn) DefWindowProc()處理

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
生產(chǎn)窗體可以使用CreateWindowEx函數(shù)。
函數(shù)功能:該函數(shù)創(chuàng)建一個(gè)具有擴(kuò)展風(fēng)格的層疊式窗口、彈出式窗口或子窗口,其他與CreateWindow函數(shù)相同。
函數(shù)原型:
CreateWindowEx函數(shù)創(chuàng)建一個(gè)層疊的,自動(dòng)彈出的(pop-up)或是一個(gè)子窗口通過(guò)擴(kuò)展格式。另外這個(gè)函數(shù)的作用與CreateWindow函數(shù)的作用相同。要獲得更多的關(guān)于創(chuàng)建窗口的信息和關(guān)于CreateWindowEx函數(shù)參數(shù)的詳細(xì)描述。參見(jiàn)CreateWindow
HWND CreateWindowEx(
DWOR DdwExStyle, //窗口的擴(kuò)展風(fēng)格
LPCTSTR lpClassName, //指向注冊(cè)類(lèi)名的指針
LPCTSTR lpWindowName, //指向窗口名稱(chēng)的指針
DWORD dwStyle, //窗口風(fēng)格
int x, //窗口的水平位置
int y, //窗口的垂直位置
int nWidth, //窗口的寬度
int nHeight, //窗口的高度
HWND hWndParent, //父窗口的句柄
HMENU hMenu, //菜單的句柄或是子窗口的標(biāo)識(shí)符
HINSTANCE hInstance, //應(yīng)用程序?qū)嵗木浔?br/>LPVOID lpParam //指向窗口的創(chuàng)建數(shù)據(jù)
);例程:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 include<windows.h>
#include<stdio.h>
LRESULT
CALLBACK WinDouProc(
HWND
hwnd,
// handle to window
UINT
uMsg,
// message identifier
WPARAM
wParam,
// first message parameter
LPARAM
lParam
// second message parameter
);
class
CWnd
{
public
:
CWnd()
{
m_hWnd = NULL;
}
BOOL
CreateEx(
DWORD
dwExStyle,
// extended window style
LPCTSTR
lpClassName,
// pointer to registered class name
LPCTSTR
lpWindowName,
// pointer to window name
DWORD
dwStyle,
// window style
int
x,
// horizontal position of window
int
y,
// vertical position of window
int
nWidth,
// window width
int
nHeight,
// window height
HWND
hWndParent,
// handle to parent or owner window
HMENU
hMenu,
// handle to menu or child-window identifier
HANDLE
hInstance,
// handle to application instance
LPVOID
lpParam
// pointer to window-creation data
);
BOOL
ShowWindow(
int
nCmdShow );
BOOL
UpdateWindow();
public
:
HWND
m_hWnd;
};
BOOL
CWnd::CreateEx(
DWORD
dwExStyle,
// extended window style
LPCTSTR
lpClassName,
// pointer to registered class name
LPCTSTR
lpWindowName,
// pointer to window name
DWORD
dwStyle,
// window style
int
x,
// horizontal position of window
int
y,
// vertical position of window
int
nWidth,
// window width
int
nHeight,
// window height
HWND
hWndParent,
// handle to parent or owner window
HMENU
hMenu,
// handle to menu or child-window identifier
HANDLE
hInstance,
// handle to application instance
LPVOID
lpParam
// 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)
return
TRUE;
else
return
FALSE;
}
BOOL
CWnd::ShowWindow(
int
nCmdShow)
{
return
::ShowWindow(m_hWnd,nCmdShow);
}
BOOL
CWnd::UpdateWindow()
{
return
::UpdateWindow(m_hWnd);
}
int
WINAPI WinMain(
HINSTANCE
hInstance,
// handle to current instance
HINSTANCE
hPrevInstance,
// handle to previous instance
LPSTR
lpCmdLine,
// pointer to command line
int
nCmdShow
// show state of window
)
{
WNDCLASS wndclass;
//先設(shè)計(jì)窗口類(lèi)
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;
//某一個(gè)變量原油幾個(gè)變量去掉一個(gè)特征,可以用取反(~)后再進(jìn)行與(&)
//例如:style上去掉CS_NOCLOSE,可以style&~CS_NOCLOSE;
RegisterClass(&wndclass);
///注意先建立再注冊(cè)昂
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;
//消息循環(huán)
while
(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
//觸發(fā)WinDouProc
}
return
0;
}
LRESULT
CALLBACK WinDouProc(
HWND
hwnd,
// handle to window
UINT
uMsg,
// message identifier
WPARAM
wParam,
// first message parameter
LPARAM
lParam
// second message parameter
)
{
switch
(uMsg)
{
case
WM_LBUTTONDOWN:
MessageBox(hwnd,
"您按下了鼠標(biāo)左鍵昂"
,
"豆豆的程序"
,MB_OK);
HDC
hdc;
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,
"感謝您對(duì)豆豆程序的支持昂"
,
strlen
(
"感謝您對(duì)豆豆程序的支持昂"
));
ReleaseDC(hwnd,hdc);
break
;
case
WM_CHAR:
char
szChar[20];
sprintf
(szChar,
"Char is %d"
,wParam);
MessageBox(hwnd,szChar,
"豆豆的程序"
,MB_OK);
break
;
case
WM_PAINT:
PAINTSTRUCT ps;
HDC
hDc;
hDc = BeginPaint(hwnd,&ps);
TextOut(hDc,0,0,
"這個(gè)是重繪滴哦"
,
strlen
(
"這個(gè)是重繪滴哦"
));
EndPaint(hwnd,&ps);
break
;
case
WM_CLOSE:
//這個(gè)case與下邊的destroy這個(gè)case不要弄錯(cuò)了,否則窗口不出現(xiàn),但任務(wù)管理器中運(yùn)行
if
(IDYES == MessageBox(hwnd,
"您真的要退出么?"
,
"豆豆的程序"
,MB_YESNO))
{
DestroyWindow(hwnd);
}
break
;
case
WM_DESTROY:
PostQuitMessage(0);
//////////////////////////////////////////?????????????????????
break
;
default
:
return
DefWindowProc(hwnd,uMsg,wParam,lParam);
// 別忘記了return
}
return
0;
}
- 3 回答
- 0 關(guān)注
- 667 瀏覽
添加回答
舉報(bào)