📜 ⬆️ ⬇️

Windows Mobile: Focus Issues in ImageButton

Good day to all!

The problem is the following: an ImageButton control is required for a Windows Mobile Application that combines the behavior of Button and PictureBox controls.

ImageButton options:
1. Use the Button control. Problem: the lack of the Image property as in WinForms analog.
2. Use the PictureBox control that supports the OnClick event. Problem: PictureBox does not support the Focus method (as well as its successors). For the sake of fairness, it can be said that this method can be called programmatically, but visually it will not be noticeable. In addition, we can not switch using taba between multiple controls such as PictureBox.
3. Inherit from Button and try to implement the Image property yourself. This way I could not achieve the desired result.
4. Inherit from Control and implement your control's drawing logic, as Mictrosoft recommends doing. Problem: the focus frame is not drawn. We will try to solve this problem.
')

Microsoft ImageButton


Microsoft proposes to inherit from Control and implement all the logic of drawing independently. This is what happens (I submit with minor code changes, the control itself can be downloaded from here ):

public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .

double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .

double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .

public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .

public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .

:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .

OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .

Focused , - . . , , , .

PS , .
public class ImageButton : Control
{
public Image Image
{
get ;
set ;
}

private Bitmap m_bmpOffscreen;

public ImageButton()
{
this .Size = new Size(21, 21);
}

protected override void OnPaint(PaintEventArgs e)
{
try
{

if ( this .Image != null )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null ) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap (ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics .FromImage(m_bmpOffscreen);

//gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage( this .Image, 0, 0, this .ClientRectangle, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this .ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

//Draw from the memory bitmap
e. Graphics .DrawImage(m_bmpOffscreen, 0, 0);

gxOff.Dispose();
}
}
catch (Exception ex)
{

}
}

private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap (image);
return bmp.GetPixel(0, 0);
}

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
}

protected override void OnPaintBackground(PaintEventArgs e)
{
//Do nothing
}
}


* This source code was highlighted with Source Code Highlighter .


double buffering, ImageButton .


, - focused. - . :

OnPaint :

gxOff.DrawRectangle( new Pen(Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


:

gxOff.DrawRectangle( new Pen(( this .Focused) ? Color.Black : Color.Transparent), rc);

* This source code was highlighted with Source Code Highlighter .


OnGotFocus OnLostFocus, Invalidate :

protected override void OnGotFocus( EventArgs e)
{
base .OnGotFocus(e);
this .Invalidate();
}

protected override void OnLostFocus( EventArgs e)
{
base .OnLostFocus(e);
this .Invalidate();
}


* This source code was highlighted with Source Code Highlighter .


Focused , - . . , , , .

PS , .

Source: https://habr.com/ru/post/66788/


All Articles