C/C++ Windows API

Moderators: Lundin
Number of threads: 450
Number of posts: 1225

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
WIN API simple error Posted by newtron on 16 Feb 2010 at 4:44 AM
hello everybody! I am beginer in WIN API so i need some help :S

this program has some errors and can someone fix it?
I will appreciate it if you help me


#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCMDLine, int nCmdShow)

{
HWND hMainWnd;
char szClassName[] = "MyClass";
MSG msg;
WNDCLASSEX wc;


wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wc)) {
MessageBox(NULL, NULL, NULL, MB_OK);
return 0;
}

hMainWnd = CreateWindow(
szClassName, NULL, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
(HWND)NULL, (HMENU)NULL,
(HINSTANCE)hInstance, NULL
);

if (IhMainWnd) {
MessageBox(NULL, NULL, NULL, MB_OK);
return 0;
}

ShowWindow(hMainWnd, nCmdShow);

while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
RECT rect;
switch (uMsg)
{
case WM_PAINT:
hDC = BeginPain(hWnd, &ps);
GetClientRect(hWnd, &rect);
DrawText(hDC, "Hello, World!", -1, &rect,
DT_SINGLELINE | DT_CENTER | DT VCENTER);
EndPaint(hWnd, &ps);
break;

case WM_CLOSE:
DestroyWindow(hWnd);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}


Report
Re: WIN API simple error Posted by anthrax11 on 16 Feb 2010 at 7:01 AM
The parameters for CreateWindow are: x, y, width, height. You can only use CW_USEDEFAULT for x and y, but not for the width and height. The dimensions can't be 0 either. So use something like CW_USEDEFAULT, CW_USEDEFAULT, 200, 300.

The rest are just typos, which I'm sure you can find with the help of your compiler.
Report
Re: WIN API simple error Posted by newtron on 18 Feb 2010 at 4:50 AM
tnks but it's still an error
.\main.cpp(24) : error C2440: '=' : cannot convert from 'char [8]' to 'LPCWSTR'

Report
Re: WIN API simple error Posted by AsmGuru62 on 18 Feb 2010 at 5:14 AM
Instead of char type use WCHAR type and all text constants then should be prefixed with L:
WCHAR s [] = L"some text";
Report
Re: WIN API simple error Posted by newtron on 19 Feb 2010 at 10:55 AM
tnks, that worked



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.