WINDOWS 编程
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
){
switch(uMsg){
case WM_CHAR:
char szChar[20];
sprintf(szChar,"char is %d",wParam);
MessageBox(hwnd,(LPCWSTR)szChar,(LPCWSTR)"weixin",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,(LPCWSTR)"button clicked",(LPCWSTR)"weixin",0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,100,100,(LPCWSTR)"北京计算机培训中心。",strlen("北京计算机培训中心。"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hdc1;
PAINTSTRUCT ps;
hdc1 = BeginPaint(hwnd,&ps);
TextOut(hdc1,100,200,(LPCWSTR)"北京paint测试中心。",strlen("北京paint测试中心。"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES == MessageBox(hwnd,(LPCWSTR)"是否真的结束?",(LPCWSTR)"weixin",MB_YESNO))
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
){
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbClsExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor = LoadCursor(NULL,IDC_APPSTARTING);
wndcls.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndcls.lpfnWndProc = WinSunProc;
wndcls.lpszClassName = (LPCWSTR)"weixin2003";
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindow((LPCWSTR)"weixin2003",(LPCWSTR)"北京test",WS_OVERLAPPEDWINDOW,100,100,400,600,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
我写了这段程序结果却显示不出窗口,为什么?我在vc2010上写的 windows
[解决办法]
声明窗口类的时候需要初始化。
WNDCLASS wndcls;
WNDCLASS wndcls = {0};