求解答 关与WIN32的诸多问题
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
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.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor = LoadCursor(0,IDC_ARROW);
wndcls.hIcon = LoadIcon(0, IDI_APPLICATION);
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WindowProc;
wndcls.lpszClassName = "WeiXin";
wndcls.lpszMenuName = 0;
wndcls.style = CS_VREDRAW | CS_HREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindow("WeiXin", "计算机", WS_OVERLAPPEDWINDOW,0,0, 300, 500, 0,0,hInstance,0);
ShowWindow(hwnd, nCmdShow);
MSG msg;
while(GetMessage(&msg, hwnd, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static string m_str="";
static CPoint point = 0;
static CPoint point1;
//HDC hdc;
//hdc = GetDC(hwnd);
PAINTSTRUCT ps;
TEXTMETRIC tm;
CClientDC dc(this);
dc.GetTextMetrics(&tm);
//CSize sz = dc.GetTextExtent(m_str);
switch(uMsg)
{
case WM_PAINT:
//hdc = BeginPaint(hwnd, &ps);
dc.TextOut(50, 50,"dhfkhjkfds");
//EndPaint(hwnd,&ps);
break;
case WM_CHAR:
if (13==wParam)
{
m_str.empty();
//CPoint point1;
point.y += tm.tmHeight;
}
else if(8 == wParam)
{
COLORREF *pOld = dc.SetTextColor(dc.GetBkColor());
dc.TextOut(point.x,point.y,m_str);
m_str -= wParam;
dc.SetTextColor(pOld);
point1.x = point.x + sz.cx;
point1.y = point.y;
}
else
{
m_str += wParam;
}
dc.TextOut(point.x,point.y,m_str);
CSize sz = dc.GetTextExtent(m_str);
point1.x = point.x + sz.cx;
point1.y = point.y;
SetCaretPos(point1.x,point1.y);
break;
case WM_LBOUTTONDOWN:
m_str.empty();
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
SetCaretPos(point.x,point.y);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"你是否要退出程序","WeiXin",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
有很多问题,求各位高手细心解答
[解决办法]
给你一个示例自己运行下:
#include <Windows.h>#include <tchar.h>LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ switch (uMsg) { case WM_CREATE: return 0; case WM_PAINT: break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, uMsg, wParam, lParam);}int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, PTSTR /*pszCmdLine*/, int /*nCmdShow*/){ WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcex.lpszMenuName = NULL; wcex.lpszClassName = _T("Windows Demo Class"); wcex.hIconSm = wcex.hIcon; if (!RegisterClassEx(&wcex)) { MessageBox(NULL, _T("RegisterClassEx failed!"), _T("Windows Demo"), MB_ICONERROR); return 0; } HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, _T("Windows Demo Class"), _T("Windows Demo"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); MSG msg; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;}