• 0

Subwindows in C


Question

i finally got a small grip on how to do windows with C :-D

here is what i have so far...

/*	Trim fat from windows*/
#define WIN32_LEAN_AND_MEAN	
#pragma comment(linker, "/subsystem:windows")
/*	Pre-processor directives*/
//#include "stdafx.h"
#include <windows.h>
/*	Windows Procedure Event Handler*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT paintStruct;
	/*	Device Context*/
	HDC hDC; 
	/*	Text for display*/
	char string[] = "Click here to do nothing."; 
	/*	Switch message, condition that is met will execute*/
	switch(message)
	{
  /*	Window is being created*/
  case WM_CREATE: 
  	return 0;
  	break;
  /*	Window is closing*/
  case WM_CLOSE: 
  	PostQuitMessage(0);
  	return 0;
  	break;
  /*	Window needs update*/
  case WM_PAINT: 
  	hDC = BeginPaint(hwnd,&paintStruct);
  	/*	Set txt color to blue*/
  	SetTextColor(hDC, COLORREF(0x01F00000));
  	/*	Display text in middle of window*/
  	TextOut(hDC,125,100,string,sizeof(string)-1);
  	EndPaint(hwnd, &paintStruct);
  	return 0;
  	break;
  default:
  	break;
	}
	return (DefWindowProc(hwnd,message,wParam,lParam));
}
/*	Main function*/
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	WNDCLASSEX  windowClass;  //window class
	HWND  hwnd;    //window handle
	MSG  	msg;    //message
	bool  done;    //flag saying when app is complete
	/*	Fill out the window class structure*/
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.style = CS_HREDRAW | CS_VREDRAW;
	windowClass.lpfnWndProc = WndProc;
	windowClass.cbClsExtra = 0;
	windowClass.cbWndExtra = 0;
	windowClass.hInstance = hInstance;
	windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	windowClass.lpszMenuName = NULL;
	windowClass.lpszClassName = "MyClass";
	windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
	/*	Register window class*/
	if (!RegisterClassEx(&windowClass))
	{
  return 0;
	}
	/*	Class registerd, so now create window*/
	hwnd = CreateWindowEx(NULL,  //extended style
  "MyClass",  	//class name
  "A Real Win App",  //app name
  WS_OVERLAPPEDWINDOW |  //window style
  WS_VISIBLE |
  WS_SYSMENU,
  100,100,  	//x/y coords
  400,250,  	//width,height
  NULL,    //handle to parent
  NULL,    //handle to menu
  hInstance,  	//application instance
  NULL);    //no extra parameter's
	/*	Check if window creation failed*/
	if (!hwnd)
  return 0;
	done = false; //initialize loop condition variable
	/*	main message loop*/
	while(!done)
	{
  PeekMessage(&msg,hwnd,NULL,NULL,PM_REMOVE);
  if (msg.message == WM_QUIT) //check for a quit message
  {
  	done = true; //if found, quit app
  }
  else
  {
  	/*	Translate and dispatch to event queue*/
  	TranslateMessage(&msg); 
  	DispatchMessage(&msg);
  }
	}
	return msg.wParam;
}

from here, i want to know how i can either make the text clickable to do something, or make it like a button so i can click it and make something happen... im pretty sure i have to make another window, but i dont know exactly how to go about it... do i have to register another class? thanks for pre-help :-P

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Add this to your wndproc:

 case WM_LBUTTONUP:
  MessageBox(hwnd, "Hello, world", "Hi!", MB_ICONINFORMATION);
  break;

If you want to add a button, you could do this:

HWND h = CreateWindowEx(0, "BUTTON", "test!", WS_VISIBLE | WS_CHILD, 0, 0, 50, 50, hwnd, (HMENU)1234, NULL, NULL);

Then to your parent wndproc, add:

 case WM_COMMAND:
  if(LOWORD(wParam) == 1234)
  {
  MessageBox(hwnd, "Hello, world, again", "Hi!", MB_ICONINFORMATION);
  }

Check msdn for docs about all the WM_* messages.

Link to comment
Share on other sites

  • 0

alright, i came close, but it keeps crashing when i run it...

i have it so on the button up message,

 case WM_LBUTTONUP:
 MessageBox(hwnd, "Hello, world", "Hi!", MB_ICONINFORMATION);
 break;

instead of a message box, i have it try to createprocess, that being cmd.exe, either alone or running the "shutdown -l" line... either way, it compiles with no errors or warnings, just crashes... any ideas?

Link to comment
Share on other sites

  • 0

just instead of

MessageBox(hwnd, "Hello, world", "Hi!", MB_ICONINFORMATION);

i have CreateProcess... im pretty sure thats how i did it, as im not home right now

Link to comment
Share on other sites

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

    • No registered users viewing this page.