kidkid đã đưa cho tớ mã nguồn. Test ra tổng cộng 3 lỗi. Tớ sẽ bôi đậm những chỗ cậu bị lỗi nghen
Mã:
// 3.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
LRESULT CALLBACK winproc(HWND, UINT, WPARAM, LPARAM);
BOOL InitApplication (HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
char AppName[]="HelloPgm";
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int cmdShow)
{
MSG msg;
if(!InitApplication(hInstance)) return FALSE;
if(!InitInstance(hInstance, cmdShow)) return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = winproc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH); // Cái này cậu thiếu (HBRUSH)
wndclass.lpszClassName = AppName;
wndclass.lpszMenuName = NULL;
return RegisterClass(&wndclass);
}
BOOL InitInstane(HINSTANCE hInstance, int cmdShow) // Cái này gõ nhầm Instance thành Instane
{
HWND hWnd;
hWnd = CreateWindow(AppName,
"Hello World",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd) return FALSE;
ShowWindow(hWnd,cmdShow);
UpdateWindow (hWnd);
return TRUE;
}
LRESULT CALLBACK Winproc(HWND hWnd, // Còn chỗ này thì Winproc không được viết hoa chữ W.
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(msg)
{
case WM_PAINT:
hdc= BeginPaint ( hWnd,&ps);
GetClientRect(hWnd,&rect);
DrawText ( hdc, " Hello World ",-1,&rect,DT_SINGLELINE| DT_CENTER | DT_VCENTER);
EndPaint(hWnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,msg,wparam,lparam);
}
Mã nguồn hoàn chỉnh sẽ là :
Mã:
// 3.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
LRESULT CALLBACK winproc(HWND, UINT, WPARAM, LPARAM);
BOOL InitApplication (HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
char AppName[]="HelloPgm";
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int cmdShow)
{
MSG msg;
if(!InitApplication(hInstance)) return FALSE;
if(!InitInstance(hInstance, cmdShow)) return FALSE;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = winproc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = AppName;
wndclass.lpszMenuName = NULL;
return RegisterClass(&wndclass);
}
BOOL InitInstance(HINSTANCE hInstance, int cmdShow)
{
HWND hWnd;
hWnd = CreateWindow(AppName,
"Hello World",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd) return FALSE;
ShowWindow(hWnd,cmdShow);
UpdateWindow (hWnd);
return TRUE;
}
LRESULT CALLBACK winproc(HWND hWnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(msg)
{
case WM_PAINT:
hdc= BeginPaint ( hWnd,&ps);
GetClientRect(hWnd,&rect);
DrawText ( hdc, " Hello World ",-1,&rect,DT_SINGLELINE| DT_CENTER | DT_VCENTER);
EndPaint(hWnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,msg,wparam,lparam);
}
--May mà cậu send mã nguồn cho. Không thì xem mỗi đoạn đầu ở trên kia thì đến tết Tây cũng ko tìm ra lỗi.
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...