建立一個 Button HWND hwndButton hwndButton = CreateWindow(
" BUTTON", // predefined class 不區分大小寫
"OK", // button text
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // styles 注意 如果樣式寫錯了 Button 将不會正常顯示
// Size and position values are given explicitly, because
// the CW_USEDEFAULT constant gives zero values for buttons.
10, // starting x position
10, // starting y position
100, // button width
100, // button height
hwnd, // parent window
NULL, // No menu
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // pointer not needed ================================================ #include <windows.h>
//expected constructor, destructor, or type conversion before
// LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow){
static TCHAR szAppName[] = TEXT("hello");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
// 加載 圖示供程式使用 IDI-圖示ID
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
// 加載 滑鼠光标供程式使用 IDC-遊标ID
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
//GetStockObject 取得一個圖形對象(此例中是取得視窗背景的畫刷對象)
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
// 視窗依照某一視窗類别建立,視窗類别用以辨別處理視窗消息的視窗消息處理程式。
// 注冊視窗
if(!RegisterClass(&wndclass)){
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName,MB_ICONERROR);
return 0;
}
// 根據視窗類别 WndClass 建立一個視窗
hwnd = CreateWindow(szAppName, // window class name
TEXT("Hello world"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, // parent window handle
NULL, // window menu handle
hInstance,//program instance handle
NULL); // creation parameters
// 在螢幕上顯示視窗
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)
{
HWND hwndButton;
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
char* str;
switch (message)
{
case WM_CREATE:
//PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
// 建立一個按鈕
hwndButton = CreateWindow(
"BUTTON", // predefined class
"OK", // button text
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // styles
// Size and position values are given explicitly, because
// the CW_USEDEFAULT constant gives zero values for buttons.
10, // starting x position
10, // starting y position
100, // button width
100, // button height
hwnd, // parent window
NULL, // No menu
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // pointer not needed
//MoveWindow(hwndSmaller, 100,100, 100, 100, TRUE) ;
return 0 ;
case WM_PAINT:
// 繪制視窗
hdc = BeginPaint (hwnd, &ps) ;
// 取得視窗顯示區域的大小
GetClientRect (hwnd, &rect) ;
str = "hello world!!!";
// 顯示字元串
// DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
// DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
// 輸出字元串
TextOut(hdc,20,20, str,strlen(str));
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
// 再消息隊列中插入一個[退出程式]消息
PostQuitMessage (0) ;
return 0 ;
}
// 執行内定的消息處理
return DefWindowProc (hwnd, message, wParam, lParam) ;
}