#include <Windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd, /*視窗句柄*/
UINT uMsg, /*資訊標識*/
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance, /*當前實體句柄*/
HINSTANCE hPrevInstance, /*上一個實體句柄*/
LPSTR lpCmdLine, /*命令列*/
int nCmdShow /*顯示狀態*/
)
{
/*設計一個視窗類*/
WNDCLASS wndcle;
wndcle.cbClsExtra = 0;
wndcle.cbWndExtra = 0;
wndcle.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndcle.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcle.hIcon = LoadIcon(NULL, IDI_ERROR);
wndcle.hInstance = hInstance;
wndcle.lpfnWndProc = WinSunProc;
wndcle.lpszClassName = TEXT("lpl2018");
wndcle.lpszMenuName = NULL;
wndcle.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcle);
char pszErrMsg[128] = { 0 };
/*創建視窗,定義一個變數用來保存成功創建視窗后回傳的句柄*/
HWND hwnd;
hwnd = CreateWindow(TEXT("LCK2018"), TEXT("http://www.baidu.com"), WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
snprintf(pszErrMsg,sizeof(pszErrMsg),TEXT("err[%d]"), GetLastError());
OutputDebugString(pszErrMsg);
MessageBox(NULL, "CreateWindow Err", "Error", 0);
}
/*顯示及重繪視窗*/
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
/*定義訊息結構體,開始訊息回圈*/
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/*撰寫視窗程序函式*/
LRESULT CALLBACK WinSunProc(
HWND hwnd, /*視窗句柄*/
UINT uMsg, /*資訊標識*/
WPARAM wParam,
LPARAM lParam
)
{
switch (uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf_s(szChar, "char code is %d", wParam);
MessageBox(hwnd, szChar, TEXT("char"), 0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked", "message", 0);
HDC hdc;
hdc = GetDC(hwnd); /*不能再相應WM_PAINT 訊息時呼叫*/
TextOut(hdc, 0, 50, TEXT("LOL HOME"), strlen("LOL HOME"));
/*ReleaseDC(hwnd,hdc);*/
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc = BeginPaint(hwnd, &ps); /* BeginPaint 只能在回應 WM_PAINT 訊息時呼叫*/
TextOut(hDc, 0, 0, "http://www.csdn.com", strlen("http://www.csdn.com"));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if (IDYES == MessageBox(hwnd, "Is End?", "message", MB_YESNO))
{
DestroyWindow(hwnd);
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
CreateWindow回傳NULL, GetLastError 是1407. 是我類填漏了還是填錯了?
uj5u.com熱心網友回復:
wndcle.lpszClassName = TEXT("lpl2018");
hwnd = CreateWindow(TEXT("LCK2018"), TEXT("http://www.baidu.com"), WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);你注冊的類名 要和創建的類名一致
uj5u.com熱心網友回復:
"lpl2018" 注冊名"LCK2018" 創建名
不一致 !
uj5u.com熱心網友回復:
1047錯誤碼對應的錯誤資訊是: "找不到視窗類。"所以 1樓2樓說的, 就是你的問題所在.
MSDN中, 也有詳細描述
HWND CreateWindow( LPCTSTR lpClassName,
LPCTSTR lpWindowName,
DWORD dwStyle,
int x,
int y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam
);
Parameters
lpClassName
[in] Pointer to a null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero. If lpClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names. For a list of system class names, see the Remarks section.
uj5u.com熱心網友回復:
hwnd = CreateWindow(TEXT("LCK2018"),。。。創建視窗的類名和注冊的 視窗類類名不一樣,你創建的視窗應該是回傳失敗,hWnd應該是0轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/60999.html
標籤:界面
上一篇:求批量改檔案名的程式(二)
