I made a simple win32 application. I placed my GUI code in a class, but I got some bugs.
Main.cpp
#include "Form.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
Form form(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
form.show();
form.set_title("Testing this stuff");
}
Form.h
#pragma once
#include <Windows.h>
class Form
{
private:
HWND hwnd;
char* title = "Form";
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
public:
Form(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
void set_title(const char* title);
void show();
~Form();
};
Form.cpp
#include "Form.h"
LRESULT CALLBACK Form::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_KEYDOWN:
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
Form::Form(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = &WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);// LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "test_class";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
if (RegisterClassEx(&wc))
{
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "test_class", title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, NULL, NULL);
if (!hwnd == NULL)
{
ShowWindow(this->hwnd, SW_NORMAL);
UpdateWindow(this->hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
void Form::show() {
ShowWindow(this->hwnd, SW_NORMAL);
UpdateWindow(this->hwnd);
}
void Form::set_title(const char* title) {
SetWindowText(this->hwnd, title);
}
Form::~Form()
{
}
Now, what I wanted it to do is show the window and change the title; however, the GUI doesn't show up and the set_title doesn't work either. I also tried for the set_title:
SendMessage(this->hwnd, WM_SETTEXT, 0, (LPARAM)title);
This doesn't work either. Also, this->hwnd is a variable I made in my class. I'm uncertain what's wrong and how to fix it. I want to make a similar Form system like Windows Form Application from .NET but without using any framework (only win32).
Related
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);
}
}
I don't manage to display the label ( static control ) at the same time that the parent windows appears.
The label appears only in the main window if I move the window with the mouse.
Can you help me ?
This is the code :
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM keyPressed, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
addControls(hwnd);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_PAINT:
{
return 0;
}
default:
return DefWindowProc(hwnd, uMsg, keyPressed, lParam);
}
return 0;
}
void addControls(HWND hwnd)
{
hwndLabel = CreateWindowW(L"static", L"Choose a resolution:", WS_VISIBLE|WS_CHILD|SS_LEFT,10, 40, 250, 200,hwnd, NULL, NULL, NULL);
}
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
//HWND hwndParamWindow;
MSG msg;
WNDCLASS wc;
RECT rc;
GetWindowRect(GetDesktopWindow(), &rc);
HANDLE processHandle = GetCurrentProcess();
wc.style = 0;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
wc.lpszMenuName = NULL;
wc.lpszClassName = (LPCSTR)"WinClass";
if(!RegisterClass(&wc))
return FALSE;
hwnd = CreateWindowW(L"WinClass", L"title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 200, 200, 900, 900, NULL, NULL, NULL, NULL);
I am looking for a method of placing a DXLinked window within a windowproc as a child window. This would allow for menus/Forms controls to be externally managed before input to the DX methods.
So far I have attempted to create two WNDCLASSEX and two HWND types, and ran both windowprocs in the main loop. This unfortunately doesn't seem to work.
A screen shot example of what I wish to accomplish.
http://clip2net.com/s/3jXUG2l - a well known modding tool that is used for Lua and C++ code attachments for objects.
Appreciate any feedback.
Relevant code I am currently using as a prototype:
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#pragma comment (lib, "d3d9.lib")
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
void init_graphics(void); // 3D declarations
struct CUSTOMVERTEX { FLOAT X, Y, Z, RHW; DWORD COLOR; };
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
//Main
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
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 = L"WindowClass";
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"DirectX Test",
WS_OVERLAPPEDWINDOW,
300, 300,
800, 600,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, nCmdShow);
initD3D(hWnd);
MSG msg;
while (TRUE)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (msg.message == WM_QUIT)
break;
render_frame();
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
// t
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);
}
Sorry for the confusion, I made some incorrect assumptions.
Appreciate the feedback however.
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
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.