C++ : mixing Directx9 and default windows - c++

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.

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);
}
}

C++ win32 GUI code in classes

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).

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.

DirectX and C++

I am having trouble with the following CPP code: http://ideone.com/XZXZJ .
I am receiving the following errors:
(1) Warning 1 warning LNK4042: object specified more than once; extras ignored c:\Users\jabbott\documents\visual studio 2010\Projects\DirectX9_Tutorial\DirectX9_Tutorial\Debug\WinMain.obj 1
(2) Error 2 error LNK1561: entry point must be defined c:\Users\jabbott\documents\visual studio 2010\Projects\DirectX9_Tutorial\DirectX9_Tutorial\LINK
No Entry Point setting isn't set. I've tried to set this to WinMain with no luck.
// WinMain.h
#ifndef APP_HPP_
#define APP_HPP_
#include <Windows.h> // Windows API Library
#include <d3d9.h> // Include DirectX 9 Library
#include <d3d10_1.h> // Include DirectX 10 Library
#include <d3d11.h> // Include DirectX 11 Library
/* Global Functions */
// Handle any messages from MS Windows
LRESULT CALLBACK WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
// Main Entry Point
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd);
/* Global Variables */
IDirect3D9 *g_pD3D; // DirectX 3D v9 Instance
IDirect3DDevice9 *g_pD3DDevice; // DX3D9: Representation of Graphics Card
LPCTSTR ClsName = "g_szInterfaceClass";
//IDirect3D10 *g_pD3D10; // DirectX 3D v10 Instance
//IDirect3DDevice10 *g_pD3DDevice10; // DX3D10: Representation of Graphics Card
//IDirect3D11 *g_pD3D11; // DirectX 3D v11 Instance
//IDirect3DDevice11 *g_pD3DDevice11; // DX3D11: Representation of Graphics Card
#define WINDOW_WIDTH 200
#define WINDOW_HEIGHT 200
/* Error Macro for Debugging */
//#define ERROR(msg) { MessageBox(NULL, msg, L"Error", MB_OK|MB_ICONEXCLAMATION); }
#endif // WINMAIN_H
WinMain.cpp:
// WinMain.cpp
#include "WinMain.h"
LRESULT CALLBACK WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd,
uMsg,
wParam,
lParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* Predeclaration of WinProc() */
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
// The WNDCLASSEX structure contains window class information
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) 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)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = ClsName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// rect structure to define the window dimensions
RECT rect;
rect.top = (long) 0;
rect.left = (long) 0;
rect.right = (long) WINDOW_WIDTH;
rect.bottom = (long) WINDOW_HEIGHT;
// Register the window to MS Windows
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// calculate the required size of the window rectangle, based on the desired size of the client rectangle
AdjustWindowRectEx( &rect,
WS_OVERLAPPEDWINDOW,
FALSE,
WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
// create the target window of the application
HWND hWindow = CreateWindowEx(NULL,
ClsName,
"Name of Window",
WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0,
rect.right-rect.left,
rect.bottom-rect.top,
NULL, NULL,
hInstance,
NULL);
if (!hWindow)
{
DestroyWindow(hWindow);
UnregisterClass(ClsName, hInstance);
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hWindow, SW_SHOW);
UpdateWindow(hWindow);
SetForegroundWindow(hWindow);
SetFocus(hWindow);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.hDeviceWindow = hWindow;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = WINDOW_WIDTH;
d3dpp.BackBufferHeight = WINDOW_HEIGHT;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
// Identify current version of Direct3D
g_pD3D =Direct3DCreate9(D3D_SDK_VERSION);
// Create the Direct3D Interface Device. Thisisanabstracted version of
if(FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&g_pD3DDevice)))
{
MessageBox(NULL, "Failed to create Direct3D InterfaceDevice.", "Error!", MB_ICONEXCLAMATION | MB_OK);
}
/* Main Game Boot */
// Enter the message loop
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
// Clear the target render buffer black
g_pD3DDevice->Clear( 0,
0,
D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0, 0, 0),
1.0f,
(DWORD) 0.0f);
// Send the buffer to the computer monitor
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
//Send any messages to WndProc function
TranslateMessage(&msg);
DispatchMessage(&msg);
}
g_pD3D->Release();
return 1;
}
Your first problem can be solved by deleting this line:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* Predeclaration of WinProc() */
You already have the implementation of the function directly above it. You only need the above if the implementation of the function occurs after the point the function is called or the implementation is in a different translation unit (source file). Generally, try to avoid the 'predeclaration' of functions if you can, use the function itself as the declaration, it will reduce the amount of maintenance required.
The second problem might be because you are compiling a console application (entry point main) rather than a windows application (entry point WinMain).

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.