How to change text size in basic text window win32 c++ - winapi

I am making a very basic tic tac toe game as my first ever attempt at anything windows. I have only a small amount of basic c++ experience as well. At the top of my program i want it to display in a large font "WELCOME TO TIC-TAC-TOE!", and then right underneath it in a much smaller font something like "DEVELOPED BY ....." or something. This is the code i wrote making the text window:
CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("STATIC"),TEXT("WELCOME TO TIC-TAC-TOE!"), WS_CHILD|WS_VISIBLE|SS_CENTER, 20,20,210,20,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
Is there a way to make the text font for "WELCOME TO TIC-TAC-TOE!" bigger? Thanks!

The following code worked if any are interested, thanks to ScottMcP-MVP for pointing me to the right website:
hwnda = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("STATIC"),TEXT("WELCOME TO TIC-TAC-TOE!"), WS_CHILD|WS_VISIBLE|SS_CENTER, 20,20,210,20,hWnd,HMENU(NULL),GetModuleHandle(NULL),NULL);
hFont=CreateFont (20, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");
SendMessage (hwnda, WM_SETFONT, WPARAM (hFont), TRUE);

I hope you save the HWND returned by CreateWindowEx: You will need it. After you create the STATIC window send it the WM_SETFONT message, as outlined here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632642(v=vs.85).aspx
Note especially the remarks on that page about font lifetime.

Related

I want to display numbers on the system tray notification Icons on windows

I am trying to create a notification Icon based application in which I want to display some numbers ranging from 1-999.
I looked at this video which is similar to what I want to do but here the system tray icon just displays the icon and it shows a pop up rather than the system tray icon showing the number or any text.
Excluding the popup item, all I want to do is to read a number (input from somewhere) and display that number in the notification icon section.
I am open to trying any technology (QT, .net) for doing this. Basically, I am looking for some examples.
While parts of your question are vague, this is very possible, I'd even dare-say quite simple. Since you mentioned you're open to trying any technology, C# would probably simplify things for you.
Generate a new 16 x 16 Bitmap and draw the number to it using the Graphics class.
Convert the Image instance to an Icon instance, after disposing of your Graphics object.
Set the Icon property of your NotifyIcon to the icon you've just created.
These are the basic steps. You'll likely need to do some research if you aren't familiar with the classes used.
Thanks for replying to my question. Here is what I came up with. Not sure if this is what you were talking about.
Bitmap bmp = new Bitmap(WindowsFormsApplication2.Properties.Resources._16by16BitmapIcon);
RectangleF rectf = new RectangleF(2, 2, 16, 16);
Graphics g = Graphics.FromImage(bmp);
g.DrawString("99", new Font("Tahoma", 7), Brushes.Blue, rectf);
pictureBox1.Image = bmp;
pictureBox1.Height = bmp.Height;
pictureBox1.Width = bmp.Width;
g.Dispose();
var thumb = (Bitmap)bmp.GetThumbnailImage(64, 64, null, IntPtr.Zero);
thumb.MakeTransparent();
notifyIcon1.Icon = Icon.FromHandle(thumb.GetHicon());
Now my next question could this be done in a better way? This is my first C Sharp app so any suggestions are welcome!
public void ShowText(string text, Font font, Color col)
{
Brush brush = new SolidBrush(col);
// Create a bitmap and draw text on it
Bitmap bitmap = new Bitmap(16, 16);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString(text, font, brush, 0, 0);
// Convert the bitmap with text to an Icon
Icon icon = Icon.FromHandle(bitmap.GetHicon());
m_notifyIcon.Icon = icon;
}

WINAPI LISTBOX scroll down

I'm creating my own program in WINAPI. I've got one problem.
I created a listbox with WS_VSCROLL style.
Everything is ok, but if I fill the listbox with strings the last strings are "invisible".
I must to scroll down list and then I can see the strings.
How do I automatically scroll down the listbox, using the mouse, to show the last string?
You know, I wrote chatBox. Every chatbox, which I know, has got this function:
hListaChatu = CreateWindowEx( WS_EX_DLGMODALFRAME, "LISTBOX", NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL, rect_okna_glownego.right - 320,
rect_okna_glownego.top, 300, rect_edit_text.top - 25 , hwnd, NULL,
hInstance, NULL );

webcam, Video Source dialog comes up

So have seen a ton of people with the same issue's but no answers. My SendMessage pop's up the "Video Source" dialog box on a windows 7 laptop with one camera. I have Cyber Link YouCam installed.
If I uninstall Cyber Link YouCam the "Video Source" dialog goes away. It appears to be the OS is acting as though YouCam is a second driver for the same device. My question is how do I stop the dialog box for "Video Source" from appearing? Once I choose an option in the dialog I don't get the dialog box (until I restart the application) so there must be a setting somewhere being set.
Though debugging found this bit of code (see whole section below) is the one that seems to be the one that calls the dialog box - SendMessage (deviceHandle, WM_CAP_CONNECT, deviceno, ((IntPtr) 0).ToInt32 ())
public void StartWebCam (int height, int width, int handleofthepicturebox, int deviceno)
{
string deviceIndex = "" + deviceno;
deviceHandle = capCreateCaptureWindowA (ref deviceIndex, WS_VISIBLE | WS_CHILD, 0, 0, width, height, handleofthepicturebox, 0);
if (SendMessage (deviceHandle, WM_CAP_CONNECT, deviceno, ((IntPtr) 0).ToInt32 ()) > 0)
{
SendMessage (deviceHandle, WM_CAP_SET_SCALE, -1, 0);
SendMessage (deviceHandle, WM_CAP_SET_PREVIEWRATE, 0x42, 0);
SendMessage (deviceHandle, WM_CAP_SET_PREVIEW, -1, 0);
}
else
{
// nothing failed maybe some clean up needed
}
}
So again the question is - how do I stop the dialog box for "Video Source" from appearing?
I developed a form to display two live captures with avicap32 on Windows XP. There is a chance the fix I found in my case also applies to yours.
In the case of avicap, it is possible to programmatically manage your webcams, and all that is registry-based. Here is the thread I found to guide me and achieve what I wanted.
In this thread, the role of the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\MediaResources\msvideo\MSVideo.VFWWDM\DevicePath registry key is highlighted for the selection of the active webcam.
And here is maybe a helpful piece of info: I had to set the value in lowercase to avoid the Video Source dialog popping up.
I determined it by monitoring the registry key before and after the call to SendMessage with WM_CAP_CONNECT.
To the best of my knowledge (and googling), nobody else has offered a fix, so even though I might be answering a little late, it would be great if you could tell us whether it worked for you as well.
Finally I Found a solution for this.
The problem happens in Windows 7 / 8
First you need this API function
Private Declare Function GetTickCount Lib "kernel32" () As Long
Then... after you call capCreateCaptureWindowA() you have to wait 1 second processing events, (note: sleep don't work the same)
IniTime = GetTickCount()
While GetTickCount() < (IniTime + 1000)
DoEvents
Wend
then you call WM_CAP_DRIVER_CONNECT (maybe a couple of times).. and THAT's IT ... no more video source dialog
With this solution it works perfect. The GetTickCount() waiting for events worked along with calling the function until it returned true.
Private Sub PreviewVideo(ByVal pbCtrl As PictureBox)
hWnd = capCreateCaptureWindowA(VideoSource, WS_VISIBLE Or WS_CHILD, 0, 0, 0,
0, pbCtrl.Handle.ToInt64, 0)
Dim IniTime As Long = GetTickCount()
While GetTickCount() < (IniTime + 1000)
Application.DoEvents()
End While
Dim OKAnswer As Boolean = False
For xretries As Integer = 1 To 10
' I'll give you Only 10 tries to connect, otherwise I AM LEAVING MICROSOFT!
OKAnswer = SendMessage(hWnd, WM_CAP_DRIVER_CONNECT, VideoSource, 0)
If OKAnswer Then
Exit For
End If
Next
If okanswer Then
SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0)
SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, pbCtrl.Width, pbCtrl.Height, SWP_NOMOVE Or SWP_NOZORDER)
Else
DestroyWindow(hWnd)
End If
End Sub
I had the same issue. Make sure you call capDriverDisconnect once you no longer need the webcam. Note that I'm using Logitech C110, but I suppose it should work in your case too.
Came over here searching for a solution for the "WM_CAP_DRIVER_CONNECT" and "Video Setup" dialog popup Error.
FOUND the solution:
It's a driver problem. Use the OEM's driver, because the Generic Microsoft Drivers are NOT working for your camera.
Use www.astra32.com to find out the USB camera's device info: Manufacturer or Model; then google for the correct drivers.
Good Luck!

How to add a custom button to windows' minimize/maximize/close (x)

I was wondering if there is a way to add (programatically, of course) an icon/button/whatever besides plain text to a window (Microsoft Windows window...)'s title bar or next to where the minimize/maximize/close buttons are. I could draw it myself and create an illusion it is a part of the window, but I wonder if in the user32 api there is such a method.
So far, I found a way to disable the minimize/maximize/close buttons but not a way to add a custom one to them. It seems strange to me.
Here is what I am trying to achieve:
I have been wondering how it is done here, since drawing a button for every window using gdi/gdi+ and then detecting if it is overlapped by another window and then displaying only the non-overlapped part seems to me like an unlikely solution. Probably the button has been registered in the window class so that every window has this button. Any pointers what to do?
In addition, how do I create a button at all, assuming I DON'T have Unicode enabled. Then in the following piece of code:
HWND hwndCommandLink = CreateWindow(
L"BUTTON", // Class; Unicode assumed.
L"", // Text will be defined later.
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_COMMANDLINK, // Styles.
10, // x position.
10, // y position.
100, // Button width.
100, // Button height.
hDlg, // Parent window.
NULL, // No menu.
(HINSTANCE)GetWindowLong(hDlg, GWL_HINSTANCE),
NULL); // Pointer not needed.
SendMessage(clHwnd, WM_SETTEXT, 0, (LPARAM)L"Command link");
SendMessage(clHwnd, BCM_SETNOTE, 0, (LPARAM)L"with note");
I have to substitute all the nice Windows constants with their long equivalent....However, when I search for them, all i get is this:
http://msdn.microsoft.com/en-us/library/bb775951(v=VS.85).aspx
Any pointers?

Capturing a Window that is hidden or minimized

I followed this tutorial (there's a bit more than what's listed here because in my code I get a window via mouse click) for grabbing a window as a bitmap and then rendering that bitmap in a different window.
My question:
When that window is minimized or hidden (SW_HIDE) my screen capture doesn't work, so is it possible to capture a window when it is minimized or hidden?
The PrintWindow api works well, I use it for capturing thumbnails for hidden windows. Despite the name, it is different than WM_PRINT and WM_PRINTCLIENT, it works with pretty much every window except for Direct X / WPF windows.
I added some code (C#) but after reviewing how I used the code, I realized that the window isn't actually hidden when I capture its bitmap, its just off screen so this may not work for your case. Could you show the window off screen, do a print and then hide it again?
public static Bitmap PrintWindow(IntPtr hwnd)
{
RECT rc;
WinUserApi.GetWindowRect(hwnd, out rc);
Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
Graphics gfxBmp = Graphics.FromImage(bmp);
IntPtr hdcBitmap = gfxBmp.GetHdc();
bool succeeded = WinUserApi.PrintWindow(hwnd, hdcBitmap, 0);
gfxBmp.ReleaseHdc(hdcBitmap);
if (!succeeded)
{
gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
}
IntPtr hRgn = WinGdiApi.CreateRectRgn(0, 0, 0, 0);
WinUserApi.GetWindowRgn(hwnd, hRgn);
Region region = Region.FromHrgn(hRgn);
if (!region.IsEmpty(gfxBmp))
{
gfxBmp.ExcludeClip(region);
gfxBmp.Clear(Color.Transparent);
}
gfxBmp.Dispose();
return bmp;
}
There are WM_PRINT and WM_PRINTCLIENT messages you can send to the window, which cause its contents to be rendered into the HDC of your choice.
However, these aren't perfect: while the standard Win32 controls handle these correctly, any custom controls in the app might not.
I am trying to get the bitmap of partially hidden controls.
I used code before that did the drawing, but included windows overlapping it. So.. maybe you want to try this.
The WM_PRINTCLIENT should (in my understanding) redraw all inside the control, even if it is not really visible.
const int WM_PRINT = 0x317, WM_PRINTCLIENT = 0x318, PRF_CLIENT = 4,
PRF_CHILDREN = 0x10, PRF_NON_CLIENT = 2,
COMBINED_PRINTFLAGS = PRF_CLIENT | PRF_CHILDREN | PRF_NON_CLIENT;
SendMessage(handle, WM_PRINTCLIENT, (int)hdc, COMBINED_PRINTFLAGS);
//GDIStuff.BitBlt(hdc, 0, 0, width, height, hdcControl, 0, 0, (int)GDIStuff.TernaryRasterOperations.SRCCOPY);
The before code is commented out now. It is based on the code found here: Pocket PC: Draw control to bitmap (accepted answer). It is basically the same as Tim Robinson suggests in this thread.
Also, have a look here
http://www.tcx.be/blog/2004/paint-control-onto-graphics/

Resources