vs2017 c++写窗口 运行无错误,但不显示窗口 为什么? 25
#include"windows.h"#include"stdio.h"LRESULTCALLBACKWindowProc(HWNDhwnd,UINTuMsg,WPARA...
#include "windows.h"
#include "stdio.h"
LRESULT CALLBACK WindowProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbClsExtra = 0;
wndcls.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WindowProc;
wndcls.lpszClassName ="yulong2018";
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindowA("yulong2018", "我的窗口", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400,
NULL,NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
TCHAR szHello[32] = "创建一个简单窗口。";
switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);
MessageBoxA(hwnd, szChar, "char", 0);
break;
case WM_LBUTTONDOWN:
MessageBoxA(hwnd, "mouse cliked", "message", 0);
HDC hdc;
hdc=GetDC(hwnd);
TextOutA(hdc, 0, 50, "my window",strlen("my window"));
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT:
HDC hdv;
PAINTSTRUCT ps;
hdv=BeginPaint(hwnd, &ps);
RECT rt;
GetClientRect(hwnd, &rt);
DrawText(hdv, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if (IDYES == MessageBoxA(hwnd, "do you quit ?", "message", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
} 展开
#include "stdio.h"
LRESULT CALLBACK WindowProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbClsExtra = 0;
wndcls.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WindowProc;
wndcls.lpszClassName ="yulong2018";
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindowA("yulong2018", "我的窗口", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400,
NULL,NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc
(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
TCHAR szHello[32] = "创建一个简单窗口。";
switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);
MessageBoxA(hwnd, szChar, "char", 0);
break;
case WM_LBUTTONDOWN:
MessageBoxA(hwnd, "mouse cliked", "message", 0);
HDC hdc;
hdc=GetDC(hwnd);
TextOutA(hdc, 0, 50, "my window",strlen("my window"));
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT:
HDC hdv;
PAINTSTRUCT ps;
hdv=BeginPaint(hwnd, &ps);
RECT rt;
GetClientRect(hwnd, &rt);
DrawText(hdv, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if (IDYES == MessageBoxA(hwnd, "do you quit ?", "message", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询