🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4002

Re: Length of text in MessageBox function

#4002
From: Richard Casten
Date: Thu, 30 Mar 2006 23:39
110 lines
4912 bytes
Here is the code:

#include <windows.h>
LRESULT CALLBACK (*old_window_proc)(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM
lParam);
LRESULT CALLBACK new_window_proc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM
lParam);

LRESULT CALLBACK hook_proc(int nCode, WPARAM wParam, LPARAM lParam);
HWND hwnd;
int pos;
char *string = "Error in Repsat\n ";
int main(int argc, char **argv)
{
     HHOOK hook;

     pos = atoi(argv[1]);
     hook = SetWindowsHookEx(WH_CBT,hook_proc,NULL,GetCurrentThreadId());

     MessageBox(NULL,string,"Error",MB_OK);
     SetWindowLong(hwnd,GWL_WNDPROC,(LONG)old_window_proc);
     UnhookWindowsHookEx(hook);
     return(0);
}
LRESULT CALLBACK hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
{
     char class_name[100];

     if(nCode < 0)
          return(CallNextHookEx(NULL,nCode,wParam,lParam));

     if(nCode == HCBT_CREATEWND)
     {
          hwnd = (HWND)wParam;
          GetClassName(hwnd,class_name,sizeof(class_name));
          if(stricmp(class_name,"Static") == 0)
               old_window_proc = (WNDPROC)SetWindowLong(hwnd,GWL_WNDPROC,
                                                       (LONG)new_window_proc);
     }

     return(0);
}
LRESULT CALLBACK new_window_proc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM
lParam)
{
     SIZE size_string,size_character,size_asterisk;
     RECT rect;
     HDC hdc;
     static HFONT fnt;

     if(uMsg == WM_SETFONT)
     {
          LOGFONT lFont;

          fnt = (HFONT)wParam;
          hdc = GetDC(hwnd);
          if (GetObject(fnt, sizeof(LOGFONT), (LPSTR) &lFont) != 0)
          {
             /* RESET TO BOLD */
                lFont.lfWeight = FW_BOLD;
               fnt = CreateFontIndirect(&lFont);
               wParam = (WPARAM)fnt;
          }
          GetWindowRect(hwnd,&rect);
          GetTextExtentPoint32(hdc,string,strlen(string),&size_string);
          if(rect.right - rect.left + 1 < size_string.cx)
          {
               HWND hwnd_parent;
               int diff;

               diff = size_string.cx - (rect.right - rect.left + 1);
               hwnd_parent = GetParent(hwnd);
               GetWindowRect(hwnd_parent,&rect);
               SetWindowPos(hwnd_parent,NULL,rect.left,rect.top,
                            rect.right - rect.left + 1 + diff,
                            rect.bottom - rect.top + 1,
                            SWP_NOZORDER | SWP_SHOWWINDOW);

               GetWindowRect(hwnd,&rect);
               SetWindowPos(hwnd,NULL,rect.left,rect.top,
                            rect.right - rect.left + 1 + diff,
                            rect.bottom - rect.top + 1,
                            SWP_NOZORDER | SWP_SHOWWINDOW);
          }
     }

     else if(uMsg == WM_PAINT)
     {
          CallWindowProc(old_window_proc,hwnd,uMsg,wParam,lParam);
          hdc = GetDC(hwnd);
          SelectObject(hdc,(HGDIOBJ)(HFONT)fnt);

          SetBkMode(hdc, TRANSPARENT);
          GetWindowRect(hwnd,&rect);
          GetTextExtentPoint32(hdc,string,pos-1,&size_string);
          GetTextExtentPoint32(hdc,&string[pos-1],1,&size_character);
          GetTextExtentPoint32(hdc,"*",1,&size_asterisk);
          rect.right = rect.right - rect.left;
          rect.bottom = rect.bottom - rect.top;
          rect.left = size_string.cx +
                      (size_character.cx - size_asterisk.cx)/2;
          rect.top = 0;
          DrawText(hdc,"*",1,&rect,DT_SINGLELINE | DT_BOTTOM | DT_INTERNAL);

          ReleaseDC(hwnd,hdc);
          return(0);
     }

     return(CallWindowProc(old_window_proc,hwnd,uMsg,wParam,lParam));
}
Technical Details
Message-ID: <442C6C38.7357B9C0@aero.org>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4422D533.32E31292@aero.org> <4422ea15$1@newsgroups.borland.com> <4422FA56.83B7F39@aero.org> <442319ab@newsgroups.borland.com> <442ADEA7.B11F561C@aero.org> <442af3e1$1@newsgroups.borland.com> <442C0FDB.EE56D321@aero.org> <442C24BE.8091C591@aero.org> <442c59a6$3@newsgroups.borland.com> <442C615F.2A382D1D@aero.org> <442c66f0$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b8d4aea1a094904b
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Message-ID: <442C6C38.7357B9C0@aero.org>
Date: Thu, 30 Mar 2006 15:39:36 -0800
From: Richard Casten <richard.casten@aero.org>
X-Mailer: Mozilla 4.75 [en] (Windows NT 5.0; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Length of text in MessageBox function
References: <4422D533.32E31292@aero.org> <4422ea15$1@newsgroups.borland.com> <4422FA56.83B7F39@aero.org> <442319ab@newsgroups.borland.com> <442ADEA7.B11F561C@aero.org> <442af3e1$1@newsgroups.borland.com> <442C0FDB.EE56D321@aero.org> <442C24BE.8091C591@aero.org> <442c59a6$3@newsgroups.borland.com> <442C615F.2A382D1D@aero.org> <442c66f0$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 130.221.27.185
X-Trace: newsgroups.borland.com 1143761871 130.221.27.185 (30 Mar 2006 15:37:51 -0700)
Lines: 113
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail