• 0

[C++] how to code for windows


Question

Hello,

I am able to code in general ( it is what i go to school for) but all of my classes are lacking an essential part. How to interact with the operating system. I had a lecture once on shell scripting ( which i would love a few tutorials if possible) but it was for linux. I want to learn how to write for a windows shell. I've can use 'dos' lang well enough, but i am interested in writing programs that can interact with the OS. any links, tutorials, ect. would be greatly appreciated THANK YOU!

Link to comment
https://www.neowin.net/forum/topic/451395-c-how-to-code-for-windows/
Share on other sites

18 answers to this question

Recommended Posts

  • 0

Best bet is to download the platform sdk and read some of the docs. For general api, you should look at Win32 api. For GUI's in C++, look at MFC, Vista includes a ton of new api's, but you'll need to download the windows SDK (pre-release; should replace platform sdk eventually).

  • 0

If you mean how to create a window, this is my class to do it (taken from my game engine, but will still work for any type of app). Sorry about no commenting etc. haven't got round to it.

window.h:

#ifndef _RENDER_WINDOW_H_
#define _RENDER_WINDOW_H_

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <string>

const std::string c_sClassName = "renderwindow";

class CRenderWindow
{
public:



	CRenderWindow( int nWidth, int nHeight, std::string sTitle, HINSTANCE hInstance );
	~CRenderWindow( );

	bool Create( );
	void Destroy( );

	void Show( );

	static LRESULT CALLBACK MessageRouter( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
	LRESULT WindowProcedure( UINT uMsg, WPARAM wParam, LPARAM lParam );

private:

protected:

	HWND	m_hWnd;
	HINSTANCE m_hInstance;
	bool	m_bReady;

	int		m_nWidth;
	int		m_nHeight;
	std::string m_sTitle;

};

#endif

window.cpp:

#include "renderwindow.h"

CRenderWindow::CRenderWindow( int nWidth, int nHeight, std::string sTitle, HINSTANCE hInstance )
{
	this->m_nWidth = nWidth;
	this->m_nHeight = nHeight;
	this->m_sTitle = sTitle;

	this->m_hWnd = NULL;
	this->m_hInstance = hInstance;
	this->m_bReady = false;

	this->Create( );
}

CRenderWindow::~CRenderWindow( )
{
	this->Destroy( );
}

bool CRenderWindow::Create( )
{
	WNDCLASSEX	wcClass;

	ZeroMemory( &wcClass, sizeof( wcClass ) );

	wcClass.cbSize = sizeof( wcClass );
	wcClass.cbClsExtra = 0;
	wcClass.cbWndExtra = 0;

	wcClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wcClass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
	wcClass.hCursor = LoadCursor( NULL, IDC_ARROW );
	wcClass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );

	wcClass.hInstance = this->m_hInstance;
	wcClass.lpszClassName = c_sClassName.c_str();
	wcClass.lpfnWndProc = MessageRouter;
	wcClass.lpszMenuName = NULL;
	wcClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

	if( !RegisterClassEx( &wcClass ) )
	{
		return false;
	}

	this->m_hWnd = CreateWindowEx( WS_EX_APPWINDOW,
								   c_sClassName.c_str(),
								   this->m_sTitle.c_str(),
								   WS_OVERLAPPEDWINDOW,
								   0,
								   0,
								   this->m_nWidth,
								   this->m_nHeight,
								   NULL,
								   NULL,
								   this->m_hInstance,
								   this );

	if( this->m_hWnd == NULL )
	{
		return false;
	}

	this->m_bReady = true;

	return true;
}

void CRenderWindow::Destroy()
{
	if( this->m_hWnd != NULL )
	{
		DestroyWindow( this->m_hWnd );
		this->m_hWnd = NULL;
	}

	UnregisterClass( c_sClassName.c_str(), this->m_hInstance );

	this->m_hInstance = NULL;
}

void CRenderWindow::Show( )
{
	ShowWindow( this->m_hWnd, SW_SHOW );
	UpdateWindow( this->m_hWnd );
}

LRESULT CALLBACK CRenderWindow::MessageRouter( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	CRenderWindow*	pWindow;

	if( uMsg == WM_NCCREATE )
	{
		pWindow = (CRenderWindow*)((LPCREATESTRUCT)lParam)->lpCreateParams;
		SetWindowLongPtr( hWnd, GWL_USERDATA, (LONG_PTR) pWindow );
		return TRUE;
	}
	else
	{
		pWindow = (CRenderWindow*) GetWindowLongPtr( hWnd, GWL_USERDATA );
		if( pWindow == NULL )
		{
			return DefWindowProc( hWnd, uMsg, wParam, lParam );
		}
	}

	pWindow->m_hWnd = hWnd;
	return pWindow->WindowProcedure( uMsg, wParam, lParam );
}

LRESULT CRenderWindow::WindowProcedure( UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch( uMsg )
	{
	case WM_DESTROY:
		PostQuitMessage( 0 );
		break;

	default:
		return DefWindowProc( this->m_hWnd, uMsg, wParam, lParam );
		break;
	}
}

winmain.cpp:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include "window.h"

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	MSG msg;

	CRenderWindow win( 800, 600, "Hello", hInstance );
	win.Show( );

	bool b_go = true;

	while( b_go )
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			switch( msg.message )
			{
			case WM_QUIT:
				b_go = false;
				break;

			default:
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
		}
	}

	return 0;
}

  • 0

The Win32 API (now the Platform SDK :( ) sux0rz. It's too much code. Nevertheless, it is important to Windows programmers. Note that if you created an installer package, you could install SDL and use that to initialize a window with a lot less code. Implementing message handlers with SDL can be troublesome sometimes when compared to the skeleton above.

  • 0
  Quote
GUI's in C++, look at MFC, Vista

Bad :p

Use WTL, a light-weight template based lib like ATL:

// CControlDlg
class CNewFeedDlg : 
	public CDialogImpl<CNewFeedDlg>,
		 public CDialogResize<CNewFeedDlg>,
	public CMessageFilter,
	public IObserver
{
   public:
	CNewFeedDlg();
	~CNewFeedDlg();

	enum { IDD = IDD_NEWFEED };

	BEGIN_TOOLTIP_MAP()
		//DEF_TOOLTIP(IDC_ICONSLIDER, IDS_TOOLTIP_ICONSLIDER)
	END_TOOLTIP_MAP()

BEGIN_MSG_MAP(CNewFeedDlg)

	// dialog stuff
	MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
	MESSAGE_HANDLER(WM_CLOSE, OnClose)
	MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
	COMMAND_ID_HANDLER(IDOK, OnOkButton)
	COMMAND_ID_HANDLER(IDCANCEL, OnCancelButton)

//	CHAIN_MSG_MAP(CDialogImpl<CNewFeedDlg>)
	REFLECT_NOTIFICATIONS()
END_MSG_MAP()

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT OnOkButton(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
	LRESULT OnCancelButton(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
}

Edited by AndreasV
  • 0

It's not using MFC. It's a light-weight template-based library like ATL; this means your objects don't even have to use at-runtime polymorphy, since it's done using at-compile-time polymorphy. WTL "thunks" the winproc to be able to associate a window with an object.

It was meant as a replacement for MFC which fitted more with ATL done by MS; however MS has dropped official support for it (they are going .NET all the way), so it's now a pretty well supported project somewhere on sourceforge.

Only bad thing is that there's little documentation around, however, there are some truelly great "components" (just sub-classed windows) out there for WTL on www.viksoe.dk .

  • 0

I also just recently began programming for windows (some months ago), and i agree with Andreas - for any project you want to take on in the windows environment, using c++, WTL is one of the best ways to go. MFC is just to heavy and brings to much "unnecessary" [s?] code to the table (or at least i thought so).

BUT - i think it would be much easier for you to understand both MFC and WTL (and whatever else you do with windows) if you first mess around with writing some winAPI code (everything else (ATL/WTL/MFC) is basically template based layering for the winAPI). I`d reccommend Charles Petzold`s book Programming Windows - which for me, made all the difference, it helped me UNDERSTAND windows - and if you understand OOP - you`ll be able to easily catch on any template based library. It is written in a clear and precise manner and has loads of usefull examples.

anyways, just my 5c - good luck with it ;)

  • 0

@Brandon: Because ATL isn't a full UI API. And WTL also use ATL; one doesn't exclude another.

We develop in MFC at work so that's what I'm used to most, but I know enough WTL to realize it's a very slick API. Just too bad about the lack of documentation (and possibly community size if you get problems), really. For these reasons, it can be a bit rough API for newbies, but still much easier than raw Win32 development, while preserving lightness of code. WTL apps don't have any MFC library dependencies and still become very small.

Anyway, among the best tutorials for it that I've seen so far is on CodeProject, and it's aimed especially for those who know a bit of MFC:

- WTL for MFC Programmers, Part I - ATL GUI Classes

- WTL for MFC Programmers, Part II - WTL GUI Base Classes

- WTL for MFC Programmers, Part III - Toolbars and Status Bars

- WTL for MFC Programmers, Part IV - Dialogs and Controls

- WTL for MFC Programmers, Part V - Advanced Dialog UI Classes

- WTL for MFC Programmers, Part VI - Hosting ActiveX Controls

- WTL for MFC Programmers, Part VII - Splitter Windows

- WTL for MFC Programmers, Part VIII - Property Sheets and Wizards

- WTL for MFC Programmers, Part IX - GDI Classes, Common Dialogs, and Utility Classes

They have an entire WTL section too, with freely downloadable code. Also, for users who want a good free IDE and develop with WTL, Using WTL with Visual C++ 2005 Express Edition is another useful article there. (that article *only* applies to the Express Edition due to its limited functionality; other editions are much more easily integrated after installing WTL)

Here's the WTL Home Page btw.

Edited by Jugalator
  • 0

If you're interested in gui(and more) you should use a wrapper for Win32 API. It's good to know that too but there are like 600+ function (pure C style :) )...

Try wxWidgets ... has 12 years of development... it's better than mfc (at least for me :) ) it's free, it's portable (you can run the programs made with it in win, linux and mac and uses the native controls on each platform) and has a lot of packages (it's not limited to gui)...

hope it helps

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • PlayStation's first wireless fight stick faces long wait, launching in 2026 by Paul Hill Sony has unveiled the FlexStrike wireless fight stick previously known as Project Defiant. Its unveiling marks the PlayStation’s first-ever proprietary fight stick controller. Unfortunately for anyone hoping to get their hands on it soon, Sony won’t be releasing it until 2026, creating a significant wait for eager fighting game fans. Though its release is still some time away, a display unit will be shown off at Evo 2025 (August 1-3). Sony notes that this displayed version of the FlexStrike may not represent the final product. As a bit of background, PlayStation gamers can already get third-party fight sticks, this marks Sony's first official entry into the market. The FlexStrike will be compatible with PS5 and PC via wired connection or wirelessly with PlayStation Link. It features mechanical switch buttons and toolless swappable restrictor gates (square, circle, octagon) with built-in storage. You can also use simultaneous PlayStation Link audio for connecting FlexStrike and a Pulse headset/earbuds on PS5 with a single USB adapter. Sony also mentions that there will be support for two FlexStrike sticks via a single PS Link USB adapter for local multiplayer and includes all DualSense Controller inputs (except directional pad via lever and mute button), plus a touchpad. The controls are adjustable with a built-in level mode switch and a lock button to prevent accidental inputs. So that it’s comfortable to use, Sony has opted for an ergonomic design with angled surfaces and a non-slip base. While we know from Sony that the FlexStrike wireless fight stick is confirmed for a 2026 release, we do not know which day or month to expect it on. Such a late entry into this market means that Sony will have already lost customers who have already invested in fight sticks from other manufacturers. For more information, keep your eyes peeled to Evo 2025 to see what Sony reveals. The company also recommends signing up for news updates on its official FlexStrike website.
    • LTSC has the same TPM requirement as other Windows 11 versions. Business SKUs, which obviously include LTSC, don't get Windows Recall. Recall can be removed for good quite easily if you really don't trust Microsoft. Not that it affects you anyway. Many reasons to switch to Linux, just not what you've called out!
    • ISTM the bigger the number gap in an update the greater number of users that are impacted or benefit from the update. I have never found a two decimal place software update to be beneficial to the majority of the software users.
    • He says something one day and the next day he changes it or claims he never said it.
  • Recent Achievements

    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
    • One Month Later
      James_kobe earned a badge
      One Month Later
    • Week One Done
      James_kobe earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      653
    2. 2
      ATLien_0
      253
    3. 3
      Xenon
      168
    4. 4
      neufuse
      147
    5. 5
      +FloatingFatMan
      124
  • Tell a friend

    Love Neowin? Tell a friend!