(Help) Error in function 'WinMain' in Eclipse CDT - c++

I was trying coding a simple Win32 GUI Application using Eclipse CDT (Eclipse used in C++) but it throws an error which says:
conflicting declaration of C function 'int WinMain(HINSTANCE, int, LPSTR, int)
My code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {
WNDCLASSEX cWindow;
HWND hWnd;
MSG Msg;
cWindow.cbSize = sizeof(WNDCLASSEX);
cWindow.lpszClassName = "cls";
cWindow.lpfnWndProc = WndProc;
cWindow.lpszMenuName = NULL;
cWindow.hInstance = hInstance;
cWindow.style = CS_OWNDC;
cWindow.hIcon = LoadIcon(NULL, IDI_APPLICATION);
cWindow.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
cWindow.hCursor = LoadCursor(NULL, IDC_ARROW);
cWindow.hbrBackground = (HBRUSH)3;
cWindow.cbClsExtra = 0;
cWindow.cbWndExtra = 0;
if(!RegisterClassEx(&cWindow))
MessageBox(NULL, "Error registring window class!", "Fatal error", MB_OK | MB_ICONERROR);
hWnd = CreateWindowEx(0, "cls", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
640, 640, HWND_DESKTOP, NULL, hInstance, NULL);
while(GetMessage(&Msg, NULL, NULL, NULL)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
DestroyWindow(hWnd);
return 0;
}
It is on C++ language, and I'm using MinGW compiler

Related

How to make console process not open?

So, I'm trying to learn how to make C/C++ Windows Desktop Applications, but I have come across a great issue, when creating a completely normal window, the cmd where the console application would run appears. Is there any way to make so that it doesn't even open? I've been seeing some methods, but they appear to hide the console, but the process is still there.Here is my code:
#define UNICODE
#define _UNICODE
#include <Windows.h>
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR args, int nCmdShow) {
WNDCLASSW wc = { 0 };
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInstance;
wc.lpszClassName = L"windowClass";
wc.lpfnWndProc = WinProc;
if (!RegisterClass(&wc))
return 1;
HWND hWnd = CreateWindow(L"windowClass", L"Window 1",
WS_OVERLAPPEDWINDOW,
100, 100,
256, 256,
NULL, NULL, NULL, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg = { 0 };
while (GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
DefWindowProc(hWnd, msg, wParam, lParam);
}
}

How to prevent some action in app using winapi?

I would like to prevent some action in my app using winapi for example I would like to block moving my window app. How can I do this?
I try this:
if( pWindowsMessage->message == WM_MOVING )
{
return 1;
}
else if(pWindowsMessage->message == WM_MOVE)
{
return 1;
}
But it doesn't work.
Other example: How to prevent close the window?
if( pWindowsMessage->message == WM_CLOSE )
{
return 1;
}
It works. But is it a good solution?
Of course the first and the second example are in the function which get messages.
You can block messages in different areas by handling WM_NCLBUTTONDOWN messages.
And According to WM_NCHITTEST,we can handle events in different regions.
Here is a sample:
#include <Windows.h>
#include <cassert>
#include <cstdlib>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
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.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_NCLBUTTONDOWN:
if (wParam == HTCAPTION || (wParam >= HTLEFT && wParam < HTBORDER))
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

cannot convert from 'LRESULT (__stdcall *)(HWND,const wchar_t,WPARAM,LPARAM)' to 'WNDPROC'

Error:
I was running through this tutorial on window creation, and have come across the error:
Error 1 error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND,const wchar_t,WPARAM,LPARAM)' to 'WNDPROC'
Question:
What going wrong here? Is this tutorial outdated or something of the sort?
Code:
The code is identical or nearly identical to that of the tutorial.
#include "windows.h"
#include "windowsx.h"
LRESULT CALLBACK WindowProc(HWND hWnd,
const wchar_t message,
WPARAM wParam,
LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//Handle for the window, filled by a function
HWND hWnd;
//This struct holds information for the window class
WNDCLASSEX wc;
//Clear out the window class
ZeroMemory(&wc, sizeof(WNDCLASSEX));
//Fill struct with needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "Window Class";
//Register the window class
RegisterClassEx(&wc);
//Create the window to use as a handle
hWnd = CreateWindowEx( NULL,
"Window Class",
"Our first window",
WS_OVERLAPPEDWINDOW,
300,
300,
500,
400,
NULL,
NULL,
hInstance,
NULL);
ShowWindow( hWnd,
nCmdShow);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//Main message handler
static LRESULT CALLBACK WindowProc(HWND hWnd,
const wchar_t message,
WPARAM wParam,
LPARAM lParam)
{
//Find the code to run for the message
switch(message)
{
case WM_DESTROY:
{
//Close the app entirely
PostQuitMessage(0);
return 0;
} break;
}
//Handle any messages the switch didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}
The declarations const wchar_t message should be replaced with UINT. This error is received because of faulty parameters.

Simple WinAPI app has an additional console window [duplicate]

This question already has answers here:
How to stop a program compiled with MinGW (g++) from opening a console window in windows
(2 answers)
Closed 9 years ago.
I've copy-pasted following skeleton of a simple C++ WinAPI application. It works, but creates an additional console window along with GUI one. How to get rid of it? I am using GCC from MinGW.
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static char sClassName[] = "MyClass";
static HINSTANCE zhInstance = NULL;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
zhInstance = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = zhInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = sClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass)) {
MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_STATICEDGE, sClassName, "db Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
320, 240, NULL, NULL, zhInstance, NULL);
if(hwnd == NULL) {
MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
Just use the -mwindows option to compile a GUI application. The default, -mconsole, creates a console application.
The default heuristic used by the MS linker is: if your program has a main function it is a console program but if it has a WinMain function it is a GUI one. But that is not used by the GNU tools. They create a console program by default.

Window doesn't appear

I have written a small program to create a window. I have made this program before, but now I'm trying to recollect all the things for myself.
When I was done writing the program, the window won't appear, and when I compare my code to the book I'm learning from, its the same. What am I missing/doing wrong?
#include <windows.h>
#include <WindowsX.h>
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
HWND hWnd;
// information for the window class
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass1";
RegisterClassEx(&wc);
// Create Window
hWnd = CreateWindowEx( NULL,
"WindowClass",
"My Program",
WS_OVERLAPPEDWINDOW,
100,
100,
600,
480,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_SHOWDEFAULT);
MSG msg;
while(GetMessage(&msg, NULL, 0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
Compare class names:
wc.lpszClassName = "WindowClass1";
hWnd = CreateWindowEx( NULL, "WindowClass", ...
The best way to find such errors is to check return code of every API.