梅林日記

雑用系プログラマの趣味とお仕事

目次

Blog 利用状況

ニュース

資格

MCAD OCP

書庫

日記カテゴリ

WM_PAINTを使ったTextBoxのカスタム描画

数日前のえムナウさんのエントリ「TextBoxを自分で描画する」を読みました。

 

TextBoxに入力もしくはデータバインドしたデータを、数値や日付データとして

フォーマットして表示するという機能は以前から必要としておりまして、これまでは

Textプロパティをoverrideするという無理やりな方式を採用していました。

 

できればTextプロパティはそのままで、表示するデータだけを変えるようにしたいと思っていたので

これはいい、と早速試してみたのですが、書いてあるとおりSetStyleやRecreateHandleを記述しても

どうしてもフォーカスがあるときと無いときでフォントの大きさが変わってしまいます。

 

色々調べた結果、WM_PAINTメッセージを捕まえてCreateGraphicsで作成したGraphicsオブジェクトに

文字列の描画を行う方式に落ち着きそうです。


namespace Umebayashi.Framework.Windows.Forms
{
    public partial class UmeTextBox : TextBox
    {
        public UmeTextBox()
        {
            InitializeComponent();
        }

        private const int WM_PAINT = 0x000F;

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_PAINT:
                    //フォーカスがある状態ではフォーマットしない
                    if (!Focused)
                    {
                        DrawText();
                    }
                    break;
                default:
                    break;
            }
        }

        private void DrawText()
        {
            TextFormatFlags tff = TextFormatFlags.VerticalCenter;
            switch (this.TextAlign)
            {
                case HorizontalAlignment.Center:
                    tff |= TextFormatFlags.HorizontalCenter;
                    break;
                case HorizontalAlignment.Left:
                    tff |= TextFormatFlags.Left;
                    break;
                case HorizontalAlignment.Right:
                    tff |= TextFormatFlags.Right;
                    break;
            }
            if (this.Multiline)
            {
                tff |= TextFormatFlags.WordBreak;
            }

            using (Graphics g = CreateGraphics())
            using (Brush backBrush = new SolidBrush(this.BackColor))
            {
                //BackColorで背景を塗りつぶす。
                //これをしないとフォーマット前後の文字列がダブって表示される。
                g.FillRectangle(backBrush, this.ClientRectangle);

                DateTime dtVal;
                decimal dcVal;
                if (DateTime.TryParse(this.Text, out dtVal))
                {
                    TextRenderer.DrawText(g, dtVal.ToString("yyyy/MM/dd"),
                        this.Font, this.ClientRectangle, this.ForeColor, tff);
                }
                else if (decimal.TryParse(this.Text, out dcVal))
                {
                    TextRenderer.DrawText(g, dcVal.ToString("#,##0"),
                        this.Font, this.ClientRectangle, this.ForeColor, tff);
                }
                else
                {
                    TextRenderer.DrawText(g, this.Text,
                        this.Font, this.ClientRectangle, this.ForeColor, tff);
                }
            }
        }
    }
}

投稿日時 : 2007年2月15日 0:03

コメントを追加

# TextBoxを自分で描画する2 2007/02/15 10:03 えムナウ Blog

TextBoxを自分で描画する2

# re: WM_PAINTを使ったTextBoxのカスタム描画 2007/06/01 16:42 通りすがりのプログラマ

ありがとうございます。
参考にさせていただきました。

# re: WM_PAINTを使ったTextBoxのカスタム描画 2017/05/06 18:32 Teddy

With millions of players playing this game on iPhone, you are sure to have unlimited amounts of fun with Diamond Space - Jewel Dash. http://fun-torrent.ru Create and share your own menus and actions - both for proximity and when the phone rings - using Applescript, key and mouse emulation.

# re: WM_PAINTを使ったTextBoxのカスタム描画 2017/07/06 21:55 オースティン


1枚のカードを勉強するときにクラッシュする問題を修正し、オプションをもっと勉強し始めました。 http://getfreegamesplease.usエンターテイメントカンパニーは、世界でも有??数のライセンシングおよびリテールマーチャンダイジング機関の1つです。

# re: WM_PAINTを使ったTextBoxのカスタム描画 2017/08/25 6:50 Harry

Integration with Monster Puzzles Export your creations to Monster Puzzles and make them into puzzles.http://finansovye-sistemy.ru/photography/cours-mikroc-pdf.php It is customized only for any apple device like, iPhone iPod and iPad touch.

# re: WM_PAINTを使ったTextBoxのカスタム描画 2017/09/22 2:44 Matthew

Import options (file name, size, type, Created date, dimensions, and a custom field).http://nevere.ru/communication/sangeetha-jathi-mullai-song.php It could replace standard Explorer and other file manager.

# re: WM_PAINTを使ったTextBoxのカスタム描画 2017/10/18 15:15 Matthew

Simply a must for anyone wanting to improve go further in life:http://downloadtorrentfromsadie.online This will keep earning you revenuedisclaimer - GE Project Solutions and supaf.

# 名前をブラウザに記憶する 2017/10/30 16:54 Harvey

SSuite Fandango Desktop can either be downloaded and used offline.http://downloadsoftwarefromstella.club The story is very cute and easy to follow and the interactive features are diverse and fun.

# re: WM_PAINTを使ったTextBoxのカスタム描画 2017/11/10 5:48 Charlie

Set the drumming timer for up to infinity and listen in meditation or while performing everyday tasks.http://downloadtorrentfromnora.online Would you like to control your Pan/Tilt/Zoom device while at work, away on vacation or on business trips.

# re: WM_PAINTを使ったTextBoxのカスタム描画 2017/11/22 6:39 Matthew

Listen to recordings in the background while you use your device.http://getwhatyouneed.gdn It mimics the old desktop-style weather displays and can turn your iPad into a desktop weatherstation or a bedside clock.

# re: Internet Explorer 11 で、右クリックしたときに表示されるメニューで、Bingではなく、Google をデフォルトの検索エンジンとして設定するには? 2017/12/08 18:14 meadc

http://www.nfl-shop.co nfl store
http://www.nike-chaussures.fr/ Air Max TN Homme
http://www.culinar-hannover.de/ Nike Air Force
http://www.pradahandbags.co prada handbags
http://www.burberry-outletsale.us/ burberry outlet sale store
me adc12.8

# re: WM_PAINTを使ったTextBoxのカスタム描画 2018/07/18 11:11 zzyytt

http://www.nikeshoesformen.us.com
http://www.adidasnmd-runner.us.com
http://www.hermesbelts.co.uk
http://www.outletonline-michaelkors.us.org
http://www.russellwestbrookshoes.us.com
http://www.curry4.us.com

# I am genuinely grateful to the owner of this site who has shared this impressive piece of writing at here. 2018/10/07 12:46 I am genuinely grateful to the owner of this site

I am genuinely grateful to the owner of this site who has shared this impressive piece of writing at here.

# Good day! This post couldn't be written any better! Reading this post reminds me of my good old room mate! He always kept chatting about this. I will forward this page to him. Pretty sure he will have a good read. Thanks for sharing! 2018/11/14 12:33 Good day! This post couldn't be written any bette

Good day! This post couldn't be written any
better! Reading this post reminds me of my good old room mate!
He always kept chatting about this. I will forward this page
to him. Pretty sure he will have a good read. Thanks for sharing!

# fcj0rhh1 2019/02/01 9:09 Aaronunwit

http://tadalafiloffers.us.com/ - tadalafil 10mg

# yocn7uaq 2019/02/01 20:38 Aaronunwit

http://advairbest.us.org/ - advair

# rdv67zsc 2019/02/02 19:40 Aaronunwit

http://orlistatbest.us.org/ - generic orlistat

# 73ceg59q 2019/02/05 17:12 Aaronunwit

http://toprolxlbest.us.com/ - helpful resources

# mlsvqdk3 2019/02/06 15:26 Aaronunwit

http://genericalbenza.com/ - Albenza

# un2zv90y 2019/02/07 14:16 Aaronunwit

http://amoxilbest.us.org/ - amoxil

# 6cceoybc 2019/02/08 6:10 Aaronunwit

http://genericplavix.com/ - plavix clopidogrel bisulfate

# u8kb3z6e 2019/02/09 15:27 Aaronunwit

http://motiliumbest.us.org/ - generic motilium

# ankla8mh 2019/02/10 13:22 Aaronunwit

http://indocinbest.us.com/ - purchase indocin

# u6enpf9a 2019/02/25 3:29 Aaronunwit

http://zofran.us.com/ - zofran

# b552ub53 2019/02/25 14:30 Aaronunwit

http://metforminnorx.us.com/ - Generic Metformin

# 5hxapma5 2019/02/25 18:14 Aaronunwit

http://buydiclofenac.us.com/ - diclofenac online

# 7y04qk97 2019/02/26 9:20 Aaronunwit

http://cafergot365.us.com/ - cafergot online

# 2txuffvz 2019/02/26 13:07 Aaronunwit

http://zofran.us.com/ - Where Can You Buy Zofran

# uger9hc4 2019/02/27 0:51 Aaronunwit

http://buyalbuterol.us.com/ - more hints

# z4wurk9b 2019/02/27 12:03 Aaronunwit

http://clomidnorxprice.com/ - clomid without a prescription

# u40svufq 2019/02/27 15:52 Aaronunwit

http://buycitalopram.us.org/ - buy citalopram

# akur7j2a 2019/02/28 7:39 Aaronunwit

http://disulfiram.us.com/ - purchase antabuse

# gpm0tawm 2019/02/28 11:23 Aaronunwit

http://abilify.us.com/ - cost of abilify

# uo6xkgp2 2019/02/28 15:15 Aaronunwit

http://tretinoincream.us.com/ - tretinoin cream

# firt9dh8 2019/02/28 23:09 Aaronunwit

http://tadalafilcompareprice.com/ - generic tadalafil 40 mg

# 7z7zjwjf 2019/03/01 3:06 Aaronunwit

http://buybuspar.us.com/ - generic for buspar

# 6kk25gtq 2019/03/01 10:42 Aaronunwit

http://hydrochlorothiazide12.us.org/ - hctz 12

# vci4amas 2019/03/01 14:30 Aaronunwit

http://propranololnorxprice.com/ - propranolol 20 mg

# 94ynixwb 2019/03/01 18:21 Aaronunwit

http://metformin2017.us.com/ - metformin

# zeqr9x2g 2019/03/03 12:36 Aaronunwit

http://motilium.us.com/ - motilium

# mqymesbw 2019/03/03 16:27 Aaronunwit

http://proventil.us.com/ - proventil price

# 0ijbdtiq 2019/03/03 20:18 Aaronunwit

http://tretinoin911.us.com/ - tretinoin

# g04q3biv 2019/03/04 0:13 Aaronunwit

http://sildenafil20mg.us.com/ - sildenafil 20 mg

# skdku8x8 2019/03/22 5:55 Aaronunwit

http://buyclomid.team/ - clomid

# phfpm8na 2019/03/22 21:39 Aaronunwit

http://stromectol.run/ - stromectol

# 5kmj1ulz 2019/03/24 5:46 Aaronunwit

http://tetracycline2020.com/ - tetracycline

# wmrycq35 2019/03/24 18:10 Aaronunwit

http://anafranil.network/ - buy anafranil online

# oc8sieqf 2019/03/24 21:35 Aaronunwit

http://baclofen.srl/ - baclofen

# 3evphlz3 2019/03/25 10:10 Aaronunwit

http://cymbalta.network/ - cymbalta

# nlby4d19 2019/03/26 2:17 Aaronunwit

http://orlistat.institute/ - buy orlistat

# y1korz4j 2019/03/26 5:44 Aaronunwit

http://toprolxl.network/ - toprol xl

# 9nfu30v4 2019/03/27 10:42 Aaronunwit

http://trazodone.recipes/ - trazodone

# z8bbt0cj 2019/03/28 6:38 Aaronunwit

http://buyprednisolone.network/ - prednisolne online

# This web site definitely has all of the information I needed about this subject and didn't know who to ask. 2019/04/02 20:16 This web site definitely has all of the informatio

This web site definitely has all of the information I needed about this subject and didn't know who to ask.

# Hi to all, how is the whole thing, I think every one is getting more from this website, and your views are fastidious in favor of new users. 2019/04/08 2:10 Hi to all, how is the whole thing, I think every o

Hi to all, how is the whole thing, I think every one is
getting more from this website, and your views are fastidious in favor of new users.

# ezzk4krz 2019/04/11 5:02 Aaronunwit

http://trazodone.run/ - trazodone hcl 50 mg

# ys0fwiux 2019/04/11 5:38 Aaronunwit

http://amoxil.network/ - learn more here

# hvkwa3fv 2019/04/11 9:55 Aaronunwit

http://diflucan.irish/ - diflucan

# f6bcq9lt 2019/04/11 13:01 Aaronunwit

http://betnovate.network/ - betnovate cream

# ntute86d 2019/04/11 14:16 Aaronunwit

http://bupropion.joburg/ - bupropion

# q19jo28c 2019/04/11 19:05 Aaronunwit

http://amoxil.golf/ - amoxil

# mev9tose 2019/04/11 22:08 Aaronunwit

http://indocin.run/ - indocin 50 mg tablets

# fde61psd 2019/04/12 4:27 Aaronunwit

http://buytrazodone.network/ - generic trazodone

# pdlt95xy 2019/04/12 8:45 Aaronunwit

http://baclofen2020.com/ - baclofen

# 8u5ucqwa 2019/04/12 13:36 Aaronunwit

http://metformin.durban/ - metformin

# i9w4dwmj 2019/04/12 16:48 Aaronunwit

http://tamoxifen.international/ - tamoxifen

# yv4drqgm 2019/04/12 18:08 Aaronunwit

http://strattera.wtf/ - strattera

# y6nrqj0h 2019/04/12 18:44 Aaronunwit

http://trazodone.run/ - trazodone

# 1i7ujba7 2019/04/13 2:27 Aaronunwit

http://cafergot.recipes/ - cafergot

# duqmloyf 2019/04/13 11:42 Aaronunwit

http://estrace.team/ - estrace

# gihyomz2 2019/04/13 13:32 Aaronunwit

http://buymotilium.team/ - buy motilium online

# b7ijfbme 2019/04/13 20:53 Aaronunwit

http://bactrim.wtf/ - buying bactrim

# osjha5x2 2019/04/13 22:46 Aaronunwit

http://xenical1038.com/ - orlistat over the counter

# 3iqmgkex 2019/04/14 9:41 Aaronunwit

http://buyfurosemide.network/buy/cost-of-furosemide.html - furosemide cost

# vvgazrs7 2019/04/14 16:59 Aaronunwit

http://diflucan.joburg/med/buy-diflucan-yeast-infection.html - buy diflucan yeast infection

# xyz0gp1v 2019/04/14 18:48 Aaronunwit

http://buytadalafil.network/1/ - tadalafil price

# 35yk258q 2019/04/14 23:03 Aaronunwit

http://buyfurosemide.network/buy/buy-furosemide-online.html - buy furosemide online

# bn1mdswe 2019/04/15 2:13 Aaronunwit

http://buymotrin.run/5/600-mg-motrin.html - 600 mg motrin

# n71svi9p 2019/04/15 3:30 Aaronunwit

http://metformin2020.com/store/metformin-1000.html - metformin er 500mg

# jaloysqo 2019/04/15 22:16 Aaronunwit

http://buyfurosemide.network/buy/cost-of-furosemide.html - cost of furosemide

# ceoqwji1 2019/04/16 2:34 Aaronunwit

http://diflucan.joburg/med/diflucan-150mg.html - diflucan 150mg

# yu7hetm8 2019/04/16 14:28 Aaronunwit

http://buyalbendazoleonlinenorx.us.com/buyalbendazole/where-can-i-buy-albendazole.html - albendazole cost

# 85eiqc37 2019/04/16 16:17 Aaronunwit

http://sildenafilnoednorx.com/buy/sildenafil-online.html - sildenafil

# 98dn8yqp 2019/04/16 23:21 Aaronunwit

http://metformin2020.com/store/metformin-1000-mg-no-prescription.html - ordering metformin on line without a prescription

# hazv3i65 2019/04/18 2:34 Aaronunwit

http://diflucan.joburg/med/diflucan-cream.html - buy diflucan no prescription

# uichn2ra 2019/04/18 4:01 Aaronunwit

http://antabuse.capetown/2/buying-antabuse-online.html - buy generic antabuse

タイトル  
名前  
URL
コメント