#include<Windows.h>
#include<stdio.h>
//#include<stdlib.h>
LRESULT CALLBACK WinNewProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor=LoadCursor(hInstance,IDC_IBEAM);
wndclass.hIcon=LoadIcon(hInstance,IDI_SHIELD);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinNewProc;
wndclass.lpszClassName=TEXT("sunlight");
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndclass);
HWND hwnd;
hwnd=CreateWindow(TEXT("sunlight"),TEXT("moonlight"),WS_OVERLAPPEDWINDOW,0,0,600,500,NULL,NULL,hInstance,0);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
BOOL bret;
while((bret=GetMessage(&msg,hwnd,0,0))!=0)
{
if(bret==-1)
{
MessageBox(NULL,L"hwnd失效",L"hwnd error", MB_SERVICE_NOTIFICATION|MB_ICONERROR);
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
LRESULT CALLBACK WinNewProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_CHAR:
wchar_t szchar[20];
swprintf(szchar,L"char is %d",wParam);
MessageBox(hwnd,szchar,TEXT("seethelight"),0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,TEXT("light"),TEXT("事的"),0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50,TEXT("真的"),wcslen(L"真的"));
TextOut(hdc,0,100,TEXT("我们将看到阳光"),wcslen(L"我们将看到阳光"));
ReleaseDC(hwnd,hdc);
break;
case WM_RBUTTONDOWN:
MessageBox(hwnd,TEXT("右键单击"),TEXT("右键单击测试"),wcslen(L"右键单击测试"));
HDC hDc;
hDc=GetDC(hwnd);
TextOut(hDc,0,150,TEXT("just a test"),wcslen(L"just a test"));
ReleaseDC(hwnd,hDc);
break;
case WM_PAINT:
HDC hdC;
PAINTSTRUCT ps;
hdC=BeginPaint(hwnd,&ps);
TextOut(hdC,0,0,TEXT("windows programming......"),strlen("windows programming......"));//strlen与wcslen在这一样?
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,TEXT("do you want to close the window?"),TEXT("结束框"),MB_YESNO))
DestroyWindow(hwnd);
//PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}//未能实现,窗口内容保留重画